第一步:配置各种文件
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:webrx@126.com</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包结束
结束。。。。。。