<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> <display-name>first-springmvc-demo</display-name> <!-- 配置SpringMVC的前端控制器,对浏览器发送的请求统一进行处理 --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--配置 DispatcherServlet 的一个初始化参数:spring mvc 配置文件按的位置和名称--> <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> <!--设置springMVC的核心控制器所能处理的请求的请求路径/所匹配的请求可以是/login或.html或.js或.css方式的请求路径但是/不能匹配.jsp请求路径的请求--> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
<?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="net.biancheng.c"></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>
package net.biancheng.c.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; @Controller public class ParamController { @RequestMapping("/") public String sayHello() { return "index"; } /** * 通过 HttpServletRequest 获取请求参数 * @param request * @return */ @RequestMapping("/getRequestParam") public String requestParam(HttpServletRequest request) { String name = request.getParameter("name"); String url = request.getParameter("url"); System.out.println("name:" + name); System.out.println("url:" + url); return "index"; } }
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>C语言中文网</title> </head> <body> <h1 th:text="'欢迎您访问:C语言中文网'"></h1> </body> </html>
name:C语言中文网 url:task.lmcjl.com
@RequestMapping("/test") public String test(String name, String language) { System.out.println("a:" + a); System.out.println("b:" + b); return "success"; }
@RequestMapping("/test") public String test(String a, String b) { System.out.println("a:" + a); System.out.println("b:" + b); return "success"; }
@RequestMapping("/test") public String test(String a, Integer b) { System.out.println("a:" + a); System.out.println("b:" + b); return "success"; }
形参的数据类型 | 获取到的请求参数值 | 举例 |
---|---|---|
String(字符串) | 所有同名请求参数的值通过逗号(“,”)拼接在一起。 | "true,false,true" |
数组 |
由所有同名请求参数值组成的数组。 该数组通常为 String(字符串)类型的,如果所有同名请求参数值都符合同一个数据类型的规范,我们还可以使用与之对应的数据类型的数组进行接收。例如,如果所有同名请求参数的取值都是 true 或 false,那么我们就可以在控制器方法中使用 Boolean 类型数组的形参进行接收。 |
{"true", "false", "true"} |
package net.biancheng.c.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class FormalParamController { /** * 跳转到 user.html * @return */ @RequestMapping("/user") public String sayHello() { return "user"; } /** * 通过形参获取请求参数 * @param userId * @param userName * @param password * @return */ @RequestMapping("/getFormalParam") public String getFormalParam(String userId, String userName, String password) { System.out.println("userId:" + userId); System.out.println("userName:" + userName); System.out.println("password:" + password); return "success"; } }
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>C语言中文网</title> </head> <body> <form th:action="@{/getFormalParam}" method="post"> <table style="margin: auto"> <tr> <td>用户 ID:</td> <td><input type="text" name="userId" required><br></td> </tr> <tr> <td>用户名:</td> <td><input type="text" name="userName" required><br></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="password" required><br></td> </tr> <tr> <td colspan="2" align="center"><input type="submit"></td> </tr> </table> </form> </body> </html>
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>C语言中文网</title> </head> <body> <h1 th:text="'恭喜您,操作成功!'"></h1> </body> </html>
图1:通过形参获取请求参数-表单
图2:通过形参获取请求参数
userId:987654321 userName:admin password:admin
@RequestMapping("/testRequestParam") public String testRequestParam(@RequestParam("name") String username, @RequestParam("pass") String password) { System.out.println(username + "," + password); return "success"; }
属性 | 说明 |
---|---|
name |
请求中的参数名。 name 为 @RequestParam 注解 value 属性的别名,它与 value 属性完全等价。 |
value |
请求中的参数名。 value 为 @RequestParam 注解 name 属性的别名,它与 name 属性完全等价。 |
required |
该请求参数名是否必须,默认值为 true,即默认情况下请求中必须包含对应的请求参数名,否则就会抛出异常。 注意:required 属性是对请求参数名设置的规则,但并没有对该请求参数是否有值进行限制。也就是说,当 required 属性为 true 或没有设置 required 属性时,请求中就必须包含对应的参数名,至于该请求参数是否有值则无所谓。 |
defaultValue |
请求参数的默认值。 注意: defaultValue 属性会使 required ="true" 失效,即将 required 属性自动设置为 false。 |
package net.biancheng.c.entity; public class User { private String UserId; private String UserName; private Integer age; public String getUserId() { return UserId; } public void setUserId(String userId) { UserId = userId; } public String getUserName() { return UserName; } public void setUserName(String userName) { UserName = userName; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User{" + "UserId='" + UserId + '\'' + ", UserName='" + UserName + '\'' + ", age=" + age + '}'; } }
package net.biancheng.c.controller; import net.biancheng.c.entity.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class UserController { /** * 通过实体类获取请求参数 * * @param user * @return */ @RequestMapping("/getUser") public String getUser(User user) { System.out.println("userId:" + user.getUserId()); System.out.println("userName:" + user.getUserName()); System.out.println("password:" + user.getPassword()); return "success"; } }
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>C语言中文网</title> </head> <body> <form th:action="@{/getUser}" method="post"> <table style="margin: auto"> <tr> <td>用户 ID:</td> <td><input type="text" name="userId" required><br></td> </tr> <tr> <td>用户名:</td> <td><input type="text" name="userName" required><br></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="password" required><br></td> </tr> <tr> <td colspan="2" align="center"><input type="submit"></td> </tr> </table> </form> </body> </html>
图3:实体类获取请求参数-表单
图4:输入请求参数
userId:123456789 userName:java-user password:qwertyuiop
图5:表单输入中文
userId:123456789 userName:Cè??è¨?????????? password:987654321
<!--请求和响应的字符串过滤器--> <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>
userId:123456789 userName:C语言中文网 password:987654321
本文链接:http://task.lmcjl.com/news/14856.html