当前位置:   article > 正文

SpringMVC学习(四):通过实体类型的形参获取请求参数(以及 通过POJO获取)_请求参数是实体类

请求参数是实体类

SpringMVC中不需要用原生的servletAPI来获取参数了,这会大大简化我们的代码。

文档结构:

 

web.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. //配置编码过滤器
  7. <filter>
  8. <filter-name>CharacterEncodingFilter</filter-name>
  9. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  10. <init-param>
  11. <param-name>encoding</param-name>
  12. <param-value>UTF-8</param-value>
  13. </init-param>
  14. <init-param>
  15. <param-name>forceResponseEncoding</param-name>
  16. <param-value>true</param-value>
  17. </init-param>
  18. </filter>
  19. <filter-mapping>
  20. <filter-name>CharacterEncodingFilter</filter-name>
  21. <url-pattern>/*</url-pattern>
  22. </filter-mapping>
  23. //配置springMVC的前端控制器DispatcherServlet
  24. <servlet>
  25. <servlet-name>DispatcherServlet</servlet-name>
  26. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  27. <init-param>
  28. <param-name>contextConfigLocation</param-name>
  29. <param-value>classpath:springMVC.xml</param-value>
  30. </init-param>
  31. <load-on-startup>1</load-on-startup>
  32. </servlet>
  33. <servlet-mapping>
  34. <servlet-name>DispatcherServlet</servlet-name>
  35. <url-pattern>/</url-pattern>
  36. </servlet-mapping>
  37. </web-app>

springMVC.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. 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">
  6. <context:component-scan base-package="demo.controller"></context:component-scan>
  7. <!-- 配置Thymeleaf视图解析器 -->
  8. <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
  9. <property name="order" value="1"/>
  10. <property name="characterEncoding" value="UTF-8"/>
  11. <property name="templateEngine">
  12. <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
  13. <property name="templateResolver">
  14. <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
  15. <!-- 视图前缀 -->
  16. <property name="prefix" value="/WEB-INF/templates/"/>
  17. <!-- 视图后缀 -->
  18. <property name="suffix" value=".html"/>
  19. <property name="templateMode" value="HTML5"/>
  20. <property name="characterEncoding" value="UTF-8" />
  21. </bean>
  22. </property>
  23. </bean>
  24. </property>
  25. </bean>
  26. </beans>

 index.html:

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <h1>首页</h1> <br>
  9. <a th:href="@{/}">走到首页</a> <br>
  10. <a th:href="@{/testRest/1/admin}">测试路径中占位符-->/testRest</a> <br>
  11. <a th:href="@{/test}">测试-->/test</a> <br>
  12. <a th:href="@{/param}">测试请求参数-->param</a> <br>
  13. <a th:href="@{/param(username='哈哈哈',password='123456')}">测试请求参数-->param</a> <br>
  14. </body>
  15. </html>

success.html:

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. success
  9. </body>
  10. </html>

User.java:

  1. package demo.bean;
  2. public class User {
  3. private Integer id;
  4. private String username;
  5. private String password;
  6. private Integer age;
  7. private String sex;
  8. private String email;
  9. public User() {
  10. }
  11. public User(Integer id, String username, String password, Integer age, String sex, String email) {
  12. this.id = id;
  13. this.username = username;
  14. this.password = password;
  15. this.age = age;
  16. this.sex = sex;
  17. this.email = email;
  18. }
  19. public Integer getId() {
  20. return id;
  21. }
  22. public void setId(Integer id) {
  23. this.id = id;
  24. }
  25. public String getUsername() {
  26. return username;
  27. }
  28. public void setUsername(String username) {
  29. this.username = username;
  30. }
  31. public String getPassword() {
  32. return password;
  33. }
  34. public void setPassword(String password) {
  35. this.password = password;
  36. }
  37. public Integer getAge() {
  38. return age;
  39. }
  40. public void setAge(Integer age) {
  41. this.age = age;
  42. }
  43. public String getSex() {
  44. return sex;
  45. }
  46. public void setSex(String sex) {
  47. this.sex = sex;
  48. }
  49. public String getEmail() {
  50. return email;
  51. }
  52. public void setEmail(String email) {
  53. this.email = email;
  54. }
  55. @Override
  56. public String toString() {
  57. return "User{" +
  58. "id=" + id +
  59. ", username='" + username + '\'' +
  60. ", password='" + password + '\'' +
  61. ", age=" + age +
  62. ", sex='" + sex + '\'' +
  63. ", email='" + email + '\'' +
  64. '}';
  65. }
  66. }

TestController.java:

  1. package demo.controller;
  2. import demo.bean.User;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.PathVariable;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. @Controller
  7. public class TestController {
  8. @RequestMapping(value = "/")
  9. public String index(){
  10. return "index";
  11. }
  12. @RequestMapping(value = "/test")
  13. public String test(){
  14. return "success";
  15. }
  16. @RequestMapping(value = "/testRest/{id}/{username}")
  17. public String testRest(@PathVariable("id") String id,@PathVariable("username") String username){
  18. System.out.println("id:"+id+" username:"+username);
  19. return "success";
  20. }
  21. @RequestMapping(value = "/param")
  22. public String test_param(String username,String password){
  23. System.out.println("username:"+username+" password:"+password);
  24. return "success";
  25. }
  26. }

运行起来:

点击最下面的超链接:

跳转成功。再看看控制台:

和html文件中传的参数一致

这样就完成了通过实体类型的形参获取请求参数

但是还有一个小优化

如果实体类中的参数很多,那么控制类中的形参也会相应地增多,那么为了减少代码量,我们可以通过pojo获取请求参数 

在index.html中加写一个form表单,传比较多的参数:

  1. <form th:action="@{/testpojo}" method="get">
  2. 用户名:<input type="text" name="username"><br>
  3. 密码:<input type="password" name="password"><br>
  4. 性别:<input type="radio" name="sex" value="男"><input type="radio" name="sex" value="女"><br>
  5. 年龄:<input type="text" name="age"><br>
  6. 邮箱:<input type="text" name="email"><br>
  7. <input type="submit" value="使用pojo获取请求参数">
  8. </form>

 给控制类中传入的形参改为类:

  1. @RequestMapping(value = "/testpojo")
  2. public String testpojo(User user){
  3. System.out.println(user);
  4. return "success";
  5. }

运行起来

将表单填写完整,点击按钮

跳转成功,浏览器已经得到了参数。再看看控制台

 获取到了参数

这样可以减少我们传参时的代码量

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/67769
推荐阅读
相关标签