1.新建一个Moudle , 添加web的支持!
导包
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.18</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
Maven可能存在资源过滤的问题,完善配置
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
resources,build过程中涉及的资源文件
targetPath,资源文件的目标路径
filtering,构建过程中是否对资源进行过滤,默认false
directory,资源文件的路径,默认位于${basedir}/src/main/resources/目录下
includes,一组文件名的匹配模式,被匹配的资源文件将被构建过程处理
excludes,一组文件名的匹配模式,被匹配的资源文件将被构建过程忽略。同时被includes和excludes匹配的资源文件,将被忽略。
filters,给出对资源文件进行过滤的属性文件的路径,默认位于${basedir}/src/main/filters/目录下。属性文件中定义若干键值对。在构建过程中,对于资源文件中出现的变量(键),将使用属性文件中该键对应的值替换。
testResources,test过程中涉及的资源文件,默认位于${basedir}/src/test/resources/目录下。这里的资源文件不会被构建到目标构件中
2.确定导入了SpringMVC 的依赖!
3.配置web.xml,注册DispatcherServlet
<?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">
<!-- 1,注册DispatcherServlet-->
<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:springmvc-servlet.xml</param-value>
</init-param>
<!-- 启动级别 1-->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- / 匹配所有请求:(不包括.jsp)-->
<!-- /* 匹配所有请求:(包括.jsp)-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4.编写SpringMVC的 配置文件!
名称: springmvc-servlet.xml: [servletname]-servlet.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">
5.添加 处理映射器
<!-- bean名称url处理程序映射器-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
6.添加 处理器适配器
<!-- 简单控制器处理程序适配器-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
7.添加 视图解析器
<!-- 内部资源视图解析器---:Dispatcherservlet给他的ModelAndview-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!-- 前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 后缀-->
<property name="suffix" value=".jsp"/>
</bean>
8,编写我们要操作业务Controller ,要么实现Controller接口,要么增加注解: 需要返回一个ModelAndView,装数据,封视图:
public class HelloController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
//ModelAndView 模型和视图
ModelAndView modelview = new ModelAndView();
//业务层
String result="hello-springmvc";
System.out.println(result);
//封装对象,放在ModelAndView中,Model
modelview.addObject("msg","Welcome--SpringMVC!");
//封装要跳转的视图,放在ModelAndView中
modelview.setViewName("hello");// /WEB-INF/jsp/hello.jsp
return modelview;
}
}
9,将自己的类交给SpringlOC容器,注册bean
<!-- handler-->
<bean id="/hello" class="com.gl.controller.HelloController" />
10,写要跳转的jsp页面,显示ModelandView存放的数据,以及我们的正常页面
<%--@elvariable id="msg" type="com.gl.controller.HelloController"--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>welcome</h1>
${msg}
</body>
</html>
11,配置Tomcat 启动测试!