springmvc中的参数接收
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; /*
*插一段~:中文参数乱码,通过在web.xml中配置,spring-web包中的CharacterEncodingFilter过滤器即可解决,,
<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> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
*/
@Controller public class Params { public Params() { System.out.println("costructor...."); } /* * 方式1:直接将方法的形参名作为参数名进行接收,,注此时形参名要和参数名相同 */ @RequestMapping("/param1") public ModelAndView param(String name, String address) { System.out.println(name + "::" + address); return null; } /* * 方式2:直接将方法的形参名作为参数名进行接收, * 同时在形参名和参数名不同的形参前面添加@@RequestParam("name"),标明该形参用于接收哪一个参数的值 ,注此时形参名要和参数名相同 */ @RequestMapping("/param2") public ModelAndView param2(@RequestParam("name") String username, String address) { System.out.println(username + "::" + address); return null; } /* * 方式3:在方法中注入servlet中的对象,servlet中的全部自带的对象均可以由spring自动注入, * 本方法中通过注入HttpServletRequest和HttpServletResponse实现参数的获取 */ @RequestMapping("/param3") public ModelAndView param3(HttpServletRequest req, HttpServletResponse resp) { String username = req.getParameter("name"); String address = req.getParameter("address"); System.out.println(username + "--" + address); return null; } /* * 方式4:通过实现创建的模型,在该模型中含有与参数名相同的属性名, spring会自动将参数名和属性名相同的参数的参数值注入到模型的对应属性上 * 对于模型中没有属性名与参数名相同的参数,可以在方法中在写一个形参用于接收该参数的值 如本方法中的confirm */ @RequestMapping("/param4") public ModelAndView param4(User u, String confirm) { System.out.println(u.name + "--" + u.address + "--" + confirm); return null; } /* * 方式5:直接从url中抽取参数的值, * 通过使用占位符{},在{}中写入参数名,在方法形参类表中的每个形参前使用@pathVeriable("展位符中的参数名")标记
*此时,,,直接在地址栏填入http://localhost:8080/param5/34/sds.do ,该方法即可获取此url中的2个参数,此方法也称之为"restful"模式 */ @RequestMapping("/param5/{id}/{name}") public ModelAndView param5(@PathVariable("id") Long id, @PathVariable("name") String name) { System.out.println(id + "--" + name); return null; } }
下面为表单,和模型user
/** *USER模型 */ public class User { String name; String address; @Override public String toString() { return "User [name=" + name + ", address=" + address + "]"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="/param5.do" method="post"> 姓名:<input type="text" name="name" /><br /> 地址:<input type="text" name="address" /><br /> 确认:<input type="text" name="confirm" /><br /> <input type="submit" value="提交" /> </form> </body> </html>