SpringMVC传参方式

来源:互联网 发布:安能物流鲁班系统mac 编辑:程序博客网 时间:2024/06/01 10:12

SpringMVC传参方式

方式一:使用servlet原生的方式,通过request.getParameter("key")获取参数;

<fieldset><legend>用户注册1</legend><form action="/value1" method="post">用户名:<input type="text" name="username"/><br/>密     码:<input type="password" name="password"/><br/><input type="submit" value="提交"/></form></fieldset>
@RequestMapping("/value1")ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception{String username=request.getParameter("username");String password=request.getParameter("password");User u=new User(username, password);System.out.println(u);return null;}

方式二:在形参列表中直接写前台需要注入的参数,基于同名规则进行注入的;

<fieldset><legend>用户注册2</legend><form action="/value2" method="post">用户名:<input type="text" name="username"/><br/>密     码:<input type="password" name="password"/><br/><input type="submit" value="提交"/></form></fieldset>
@RequestMapping("/value2")ModelAndView value2(String username, String password) throws Exception{User u=new User(username, password);System.out.println(u);return null;}

如果前台名字和后台形参名字不一致的情况,使用@RequestParam("前台指定名称")注解来进行处理:

<fieldset><legend>用户注册3</legend><form action="/value3" method="post">用户名:<input type="text" name="username1"/><br/>密     码:<input type="password" name="password"/><br/><input type="submit" value="提交"/></form></fieldset>
@RequestMapping("/value3")ModelAndView value3(@RequestParam("username1")String username, Stringpassword) throws Exception{User u=new User(username, password);System.out.println(u);return null;}

方式三:使用模型传参数的方式(采用属性注入形式);

<fieldset><legend>用户注册4</legend><form action="/value4" method="post">用户名:<input type="text" name="username"/><br/>密     码:<input type="password" name="password"/><br/><input type="submit" value="提交"/></form></fieldset>

@RequestMapping("/value4")ModelAndView value4(User u) throws Exception{System.out.println(u);return null;}

方式四:使用地址栏传参方式:需要使用注解@PathVariable("XXX"),前台请求地址为:http://localhost:8080/value5/6。此时地址栏的6会作为参数注入id字段中

@RequestMapping("/value5/{abc}")ModelAndView value5(@PathVariable("abc")String name) throws Exception{System.out.println(name);return null;}

相关源码:

1、User类

packagecom.lixing.springmvc.hello;public class User {     private Stringusername;     private Stringpassword;     public StringgetUsername() {         returnusername;     }     public voidsetUsername(String username) {         this.username =username;     }     public StringgetPassword() {         returnpassword;     }     public voidsetPassword(String password) {         this.password =password;     }     public User(Stringusername, Stringpassword) {         super();         this.username =username;         this.password =password;     }     @Override     public StringtoString() {         return"User[username=" +username + ",password=" +password + "]";     }     public User() {         super();     }}

2、ValueController类

packagecom.lixing.springmvc.hello;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;import org.apache.tomcat.util.buf.UEncoder;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.PathVariable;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.servlet.ModelAndView;@Controllerpublic classValueController {    //方式一:使用servlet原生的方式,通过request.getParameter("key")获取参数;    @RequestMapping("/value1")    ModelAndViewhandleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception{        String username=request.getParameter("username");        String password=request.getParameter("password");        User u=new User(username,password);        System.out.println(u);        return null;    }    //方式二:在形参列表中直接写前台需要注入的参数,基于同名规则进行注入的    @RequestMapping("/value2")    ModelAndViewvalue2(String username, String  password)throws Exception{        User u=new User(username,password);        System.out.println(u);        return null;    }//如果前台名字和后台形参名字不一致的情况,使用@RequestParam("前台指定名称")注解来进行处理    @RequestMapping("/value3")    ModelAndViewvalue3(@RequestParam("username1")Stringusername, String  password) throws Exception{        User u=new User(username,password);        System.out.println(u);        return null;    }    //方式三:使用模型传参数的方式    @RequestMapping("/value4")    ModelAndViewvalue4(User u)throws Exception{        System.out.println(u);        return null;    }    //方式四:使用地址栏传参方式:需要使用注解@PathVariable("XXX")    //前台请求地址为:http://localhost:8080/value5/6    //此时地址栏的6会作为参数注入id字段中    @RequestMapping("/value5/{abc}")    ModelAndViewvalue5(@PathVariable("abc")Stringname)throws Exception{        System.out.println(name);        return null;    }}

3、input.jsp

<%@ page language="java"contentType="text/html;charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><metahttp-equiv="Content-Type"content="text/html; charset=UTF-8"><title>Insert titlehere</title></head><body><fieldset>      <legend>用户注册1</legend>      <formaction="/value1"method="post">           用户名:<inputtype="text"name="username"/><br/>           密     码:<inputtype="password"name="password"/><br/>           <inputtype="submit"value="提交"/>      </form></fieldset><hr/><fieldset>      <legend>用户注册2</legend>      <formaction="/value2"method="post">           用户名:<inputtype="text"name="username"/><br/>           密     码:<inputtype="password"name="password"/><br/>           <inputtype="submit"value="提交"/>      </form></fieldset><hr/><fieldset>      <legend>用户注册3</legend>      <formaction="/value3"method="post">           用户名:<inputtype="text"name="username1"/><br/>           密     码:<inputtype="password"name="password"/><br/>           <inputtype="submit"value="提交"/>      </form></fieldset><hr/><fieldset>      <legend>用户注册4</legend>      <formaction="/value4"method="post">           用户名:<inputtype="text"name="username"/><br/>           密     码:<inputtype="password"name="password"/><br/>           <inputtype="submit"value="提交"/>      </form></fieldset></body></html>

4、applicationContext.xml

<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"      xmlns:contex="http://www.springframework.org/schema/context"      xmlns:mvc="http://www.springframework.org/schema/mvc"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xsi:schemaLocation="      http://www.springframework.org/schema/beans      http://www.springframework.org/schema/beans/spring-beans.xsd      http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context.xsd      http://www.springframework.org/schema/mvc      http://www.springframework.org/schema/mvc/spring-mvc.xsd  ">      <!--<bean name="/hello"class="com.lixing.springmvc.hello.HelloWordConctroller"/>      <beanclass="com.lixing.springmvc.hello.AnnotationConctroller"/> -->      <!-- 开启注解扫描 -->      <contex:component-scanbase-package="com.lixing.springmvc.hello"/>      <!--springMVC注解驱动支持,能够做很多事情,比如JSON解析  -->      <mvc:annotation-driven/></beans>

不当之处,敬请批评指正!

原创粉丝点击