springmvc註解入門和專案使用靜態資源
阿新 • • 發佈:2018-12-11
第一步:配置各種檔案
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zit</groupId> <artifactId>springmvc02</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>springmvc02</name> <dependencies> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.12</version> </dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.2</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.0.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2.1-b03</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> <testSourceDirectory>src/test/java</testSourceDirectory> <sourceDirectory>src/main/java</sourceDirectory> <!-- 解決無法載入資源配置檔案 --> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> </resource> </resources> <plugins> <!-- 配置maven編譯外掛 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- 配置jetty servlet伺服器 --> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.4.12.v20180830</version> <configuration> <stopKey>exit</stopKey> <stopPort>8989</stopPort> <scanIntervalSeconds>10</scanIntervalSeconds> <reload>manual</reload> <webAppConfig> <contextPath>/</contextPath> </webAppConfig> <httpConnector> <port>80</port> </httpConnector> </configuration> <executions> <execution> <id>jetty-run</id> <phase>test</phase> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
src/main/resources/smvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 配置自動掃描的包 --> <context:component-scan base-package="com.zit.controller" /> <!-- 在沒有配置mvc:resources的時候沒有問題,一旦配置了mvc:resources,註解方式的url就沒有載入 --> <mvc:annotation-driven> <!-- 訊息轉換器 --> <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes" value="text/html;charset=UTF-8"/> </bean> </mvc:message-converters> </mvc:annotation-driven> <!--<mvc:resources mapping="/images/**" location="/images/" />--> <!-- 配置檢視解析器 如何把handler 方法返回值解析為實際的物理檢視 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/template/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
src/main/webapp/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <display-name>springmvc02</display-name> <!--springmvc 5.0.9--> <!--不使用註解,可以使用配置--> <!-- //@WebServlet(name = "springmvc", urlPatterns = {"/"}, loadOnStartup = 1, initParams = {@WebInitParam(name = "contextConfigLocation", value = "classpath:smvc.xml")})--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:smvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 配置spring框架中的編碼過濾器,實現接post請求中文亂碼處理 --> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--訪問靜態資源--> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.html</url-pattern> <url-pattern>*.css</url-pattern> <url-pattern>*.js</url-pattern> <url-pattern>*.jpg</url-pattern> <url-pattern>*.gif</url-pattern> <url-pattern>*.png</url-pattern> </servlet-mapping> <!--配置web專案的web.xml檔案的首頁--> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <error-page><!--當系統出現404錯誤,跳轉到頁面nopage.html--> <error-code>404</error-code> <location>/404.html</location> </error-page> <error-page><!--當系統出現java.lang.NullPointerException,跳轉到頁面error.html--> <exception-type>java.lang.Exception</exception-type> <location>/error.html</location> </error-page> <session-config><!--會話超時配置 ,單位分鐘--> <session-timeout>30</session-timeout> </session-config> </web-app>
第二步:建立各種檔案(按照順序建立)
src/main目錄下:
src/main/java/com.zit/
src/main/java/com.zit/controller/HelloController.java
package com.zit.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by webrx on 2018-09-21.
*/
@Controller
public class HelloController {
@GetMapping("/abc")
@ResponseBody
public void abc(PrintWriter out) {
out.print("hello world");
System.out.println("hello world");
}
@GetMapping("/show.do")
@ResponseBody
public void showasdf(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<meta charset=\"utf-8\">");
out.println("<title>玉靈 QQ:7031633 Email:[email protected]</title>");
out.println("<meta name=\"keywords\" content=\"關鍵字\">");
out.println("<meta name=\"description\" content=\"簡介\">");
out.println("</head>");
out.println("<body>");
out.println("<h3>Hello World 中文效果</h3>");
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
}
src/mian/java/com.zit/servlet/SpringSErvlet.java
package com.zit.servlet;
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
//@WebServlet(name = "springmvc", urlPatterns = {"/"}, loadOnStartup = 1, initParams = {@WebInitParam(name = "contextConfigLocation", value = "classpath:smvc.xml")})
public class SpringServlet extends DispatcherServlet {
}
src/main/resources/smvc.xml內容上面已經寫過了
src/main/webapp頁面
src/main/webapp/css包:src/main/webapp/css/index.css
h3{
color:red;
}
src/main/webapp/images包:src/main/webapp/images/a.jpg
一張圖片,Jpg就行
src/main/webapp/js/my.js
alert("ok")
src/main/webapp/WEB-INF/web.xml上面已經寫過了。
webapp主目錄下:
webapp/404.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>404</title>
</head>
<body>
<h3>404頁面找不到</h3>
</body>
</html>
webapp/error.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>error</title>
</head>
<body>
<h3>錯誤頁面</h3>
</body>
</html>
webapp/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="css/index.css">
<script src="js/my.js"></script>
</head>
<body>
<h3>hello world</h3>
<img src="images/a.jpg" alt="">
</body>
</html>
webapp/index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="abc">abc</a>
<br>
${3*2}
<%
int a=4/0;
%>
<a href="show.do">show檢視</a>
</body>
</html>
web包完成
再建立src/test/java包結束
結束。。。。。。