1.spring mvc url参数怎么取
springmvc请求参数获取的几种方法1、直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交。
/*** 1.直接把表单的参数写在Controller相应的方法的形参中* @param username* @param password* @return*/@RequestMapping("/addUser1")public String addUser1(String username,String password) {System.out.println("username is:"+username);System.out.println("password is:"+password);return "demo/index";}url形式:/SSMDemo/demo/addUser1?username=lixiaoxi&password=111111 提交的参数需要和Controller方法中的入参名称一致。2、通过HttpServletRequest接收,post方式和get方式都可以。
/*** 2、通过HttpServletRequest接收* @param request* @return*/@RequestMapping("/addUser2")public String addUser2(HttpServletRequest request) {String username=request.getParameter("username");String password=request.getParameter("password");System.out.println("username is:"+username);System.out.println("password is:"+password);return "demo/index";}3、通过一个bean来接收,post方式和get方式都可以。(1)建立一个和表单中参数对应的beanpackage demo.model;public class UserModel {private String username;private String password;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;}}(2)用这个bean来封装接收的参数/*** 3、通过一个bean来接收* @param user* @return*/@RequestMapping("/addUser3")public String addUser3(UserModel user) {System.out.println("username is:"+user.getUsername());System.out.println("password is:"+user.getPassword());return "demo/index";}4、通过@PathVariable获取路径中的参数/*** 4、通过@PathVariable获取路径中的参数* @param username* @param password* @return*/@RequestMapping(value="/addUser4/{username}/{password}",method=RequestMethod.GET)public String addUser4(@PathVariable String username,@PathVariable String password) {System.out.println("username is:"+username);System.out.println("password is:"+password);return "demo/index";}例如,访问/SSMDemo/demo/addUser4/lixiaoxi/111111 路径时,则自动将URL中模板变量{username}和{password}绑定到通过@PathVariable注解的同名参数上,即入参后username=lixiaoxi、password=111111。
5、使用@ModelAttribute注解获取POST请求的FORM表单数据Jsp表单如下: Java Controller如下:/*** 5、使用@ModelAttribute注解获取POST请求的FORM表单数据* @param user* @return*/@RequestMapping(value="/addUser5",method=RequestMethod.POST)public String addUser5(@ModelAttribute("user") UserModel user) {System.out.println("username is:"+user.getUsername());System.out.println("password is:"+user.getPassword());return "demo/index";}6、用注解@RequestParam绑定请求参数到方法入参当请求参数username不存在时会有异常发生,可以通过设置属性required=false解决,例如: @RequestParam(value="username", required=false)/*** 6、用注解@RequestParam绑定请求参数到方法入参* @param username* @param password* @return*/@RequestMapping(value="/addUser6",method=RequestMethod.GET)public String addUser6(@RequestParam("username") String username,@RequestParam("password") String password) {System.out.println("username is:"+username);System.out.println("password is:"+password);return "demo/index";}。
2.SpringMVC获取参数的几种方式
获取页面参数的几种方式1、直接把表单的参数写在Controller相应的方法的形参中 案例:/*** 1.直接把表单的参数写在Controller相应的方法的形参中*/ @RequestMapping("/add") public String add(String username,String password) { System.out.println("username is:"+username); System.out.println("password is:"+password); return "index"; }2、通过HttpServletRequest接收 案例:/*** 2、通过HttpServletRequest接收*/ @RequestMapping("/add") public String add(HttpServletRequest request) { String username=request.getParameter("username"); String password=request.getParameter("password"); System.out.println("username is:"+username); System.out.println("password is:"+password); return "index"; }3、通过一个bean来接收 案例:public class User { private Long id; private String username; private String password; public User(){} public Long getId() { return id; } public String getUsername() { return username; } public String getPassword() { return password; } public void setId(Long id) { this.id = id; } public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } }/*** 3、通过一个bean来接收*/ @RequestMapping("/add") public String add(User user) { System.out.println("username is:"+user.getUsername()); System.out.println("password is:"+user.getPassword()); return "index"; }4、通过@PathVariable获取路径中的参数 案例:/*** 4、通过@PathVariable获取路径中的参数* @param username* @param password* @return*/ @RequestMapping(value="/add/{username}/{password}",method=RequestMethod.GET) public String addUser4(@PathVariable String username,@PathVariable String password) { System.out.println("username is:"+username); System.out.println("password is:"+password); return "index"; }。
3.在springmvc 参数中,什么都可以写么
1. jar包引入
Spring 2.5.6:spring.jar、spring-webmvc.jar、commons-logging.jar、cglib-nodep-2.1_3.jar
Hibernate 3.6.8:hibernate3.jar、hibernate-jpa-2.0-api-1.0.1.Final.jar、antlr-2.7.6.jar、commons-collections-3.1、dom4j-1.6.1.jar、javassist-3.12.0.GA.jar、jta-1.1.jar、slf4j-api-1.6.1.jar、slf4j-nop-1.6.4.jar、相应数据库的驱动jar包
SpringMVC是一个基于DispatcherServlet的MVC框架,每一个请求最先访问的都是DispatcherServlet,DispatcherServlet负责转发每一个Request请求给相应的Handler,Handler处理以后再返回相应的视图(View)和模型(Model),返回的视图和模型都可以不指定,即可以只返回Model或只返回View或都不返回。
DispatcherServlet是继承自HttpServlet的,既然SpringMVC是基于DispatcherServlet的,那么我们先来配置一下DispatcherServlet,好让它能够管理我们希望它管理的内容。HttpServlet是在web.xml文件中声明的。
4.JSP页面 里a超链接如何带参数可以在后台controller方法参数里直接接
jsp页面的请求地址后带上参数
href="xxxx?key1=1&key2=2"在controller里不用注解可以这么写
public ModelAndView handleRequest(HttpServletRequest req) throws Exception {
ModelAndView mv = new ModelAndView();
mv.addObject("key1", req.getParameter("key1"));//req.getParameter("key1")是取得key1的值
mv.addObject("key2", req.getParameter("key2"));
mv.setViewName("login/login.jsp");
return mv;
}用注解可以这么写
public ModelAndView handleRequest(@RequestParam("key1") String key1,@RequestParam String key2) throws Exception {
ModelAndView mv = new ModelAndView();
mv.addObject("key1", key1);
mv.addObject("key2", key2);
mv.setViewName("login/login.jsp");
return mv;
}
5.springmvc怎么传递参数
1,使用HttpServletRequest获取
Java代码
@RequestMapping("/login.do")
public String login(HttpServletRequest request){
String name = request.getParameter("name")
String pass = request.getParameter("pass")
}
2,Spring会自动将表单参数注入到方法参数,和表单的name属性保持一致。和Struts2一样
Java代码
@RequestMapping("/login.do")
public String login(HttpServletRequest request,
String name,
@RequestParam("pass")String password) // 表单属性是pass,用变量password接收
{
syso(name);
syso(password)
}
3,自动注入Bean属性
Java代码
6.springmvc requestbody 怎么实现的
简介:
@RequestBody
作用:
i) 该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对象上;
ii) 再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上。
使用时机:
A) GET、POST方式提时, 根据request header Content-Type的值来判断:
application/x-www-form-urlencoded, 可选(即非必须,因为这种情况的数据@RequestParam, @ModelAttribute也可以处理,当然@RequestBody也能处理);
multipart/form-data, 不能处理(即使用@RequestBody不能处理这种格式的数据);
其他格式, 必须(其他格式包括application/json, application/xml等。这些格式的数据,必须使用@RequestBody来处理);
B) PUT方式提交时, 根据request header Content-Type的值来判断:
application/x-www-form-urlencoded, 必须;
multipart/form-data, 不能处理;
其他格式, 必须;
说明:request的body部分的数据编码格式由header部分的Content-Type指定;
@ResponseBody
作用:
该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。
使用时机:
返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;
转载请注明出处育才学习网 » springmvchref参数怎么写