赞
踩
在SpringMVC中不需要用原生的servletAPI来获取参数了,这会大大简化我们的代码。
文档结构:
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">
-
-
- //配置编码过滤器
- <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>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的前端控制器DispatcherServlet
- <servlet>
- <servlet-name>DispatcherServlet</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>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>DispatcherServlet</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
-
- </web-app>
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 https://www.springframework.org/schema/context/spring-context.xsd">
-
- <context:component-scan base-package="demo.controller"></context:component-scan>
-
- <!-- 配置Thymeleaf视图解析器 -->
- <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
- <property name="order" value="1"/>
- <property name="characterEncoding" value="UTF-8"/>
- <property name="templateEngine">
- <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
- <property name="templateResolver">
- <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
-
- <!-- 视图前缀 -->
- <property name="prefix" value="/WEB-INF/templates/"/>
-
- <!-- 视图后缀 -->
- <property name="suffix" value=".html"/>
- <property name="templateMode" value="HTML5"/>
- <property name="characterEncoding" value="UTF-8" />
- </bean>
- </property>
- </bean>
- </property>
- </bean>
-
- </beans>
index.html:
- <!DOCTYPE html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
-
- <h1>首页</h1> <br>
- <a th:href="@{/}">走到首页</a> <br>
- <a th:href="@{/testRest/1/admin}">测试路径中占位符-->/testRest</a> <br>
- <a th:href="@{/test}">测试-->/test</a> <br>
- <a th:href="@{/param}">测试请求参数-->param</a> <br>
- <a th:href="@{/param(username='哈哈哈',password='123456')}">测试请求参数-->param</a> <br>
-
-
- </body>
- </html>
success.html:
- <!DOCTYPE html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
-
- success
-
- </body>
- </html>
User.java:
- package demo.bean;
-
- public class User {
-
- private Integer id;
- private String username;
- private String password;
- private Integer age;
- private String sex;
- private String email;
-
- public User() {
- }
-
- public User(Integer id, String username, String password, Integer age, String sex, String email) {
- this.id = id;
- this.username = username;
- this.password = password;
- this.age = age;
- this.sex = sex;
- this.email = email;
- }
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getUsername() {
- return username;
- }
-
- public void setUsername(String username) {
- this.username = username;
- }
-
- public String getPassword() {
- return password;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
-
- public Integer getAge() {
- return age;
- }
-
- public void setAge(Integer age) {
- this.age = age;
- }
-
- public String getSex() {
- return sex;
- }
-
- public void setSex(String sex) {
- this.sex = sex;
- }
-
- public String getEmail() {
- return email;
- }
-
- public void setEmail(String email) {
- this.email = email;
- }
-
- @Override
- public String toString() {
- return "User{" +
- "id=" + id +
- ", username='" + username + '\'' +
- ", password='" + password + '\'' +
- ", age=" + age +
- ", sex='" + sex + '\'' +
- ", email='" + email + '\'' +
- '}';
- }
- }
TestController.java:
- package demo.controller;
-
- import demo.bean.User;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- @Controller
- public class TestController {
-
- @RequestMapping(value = "/")
- public String index(){
- return "index";
- }
-
- @RequestMapping(value = "/test")
- public String test(){
- return "success";
- }
-
- @RequestMapping(value = "/testRest/{id}/{username}")
- public String testRest(@PathVariable("id") String id,@PathVariable("username") String username){
- System.out.println("id:"+id+" username:"+username);
- return "success";
- }
-
- @RequestMapping(value = "/param")
- public String test_param(String username,String password){
- System.out.println("username:"+username+" password:"+password);
- return "success";
- }
-
-
- }
运行起来:
点击最下面的超链接:
跳转成功。再看看控制台:
和html文件中传的参数一致
这样就完成了通过实体类型的形参获取请求参数
但是还有一个小优化
如果实体类中的参数很多,那么控制类中的形参也会相应地增多,那么为了减少代码量,我们可以通过pojo获取请求参数
在index.html中加写一个form表单,传比较多的参数:
- <form th:action="@{/testpojo}" method="get">
- 用户名:<input type="text" name="username"><br>
- 密码:<input type="password" name="password"><br>
- 性别:<input type="radio" name="sex" value="男">男<input type="radio" name="sex" value="女">女<br>
- 年龄:<input type="text" name="age"><br>
- 邮箱:<input type="text" name="email"><br>
- <input type="submit" value="使用pojo获取请求参数">
- </form>
给控制类中传入的形参改为类:
- @RequestMapping(value = "/testpojo")
- public String testpojo(User user){
- System.out.println(user);
- return "success";
- }
运行起来
将表单填写完整,点击按钮
跳转成功,浏览器已经得到了参数。再看看控制台
获取到了参数
这样可以减少我们传参时的代码量
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。