一、逐个接收
1 import org.springframework.stereotype.Controller; 2 import org.springframework.web.bind.annotation.RequestMapping; 3 import org.springframework.web.servlet.ModelAndView; 4 ?5 @Controller // 表示当前类是一个处理器 6 @RequestMapping("/test") 7 public class MyController { 8 ?9 ????@RequestMapping("/register.do")10 ????public ModelAndView doRegister(String name, int age) {11 ????????// TODO Auto-generated method stub12 ????????13 ????????System.out.println("name=" + name);14 ????????System.out.println("age=" + age);15 ????????16 ????????ModelAndView mv = new ModelAndView();17 ????????mv.addObject("name", name);18 ????????mv.addObject("age", age);19 ????????mv.setViewName("/WEB-INF/jsp/welcome.jsp");20 ????????return mv;21 ????}22 23 }
1 <!-- ??注册组件扫描器 -->2 ???<context:component-scan base-package="com.jmu.handlers"></context:component-scan>
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 <%@page isELIgnored="false" %> 4 <html> 5 <head> 6 <title>My JSP ‘index.jsp‘ starting page</title> 7 </head> 8 ?9 <body>10 ????<form action="${pageContext.request.contextPath}/test/register.do"11 ????????method="post">12 ????????姓名:<input type="text" name="name"> <br> 13 ????????年龄:<input type="text" name="age"> <br> 14 ????????<input type="submit" value="注册">15 ????</form>16 </body>17 </html>
1 <body>2 ????name=${name}<br>3 ????age=${age}<br>4 ??</body>
二、解决中文乱码问题
在CharacterEncodingFilter中
在web.xml中添加
<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>forceEncoding</param-name> ?????<param-value>true</param-value> ???</init-param> ?</filter>
三、校正请求参数名
四、以对象形式整体接收
1 public class Student { 2 ????private String name; 3 ????private int age; 4 ?5 ????public String getName() { 6 ????????return name; 7 ????} 8 ?9 ????public void setName(String name) {10 ????????this.name = name;11 ????}12 13 ????public int getAge() {14 ????????return age;15 ????}16 17 ????public void setAge(int age) {18 ????????this.age = age;19 ????}20 21 ????@Override22 ????public String toString() {23 ????????return "Student [name=" + name + ", age=" + age + "]";24 ????}25 26 }
1 import org.springframework.stereotype.Controller; 2 import org.springframework.web.bind.annotation.RequestMapping; 3 import org.springframework.web.servlet.ModelAndView; 4 ?5 import com.jmu.beans.Student; 6 ?7 @Controller 8 @RequestMapping("/test") 9 public class MyController {10 11 ????@RequestMapping("/register.do")12 ????public ModelAndView doRegister(Student student) {13 ????????System.out.println("name=" + student.getName());14 ????????System.out.println("age=" + student.getAge());15 16 ????????ModelAndView mv = new ModelAndView();17 ????????mv.addObject("student", student);18 ????????mv.setViewName("/WEB-INF/jsp/welcome.jsp");19 ????????return mv;20 21 ????}22 }
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 ????????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 ????????xmlns:context="http://www.springframework.org/schema/context" 4 ????????xmlns:aop="http://www.springframework.org/schema/aop" 5 ????????xmlns:tx="http://www.springframework.org/schema/tx" 6 ????????xmlns:mvc="http://www.springframework.org/schema/mvc" 7 ????????xsi:schemaLocation="http://www.springframework.org/schema/beans 8 ????????http://www.springframework.org/schema/beans/spring-beans.xsd 9 ????????http://www.springframework.org/schema/context10 ????????http://www.springframework.org/schema/context/spring-context.xsd11 ????????http://www.springframework.org/schema/aop12 ????????http://www.springframework.org/schema/aop/spring-aop.xsd13 ????????http://www.springframework.org/schema/tx14 ????????http://www.springframework.org/schema/tx/spring-tx.xsd15 ????????http://www.springframework.org/schema/mvc16 ????????http://www.springframework.org/schema/mvc/spring-mvc.xsd">17 ?<!-- ??注册组件扫描器 -->18 ???<context:component-scan base-package="com.jmu.handlers"></context:component-scan>19 </beans>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 3 ??<display-name>02-springmvc-Object</display-name> 4 ???<filter> 5 ????<filter-name>CharacterEncodingFilter</filter-name> 6 ????<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 7 ????<init-param> 8 ??????<param-name>encoding</param-name> 9 ??????<param-value>UTF-8</param-value>10 ????</init-param>11 ????<init-param>12 ??????<param-name>forceEncoding</param-name>13 ??????<param-value>true</param-value>14 ????</init-param>15 ??</filter>16 ??<filter-mapping>17 ??<filter-name>CharacterEncodingFilter</filter-name>18 ??<url-pattern>/*</url-pattern>19 ??</filter-mapping>20 ??<servlet>21 ????<servlet-name>springMVC</servlet-name>22 ????<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>23 ????<init-param>24 ??????<param-name>contextConfigLocation</param-name>25 ??????<param-value>classpath:springmvc.xml</param-value>26 ????</init-param>27 ????<load-on-startup>1</load-on-startup>28 ??</servlet>29 ??<servlet-mapping>30 ????<servlet-name>springMVC</servlet-name>31 ????<url-pattern>*.do</url-pattern>32 ??</servlet-mapping>33 ??<welcome-file-list>34 ????<welcome-file>index.jsp</welcome-file>35 ??</welcome-file-list>36 </web-app>
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 ????String path = request.getContextPath(); 4 ????String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() 5 ????????????+ path + "/"; 6 %> 7 <%@page isELIgnored="false"%> 8 ?9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">10 <html>11 <head>12 <base href="<%=basePath%>">13 14 <title>My JSP ‘index.jsp‘ starting page</title>15 </head>16 17 <body>18 ????<form action="${pageContext.request.contextPath }/test/register.do"19 ????????method="post">20 ????????姓名:<input type="text" name="name" /><br> 年 龄:<input type="text"21 ????????????name="age" /><br> <input type="submit" value="注册" />22 ????</form>23 </body>24 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 <%@page isELIgnored="false" %> 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 ??<head>10 ????<base href="<%=basePath%>">11 ????12 ????<title>welcome ?page</title>13 ????14 ??</head>15 ??16 ??<body>17 ???student=${student} <br>18 ??</body>19 </html>
五、域属性参数的接收
域属性参数,即对象属性,当请求参数中的数据为某类对象域属性的属性值时,要求请求参数名为“域属性名.属性”。
1 public class School { 2 ????private String sname; 3 ????private String address; 4 ?5 ????public String getSname() { 6 ????????return sname; 7 ????} 8 ?9 ????public void setSname(String sname) {10 ????????this.sname = sname;11 ????}12 13 ????public String getAddress() {14 ????????return address;15 ????}16 17 ????public void setAddress(String address) {18 ????????this.address = address;19 ????}20 21 ????@Override22 ????public String toString() {23 ????????return "School [sname=" + sname + ", address=" + address + "]";24 ????}25 26 27 28 }
六、路径变量
SpingMVC_注解式开发_接收请求参数
原文地址:https://www.cnblogs.com/hoje/p/8556036.html