SpringMVC
引言
java开源框架,Spring Framework的一个独立模块
MVC框架,在项目中开辟MVC层次架构
对控制器中的功能 包装 简化 扩展践行工厂模式,功能架构在工厂之上
mvc架构
MVC :Model 模型 View 视图 Controller 控制器
模型:即业务模型,负责完成业务中的数据通信处理,对应项目中的service和dao
视图:渲染数据,生成页面,对应项目的jsp
控制器:直接对接请求,控制mvc流程,调度模型,选择视图,对应项目中的servlet
开发流程
1.导入依赖
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
</dependencies>
2.配置核心(前端)控制器
作为一个mvc框架,首先要解决的是:如何能够接收到请求
所以mvc框架大都会设计一款前端控制器,选型在Servlet或Filter两者之一,在框架最前沿先工作,接受所有的请求。
此控制器接受到请求后,还会负责SpringMVC的核心调度管理,所以即是前端也是核
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">
<!--SpringMVC前端控制器-->
<servlet>
<servlet-name>ozl_mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--可选配置,不写就是懒汉式加载,写了就是饿汉式加载-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ozl_mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3.后端控制器
HelloController
@Controller
@RequestMapping("/hello")
public class HelloController{
@RequestMapping("/test1")
public String test1(){
System.out.println("hello1");
return "hello";
}
@RequestMapping("/test2")
public String test2(){
System.out.println("hello2");
return "hello";
}
}
4.配置文件
mvc.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="cn.ozl"></context:component-scan>
<!--启动mvc注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--视图解析器
作用:1.捕获后端控制器的返回值="index"
2.解析 在返回的前后拼接 /xxx.jsp
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
5.访问
hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
hello
</body>
</html>
接收请求参数
1.基本类型参数
ParmController
@Controller
@RequestMapping("/param")
public class ParmController {
// http://localhost:8080/param/test1?id=1&name=olz&gender=true&birth=20120/10/20%2018:58
@RequestMapping("/test1")
public String test1(Integer id, String name, Boolean gender, Date birth) {
System.out.println("test1");
System.out.println("id:" + id + "name:" + name + "gender:" + gender + "birth:" + birth);
return "hello";
}
}
输入http://localhost:8080/param/test1?id=1&name=olz&gender=true&birth=20120/10/20%2018:58
就可以接受到参数
2.实体收参(建议)
实体类User
public class User {
private Integer id;
private String name;
private Boolean gender;
private Date birth;
private String[] hobby;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", gender=" + gender +
", birth=" + birth +
", hobby=" + Arrays.toString(hobby) +
'}';
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean getGender() {
return gender;
}
public void setGender(Boolean gender) {
this.gender = gender;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
}
ParmController
// http://localhost:8080/param/test2?id=1&name=olz&gender=true&birth=20120/10/20%2018:58
@RequestMapping("/test2")
public String test2(User user){
System.out.println("test2");
System.out.println(user);
return "hello";
}
3.数组收参
param.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/param/test3">
id:<input type="text" name="id"><br>
name:<input type="text" name="name"><br>
gender:<input type="text" name="gender"><br>
birth:<input type="text" name="birth"><br>
<input type="checkbox" name="hobby" value="football">足球
<input type="checkbox" name="hobby" value="swim">游泳
<input type="checkbox" name="hobby" value="basketball">篮球<br>
<input type="submit" value="提交">
</form>
</body>
</html>
// http://localhost:8080/param/test3?hobby=football&hobby=swim&hobby=basketball
@RequestMapping("/test3")
public String test3(String[] hobby){
System.out.println("test3");
for (String s : hobby) {
System.out.println(s);
}
return "hello";
}
4.集合收参(了解)
param.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/param/test4" method="post">
id:<input type="text" name="users[0].id"><br>
name:<input type="text" name="users[0].name"><br>
gender:<input type="text" name="users[0].gender"><br>
<hr>
id:<input type="text" name="users[1].id"><br>
name:<input type="text" name="users[1].name"><br>
gender:<input type="text" name="users[1].gender"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
ParmController
@RequestMapping("/test4")
public String test4(UserList userList){
System.out.println("test4");
for (User user : userList.getUsers()) {
System.out.println(user);
}
return "hello";
}
5.路径参数
// {id} 命名路径
// {id} 等价于*
// @PathVariable将{id}路径匹配到值赋给id参数
@RequestMapping("/test5/{id}/{name}")
public String test5(@PathVariable("id") Integer id,@PathVariable("name") String name2){
System.out.println("test5");
System.out.println("id:"+id);
System.out.println("name:"+name2);
return "hello";
}
6.中文乱码
为了防止接收参数出现中文数据,导致乱码
页面字符统一
JSP:<%@page pageEncoding=“utf-8”%>
HTML:< meta charset=“UTF-8” >
tomcat字符集设置,对get请求中,中文参数乱码有效
tomcat配置文件:URIEnoding=utf-8
设置filter,对post请求中,中文参数乱码有效
<!--过滤会进行:request.setCharacterEncoding("utf-8");-->
<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>