SpringMVC第一章
2019-07-26 本文已影响0人
韩克
SpringMVC第一章
@autor:韩克 2019-07-24 10:42:05
springMVC工作原理
yuanli.png注:
DispatcherServlet : 前置控制器,接收所有请求(如果配置/不包含 javap)
HandlerMapping: 解析请求格式的.判断希望要执行哪个具体的方法.
HandlerAdapter: 负责调用具体的方法.
ViewResovler:视图解析器.解析结果,准备跳转到具体的物理视图
环境搭建
-
导入 jar
1.jpg
-
在 web.xml 中配置Spring配置文件和Web监听器
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classPath:applicationContext.xml</param-value>
</context-param>
</web-app>
- 在 web.xml 中配置前端控制器DispatcherServlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.form</url-pattern>
</servlet-mapping>
</web-app>
注意:
1.<url-pattern>*.form</url-pattern>中的*.form对应的是
<form action="survey/2019.07.24/item.form" method="post">
2.<servlet-name>springMVC</servlet-name>决定了创建*-selvlet.xml文件名中的*
即最后创建的springMVC配置文件的名字为springMVC-selvlet.xml
- 在 WEB-INF 中创建*-selvlet.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:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 扫描注解 -->
<context:component-scan base-package="com.xt.handler"/>
</beans>
注意配置: <context:component-scan base-package="com.xt.handler"/> 这样才能将handler注册到DispatcherServlet中
- 字符编码过滤器
<!-- 在 web.xml 中配置 Filter -->
<filter>
<filter-name>encoding</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>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- 编写控制器类
使用@RequestParam 接受简单参数
@RequestMapping(value="/{week}/item",method=RequestMethod.POST)
public String getParam(@PathVariable("week") String week,
@RequestParam("name") String name,
@RequestParam("age") int age,
@RequestParam("tel") String tel,
@RequestParam("answer1") String a1,
@RequestParam("answer2") String a2,
@RequestParam("answer3") String a3) {
System.out.println(week+" "+"调查问卷");
System.out.println("姓名:"+name);
System.out.println("年龄:"+" "+age);
System.out.println("tel:"+" "+tel);
System.out.println("对本期的封面和内容版式设计:"+" "+a1);
System.out.println("喜欢的文章:"+" "+a2);
System.out.println("不喜欢的文章:"+" "+a3);
return "redirect:/survey.javap";
}
使用@ModelAttribute 接受一组数据,并封装成对象,并把该对象存储到request作用域中
@ModelAttribute("survey")Survey survey
@RequestMapping(value="/{week}/item",method=RequestMethod.POST)
public String getParam2(@PathVariable("week")String week , @ModelAttribute("survey")Survey survey) {
survey.setWeek(week);
System.out.println(survey.getWeek()+" "+"调查问卷");
System.out.println("姓名:"+survey.getName());
System.out.println("年龄:"+" "+survey.getAge());
System.out.println("tel:"+" "+survey.getTel());
System.out.println("对本期的封面和内容版式设计:"+" "+survey.getAnswer1());
System.out.println("喜欢的文章:"+" "+survey.getAnswer2());
System.out.println("不喜欢的文章:"+" "+survey.getAnswer3());
return "redirect:/survey.javap";
}
使用@PathVariable接受请求路径中的参数
@RequestMapping(value="/{week}/item",method=RequestMethod.POST)
@PathVariable("week")String week