接受参数方法
1、@Pathvariable | Method= RequestMethod.Get |
@RequestMapping(value= " /{id}/{str} " )
public ModelAndView helloWorld(@PathVariable String id, @PathVariable String str) {
System.out.println(id);
System.out.println(str);
return new ModelAndView( " /helloWorld " );
}
来自 <http://blog.csdn.net/yaerfeng/article/details/23658391>
2、@ModelAttribute | Method = RequestMethod.Post |
|
Jsp
<formmethod="post" action="hao.do">
a: <input id="a"type="text" name="a"/>
b: <input id="b"type="text" name="b"/>
<input type="submit"value="Submit" />
</form>
Java pojobean
public class Pojo{
private String a;
private int b;
}
JavaController
@RequestMapping(method=RequestMethod.POST)
public String processSubmit(@ModelAttribute(" pojo " ) Pojo pojo) {
return " helloWorld " ;
}
3、HttpServletRequest | Method=RequestMethod.Get |
Java
@RequestMapping(method=RequestMethod.GET)
public String get(HttpServletRequest request,HttpServletResponse response) {
System.out.println(request.getParameter(" a " ));
return " helloWorld " ;
}
4、@RequestParam | Method=RequestMethod.Get |
@RequestMapping(value=" /requestParam " , method= RequestMethod.GET)
public String setupForm(@RequestParam( "a " ) String a, ModelMap model) {
System.out.println(a);
return " helloWorld " ;}
返回页面及结果
PS:以下均可用EL表达式获取
1、String | 页面名称 |
Request | Attribute |
@RequestMapping("/toPerson41.do")
public String toPerson41(HttpServletRequest request) throws Exception {
request.setAttribute("p", newPesion());
return "index";
}
2.1、ModelAndView | 页面名称 |
Request | Map.put |
@RequestMapping("/toPerson42.do")
public ModelAndView toPerson42() throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("p", newPesion());
return new ModelAndView("index", map);
}
2.2、String | 页面名称 |
Request | Map.put |
@RequestMapping("/toPerson43.do")
public String toPerson43(Map<String, Object> map) throws Exception {
map.put("p", newPesion());
return "index";
}
建议使用这个
2.3、String | 页面名称 |
Request | Model.addAtrribute |
@RequestMapping("/toPerson44.do")
public String toPerson44(Model model) throws Exception {
// 把参数值放到request类里面去
model.addAttribute("p", newPesion());
return "index";
}
PS:
参数的作用范围Scope
Request & Response | 在当前请求有效 |
Page | 在当前页面有效 |
Session | 在当前浏览器有效 |
Application | 在当前服务器有效 |
No mapping found for http request with URL 问题
如果遇到这个问题,可以在Spring-mvc.xml中查看以下语句是否正确
<context:component-scanbase-package="cn.bnuz.party.**" />