SpringMVC深入使用
(一)基于XML配置的使用
(1)配置
1.SpringMVC基础配置
2.XML配置Controller,HandlerMapping组件映射
3.XML配置ViewResolver组件映射
(2)步骤
1.在pom.xml中导入jar包
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
2.在web.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:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3.创建一个MyHandler类,并继承Controller
package handle;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyHandle implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
//装载模型数据和逻辑视图
ModelAndView modelAndView=new ModelAndView();
//添加模型数据
modelAndView.addObject("name","lili");
//添加逻辑视图
modelAndView.setViewName("index");
return modelAndView;
}
}
4.创建springmvc.xml,并在springmvc中配置HandlerMapping,Handler和视图解析器
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置HandlerMapping,将url请求映射到Handler-->
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<!--配置Mapping-->
<property name="mappings">
<props>
<!--配置test请求对应的Handler-->
<prop key="/test">testHandler</prop>
</props>
</property>
</bean>
<!--配置Handler-->
<bean id="testHandler" class="handle.MyHandle"></bean>
<!--配置视图解析器--><!--将逻辑视图解析成物理视图-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--配置前缀-->
<property name="prefix" value="/"></property>
<!--配置后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
5.在index.jsp页面上显示
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${name}
</body>
</html>
(二)基于注解方式的使用
(1)配置
1.SpringMVC基础配置
2. Controller,HandlerMapping通过注解进行映射
3.XML配置ViewResolver组件映射
(2)步骤
1.在pom.xml中导入jar包
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
2.在web.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:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3.创建一个AnnotationHandler类
package handle;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.Map;
//让容器知道这个类是以控制器存在的
@Controller
public class AnnotationHandler {
/**
* 业务方法1:ModelAndView完成数据的传递,视图的解析
*/
@RequestMapping("/ModelAndViewTest")
public ModelAndView ModelAndViewTest(){
//创建ModelAndView对象
ModelAndView modelAndView=new ModelAndView();
//填充模型数据
modelAndView.addObject("name","Tom");
//设置逻辑视图
modelAndView.setViewName("index");
return modelAndView;
}
/**
* 业务方法2:Model传值,String进行视图解析
*/
@RequestMapping("/ModelTest")
public String ModelTest(Model model){
//填充模型数据
model.addAttribute("name","Lulu");
//设置逻辑视图
return "index";
}
/**
* 业务方法3:Map传值,String进行视图解析
*/
@RequestMapping("/MapTest")
public String MapTest(Map<String,String> map){
//填充模型数据
map.put("name","Lisa");
//设置逻辑视图
return "index";
}
}
4.创建springmvc.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"
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">
<!--将AnnotationHandler自动扫描到IOC容器中-->
<context:component-scan base-package="handle"></context:component-scan>
<!--配置视图解析器--><!--将逻辑视图解析成物理视图-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--配置前缀-->
<property name="prefix" value="/"></property>
<!--配置后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
5.在index.jsp页面上显示
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${name}
</body>
</html>