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>
不当之处,敬请批评指正!
阅读全文
0 0
- SpringMVC传参方式
- SpringMVC集中传参方式
- Springmvc:前后端传参方式总结
- ajax与springmvc传参方式总结
- springMvc 传值方式
- springMVC传值方式
- SpringMvc传值方式
- springmvc传值方式
- SpringMVC的各种前后台参数绑定传参方式
- SpringMVC传参,接参,集中传参,jsp页面提参的方式,以及关于SpringMVC下载和异常方式
- 第二节:springmvc传值方式
- SpringMvc ajax json 传值方式
- SpringMVC前后端传值方式
- SpringMVC—注解方式
- SpringMVC注解方式
- springmvc注解方式访问
- SpringMVC返回数据方式
- SpringMVC参数传递方式
- testng+maven+java+idea 接口测试入门四:查看测试报告并将测试结果发送邮件
- metasploit维持访问
- 实现对象深拷贝的简单案例
- 【JZOJ 5451】【NOIP2017提高A组冲刺11.4】Genocide
- 编码之json
- SpringMVC传参方式
- 八方向移动限制想法
- Linux中的hosts文件
- 倔强青铜编程日记<2>:windows visual studio 下opencv以及opencv-contrib、samples的安装
- js基本操作3
- tf.layers.conv2d和tf.nn.conv2d使用区别
- Python 如何根据字符串创建列表
- PyQt5学习教程18:70行的货币转换程序
- 《EffectiveC++》读书笔记(三)--- 尽量延后变量的定义