MVC框架对Controller层开发的支持
1.MVC基础配置
1. 基础的目录结构:
2. pom.xml中引入MVC模块
<?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.xyd</groupId>
<artifactId>gittest0908</artifactId>
<version>1.0-SNAPSHOT</version>
<!--打包格式-->
<packaging>war</packaging>
<!--设置jdk格式和编码格式-->
<properties>
<maven.complier.source>1.8</maven.complier.source>
<maven.complier.target>1.8</maven.complier.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--引入webmvc模块-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!--标准标签库-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--设置test依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
3. 配置Spring容器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">
<!--controller扫描注册到容器中-->
<context:component-scan base-package="com.example.controller"></context:component-scan>
<!--开启mvc驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="suffix" value=".jsp"></property>
<property name="prefix" value="/WEB-INF/"></property>
</bean>
</beans>
4. web.xml中配置依赖的Servlet类:DispatcherServlet
并将springxml导入到类参数中
<?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">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-controller.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
5. Controller层开发
TestController
@Controller
public class TestController {
@RequestMapping("/demo")
public String m1(){
return "demo1";
}
}
Demo1.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>Hello</h1>
</body>
</html>
6. @RequestMapping 注解
如果这个测试类想要处理一个地址下所有的子请求可以在类上加注解
下面案例表示处理的请求地址是:/rc/demo
默认请求方式为get,也可以在注解中使用
@RequestMapping(value = "/demo" ,method =RequestMethod.GET)
@Controller
@RequestMapping("/rc")
public class TestController {
@RequestMapping("/demo")
public String m1(){
return "demo1";
}
}
2.对Controller请求方式的处理
@RequestMapping("/demo1")//可以处理任意请求。
对于Get请求:
@GetMapping("/demo1")
//等价于:
@RequestMapping(value = "/demo1",method = RequestMethod.GET)
对于Post请求:
@PostMapping("/demo1")
//等价于:
@RequestMapping(value = "/demo1",method = RequestMethod.POST)
3.对Controller请求参数的获取
1.在Servlet中获取请求参数方式:
request.getParameter("参数名");
2.在Mvc框架中获取请求参数
- 在浏览器中传入的参数名与类中方法要接收的参数名一致
@Controller
@RequestMapping("/rc")
public class TestController {
@RequestMapping(value = "/demo")
public String m1(String a){
System.out.println(a);
return "demo1";
}
}
-
浏览器中传入的即使是数字,但返回到后端解析前默认是字符串,在servlet开发中是需要进行强转的,但在框架中,只要传递的是数字,接收类型也是数字类型,则会自动转换
-
将浏览器参数指定给特定的方法参数,使用
@RequestParam("浏览器传递的参数名")
注解修饰
@Controller
@RequestMapping("/rc")
public class TestController {
@RequestMapping(value = "/demo")
public String m1( @RequestParam("p_a") String a){
System.out.println(a);
return "demo1";
}
}
- 多个同名的参数获取
@Controller
@RequestMapping("/rc")
public class TestController {
@RequestMapping(value = "/demo")
public String m1( String[] a){
System.out.println(Arrays.asList(a));
return "demo1";
}
}
打印结果:
也能接收一个参数的传递,但如果没有参数传递则会报空指针异常
或者使用集合接收:
这里使用List接收时必须使用@RequestParam("a")
,否则会报错
@Controller
@RequestMapping("/rc")
public class TestController {
@RequestMapping(value = "/demo")
public String m1(@RequestParam("a") List<String> a){
System.out.println(a);
return "demo1";
}
}
- 将浏览器传递的参数封装到一个对象中
创建一个对象:
public class User {
private Integer id;
private String name;
private Integer age;
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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
浏览器传入:
controller代码:
@Controller
@RequestMapping("/rc")
public class TestController {
@RequestMapping(value = "/demo")
public String m1(User user){
System.out.println(user);
return "demo1";
}
}
显示结果:
框架中默认使用的是get/set方法,单独设置构造方法没有效果
3.请求参数中文乱码的解决
对于get方式获得的参数一般不存在中文乱码,主要是Post方式
将原来的jsp页面使用form表单提交,提交方式为post
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/rc/demo" method="post">
<input type="text" name="name" value="张三">
<input type="submit" value="提交">
</form>
</body>
</html>
Controller代码
@Controller
@RequestMapping("/rc")
public class TestController {
@RequestMapping("/demo")
public String m1(User user){
System.out.println(user);
return "demo1";
}
}
没有处理前显示结果:
对于中文乱码问题需要使用过滤器进行处理
在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">
<!--解决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>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置springMvc的servlet-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-controller.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
增加后显示结果:
4.对Controller请求头的获取
只需要将原来参数传递修饰的注解@RequestParam改成@RequestHeader即可
@Controller
@RequestMapping("/rc")
public class TestController {
@RequestMapping("/demo")
public String m1(@RequestHeader("User-Agent") String page){
System.out.println(page);
return "demo1";
}
}
显示结果: