Spring3 MVC请求参数获取的几种场景
访问/aaa/bbb所对应的@Controller
@RequestMapping("/aaa")//类级别,可以不需要,如果要了,下面所有的请求路径前都需要加入/aaa
public class DefaultController{
@RequestMapping("/bbb")
//方法级别,必须有,决定这个方法处理哪个请求,如果有类级别 /aaa/bbb
public String xxx()
{
//如果没有类级别的就直接请求/bbb
return;
}
}
一、通过@PathVariabl获取路径中的参数
eg1:
例如,访问user/123/chan路径时,执行以上方法,其中,参数id=123,name=chan
@Controller使用以下代码来接收参数
@RequestMapping(value="user/{id}/{name}",method=RequestMethod.GET)
public String printMessage1(@PathVariable String id,@PathVariable String name, ModelMap model) {
System.out.println(id);
System.out.println(name);
model.addAttribute("message", "111111");
return "users";
}
eg2:
@RequestMapping("/somepath/{userName}")
public String register(@PathVariable(value = "userName") String userName) {
ModelAndView mav = new ModelAndView();
return "user/createSuccess";
}
在springmvc注解的@RequestMapping("/somepath/{userName}")如何处理默认值的情况,比如我在发送请求的时候,userName有时候是没有的,所以导到了不能匹配这个action
http://localhost:8080/spc/movie/somepath/就不能匹配上面的请求:
http://localhost:8080/spc/movie/somepath/tom而这个才可以匹配。
@PathVariable 会将url中的参数解析到对应的方法参数上,需要在@RequestMapping()指定匹配模式
@RequestMapping("somepath/{userName}")
这时你访问地址"somepath/Tom"就能把"Tom"解析到方法参数userName上
http://blog.sina.com.cn/s/blog_beb1f2ec01015pz4.html
@RequestMapping("/user")或@RequestMapping(value="/user") 对路径进行映射
@RequestMapping(params="user") 对参数进行映射
@RequestParam(value="user") 对参数进行映射
二、用注解@RequestParam绑定请求参数
例如,访问user?name=chan路径时
@Controller如下
@RequestMapping(value = "/user", method = RequestMethod.GET)
public String setupForm(@RequestParam("name") String name, ModelMap model) {
System.out.println(name);
return "helloWorld";
}
用注解@RequestParam绑定请求参数name到变量name
当请求参数a不存在时会有异常发生,可以通过设置属性required=false解决,
例如: @RequestParam(value="name", required=false)
请求方法映射
或 的关系:method={RequestMethod.POST, RequestMethod.GET};
如果需要支持OPTIONS、TRACE,请添加DispatcherServlet在web.xml的初始化参数:dispatchOptionsRequest 和 dispatchTraceRequest 为true。
请求参数映射
@RequestMapping(params="create") 有create参数
@RequestMapping(params!="create") 没有create参数
@RequestMapping(params="create=sfp") create=sfp
@RequestMapping(params="create!=sfp") create!=sfp
且 的关系 @RequestMapping(params={"test1", "test2=create"})
http://www.cnblogs.com/wang-jing/p/4567214.html
三、@ModelAttribute获取POST请求的FORM表单数据
提交以下表单
<form method="post" action="user">
a: <input id="a" type="text" name="a"/>
b: <input id="b" type="text" name="b"/>
<input type="submit" value="Submit" />
</form>
Java Pojo如下
public class Pojo{
private String a;
private int b;
}
Java @Controller如下
@RequestMapping(value = "/user",method = RequestMethod.POST)
public String processSubmit(@ModelAttribute("pojo") Pojo pojo) {
return "helloWorld";
}
三、直接用HttpServletRequest获取
例如,访问user?name=chan路径时
@Controller如下
@RequestMapping(value = "/user",method = RequestMethod.GET)
public String get(HttpServletRequest request, HttpServletResponse response) {
System.out.println(request.getParameter("name"));
return "helloWorld";
}
http://www.cnblogs.com/leiOOlei/p/3658147.html
Spring3 MVC请求参数获取的几种场景的更多相关文章
- Spring3 MVC请求参数获取的几种方法
Spring3 MVC请求参数获取的几种方法 一. 通过@PathVariabl获取路径中的参数 @RequestMapping(value="user/{id}/{name}&q ...
- Spring3 MVC请求参数获取的几种方法[转]
Spring3 MVC请求参数获取的几种方法 Spring3 MVC请求参数获取的几种方法 一. 通过@PathVariabl获取路径中的参数 @RequestMapping(value=& ...
- Spring3 MVC请求参数获取的几种方法[转载]
http://www.cnblogs.com/leiOOlei/p/3658147.html 一. 通过@PathVariabl获取路径中的参数 @RequestMapping(value= ...
- Spring MVC 的请求参数获取的几种方法
通过@PathVariabl注解获取路径中传递参数 @RequestMapping(value = "/{id}/{str}") public ModelAndView hello ...
- springmvc请求参数获取的几种方法
1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交. /** * 1.直接把表单的参数写在Controller相应的方法的形参中 * @pa ...
- jmeter脚本中请求参数获取的几种方式
a.从数据库获取: 譬如接口请求参数中id的值,我需要从数据库获取,如下设置: 先设置jdbc connection configuration,然后设置JDBC b.从CSV获取: 获取CSV文件 ...
- Spring MVC请求参数绑定 自定义类型转化 和获取原声带额servlet request response信息
首先还在我们的框架的基础上建立文件 在domian下建立Account实体类 import org.springframework.stereotype.Controller; import org. ...
- springmvc请求参数获取(自动绑定)的几种方法
1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交. /** * 1.直接把表单的参数写在Controller相应的方法的形参中 * @pa ...
- SpringBoot 基于web应用开发(请求参数获取,静态资源,webjars)
SpringBoot 基于web应用开发 一.Lombok使用 1.导入依赖库 <dependency> <groupId>org.projectlombok</g ...
随机推荐
- 初始化openwrt的rootpassword
更改openwrt源代码 shadow 文件 package/base-files/files/etc/shadow shadow 文件參考http://blog.csdn.net/u01164188 ...
- 使用ILmerge合并Exe、Dll文件的帮助类
原文:使用ILmerge合并Exe.Dll文件的帮助类 using System; using System.Collections.Generic; using System.Text; using ...
- hdu4908(中位数)
传送门:BestCoder Sequence 题意:给一个序列,里面是1-N的排列,给出m,问以m为中位数的奇数长度的序列个数. 分析:先找出m的位置,再记录左边比m大的状态,记录右边比m大的状态,使 ...
- 【Bug笔记】The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
因为今天下载了一个eclipse se的版本号.所以想把原本eclipse ee这个软件外面的目录eclipse名字该成eclipse ee,方便以后的区分和管理.改了后又一次打开eclipse ee ...
- Python用Tkinter的Frame实现眼睛护士的倒计时黑色屏幕
import Tkinter,time class MyFrame(Tkinter.Frame): def __init__(self): Tkinter.Frame.__init__(self) s ...
- FUDCon - FedoraProject
FUDCon - FedoraProject FUDCon: Fedora Users and Developers Conference FUD: An acronym for Fear, Unce ...
- 求1e11以内的素数
有两种做法,一种是打表,另一种是直接求. 打表 将1e11每隔len(len=2000w)个数字统计一下该区间内素数的个数,比如cnt[1] 表示[1,len]以内有多少个素数,cnt[2]表示[le ...
- 在静态方法里调用spring注入的方法
在静态方法里直接调用非静态方法是不行的. 那如何调用spring注入的方法呢? @Component public class AutoLoginUtil { @Autowired p ...
- Instruments的使用 逻辑查错,内存泄漏分析等工具集
原创文章,转载请注明 XCode 开发后期,要对代码进行改进和优化,查内存泄漏是其中一项重要工作,今天下午偷了点时间,把前段时间的代码稍微整理了一下,顺带用了下这个工具,还真发现了些问题.这里记录一下 ...
- 用XAML做网页!!—终结篇
原文:用XAML做网页!!-终结篇 迄今为止的设计都很顺利,但这次就不得不接触我前面所说的非常糟糕的流文档了,但在此之前先来把标题弄好: <Border BorderBrush="#6 ...