Spring-mvc-Handler的返回值用法以及URL映射
2016-07-13 14:56
429 查看
1)返回的是ModelAndView对象?
主要运用于:及传递页面也传递数据
ModelAndView代表的是响应的视图,还有一个向该视图传递的数据。比如:
2)返回的是String类型?
主要运用于:传递页面
a.转发。类似于request.getRequestDispacher(“页面名称”).forward(request,response);
特点:url路径不发生改变,但是request共享。
在handler方法返回的时候:return “forward:/edit.action”;
b.重定向,类似于response.sendRedirect(“页面名称”);
特点:url路径发生改变,但是request不共享。
在handler中返回值:return "redirect:/item.action";
3)返回的是void类型。有的handler的处理是不需要有返回页面的情况,那这个时候就采取返回的类型为void。
适用情况:请求json或者xml的数据格式,或者请求的就是一串字符串数据。
写法:类似于servlet的处理。
2.URL映射
1)同一个handler处理多个路径的情况下:
以上表示的就是可以处理index.action和hello.action的路径。
method表示限定的请求方法,
2)url风格设定?
restful风格设定:
我们的请求路径的风格为这样:
/方法名/参数1/参数2
案例:
注意:{}里的参数名和PathVariable里的value里的名字一定要一致。
浏览器访问路径:http://localhost:10086/ssmdemo2/detail/81.action
3)通配符映射:
• 我们还可以通过通配符对URL映射进行配置,通配符有“?”和“*”两个字符。其中“?”表示1个字符,“*”表示匹配多个字符,“**”表示匹配0个或多个路径。
比如:“/helloworld/index?”可以匹配“/helloworld/indexA”、“/helloworld/indexB”,但不能匹配“/helloworld/index”也不能匹配“/helloworld/indexAA”;
“/helloworld/index*”可以匹配“/helloworld/index”、“/helloworld/indexA”、“/helloworld/indexAA”但不能匹配“/helloworld/index/A”;
“/helloworld/index/*”可以匹配“/helloworld/index/”、“/helloworld/index/A”、“/helloworld/index/AA”、“/helloworld/index/AB”但不能匹配
“/helloworld/index”、“/helloworld/index/A/B”;
“/helloworld/index/**”可以匹配“/helloworld/index/”下的多有子路径,比如:“/helloworld/index/A/B/C/D”;
如果现在有“/helloworld/index”和“/helloworld/*”,如果请求地址为“/helloworld/index”那么将如何匹配?Spring MVC会按照最长匹配优先原则(即和映射配置中哪个匹配的最多)来匹配,所以会匹配“/helloworld/index
4)url正则表达式匹配
Spring MVC还支持正则表达式方式的映射配置,我们通过一
@RequestMapping(value="/reg/{name:\\w+}-{age:\\d+}", method = {RequestMethod.GET})
public ModelAndView regUrlTest(@PathVariable(value="name") String name, @PathVariable(value="age") Integer age){
}
主要运用于:及传递页面也传递数据
ModelAndView代表的是响应的视图,还有一个向该视图传递的数据。比如:
<span style="font-size:18px;"><span style="color:#999999;">@RequestMapping(value="/getalluser.action") public ModelAndView getAllUser(){ ModelAndView model = new ModelAndView(); List<UserInfo> list = userInfoService.getList(null); </span><span style="background-color: rgb(255, 153, 255);"><span style="color:#3333ff;">model.addObject("users", list);//表示向页面传递的数据 model.setViewName("/index.jsp");//表示呈现的页面</span></span><span style="color:#999999;"> return model; }</span></span>
2)返回的是String类型?
主要运用于:传递页面
a.转发。类似于request.getRequestDispacher(“页面名称”).forward(request,response);
特点:url路径不发生改变,但是request共享。
在handler方法返回的时候:return “forward:/edit.action”;
b.重定向,类似于response.sendRedirect(“页面名称”);
特点:url路径发生改变,但是request不共享。
在handler中返回值:return "redirect:/item.action";
3)返回的是void类型。有的handler的处理是不需要有返回页面的情况,那这个时候就采取返回的类型为void。
适用情况:请求json或者xml的数据格式,或者请求的就是一串字符串数据。
写法:类似于servlet的处理。
<span style="color:#999999;">@RequestMapping(value=”/aaa.action”) public void addType(HttpServletRequest req,HttpServletResponse resp){ //JSON处理 List<UserInfo> list = userService.getList(); //序列化的操作 </span><span style="color:#3333ff;"> <span style="background-color: rgb(255, 153, 255);"> JSONArray jsonArray = JSONArray.fromObject(list);</span></span><span style="color:#999999;"> String strInfo = jsonArray.toString(); System.out.println(strInfo); PrintWriter pw = response.getWriter(); pw.print(strInfo); }</span>
2.URL映射
1)同一个handler处理多个路径的情况下:
<span style="font-size:18px;color:#999999;">@RequestMapping(value={"/index", "/hello"}, method = {RequestMethod.GET})</span>
以上表示的就是可以处理index.action和hello.action的路径。
method表示限定的请求方法,
2)url风格设定?
restful风格设定:
我们的请求路径的风格为这样:
/方法名/参数1/参数2
案例:
<span style="color:#999999;">@RequestMapping(value="/detail/</span><span style="background-color: rgb(255, 153, 255);"><span style="color:#3333ff;">{id}</span>< 4000 ;/span><span style="color:#999999;">",method={RequestMethod.GET}) public ModelAndView detail(</span><span style="background-color: rgb(255, 153, 255);"><span style="color:#3333ff;">@PathVariable(value="id")</span></span><span style="color:#999999;"> Integer id){ System.out.println("hello"); ModelAndView model = new ModelAndView(); UserInfo u = userInfoService.getUserInfo(id); model.addObject("user", u); model.setViewName("/user.jsp"); return model; }</span>
注意:{}里的参数名和PathVariable里的value里的名字一定要一致。
浏览器访问路径:http://localhost:10086/ssmdemo2/detail/81.action
3)通配符映射:
• 我们还可以通过通配符对URL映射进行配置,通配符有“?”和“*”两个字符。其中“?”表示1个字符,“*”表示匹配多个字符,“**”表示匹配0个或多个路径。
比如:“/helloworld/index?”可以匹配“/helloworld/indexA”、“/helloworld/indexB”,但不能匹配“/helloworld/index”也不能匹配“/helloworld/indexAA”;
“/helloworld/index*”可以匹配“/helloworld/index”、“/helloworld/indexA”、“/helloworld/indexAA”但不能匹配“/helloworld/index/A”;
“/helloworld/index/*”可以匹配“/helloworld/index/”、“/helloworld/index/A”、“/helloworld/index/AA”、“/helloworld/index/AB”但不能匹配
“/helloworld/index”、“/helloworld/index/A/B”;
“/helloworld/index/**”可以匹配“/helloworld/index/”下的多有子路径,比如:“/helloworld/index/A/B/C/D”;
如果现在有“/helloworld/index”和“/helloworld/*”,如果请求地址为“/helloworld/index”那么将如何匹配?Spring MVC会按照最长匹配优先原则(即和映射配置中哪个匹配的最多)来匹配,所以会匹配“/helloworld/index
4)url正则表达式匹配
Spring MVC还支持正则表达式方式的映射配置,我们通过一
@RequestMapping(value="/reg/{name:\\w+}-{age:\\d+}", method = {RequestMethod.GET})
public ModelAndView regUrlTest(@PathVariable(value="name") String name, @PathVariable(value="age") Integer age){
}
相关文章推荐
- java-用HttpURLConnection发送Http请求.
- Python动态类型的学习---引用的理解
- 我是运营,我没有假期
- 土人系列AS入门教程 -- 对象篇
- DB2数据库的安装
- C#实现把指定数据写入串口
- “传奇”图象数据存储方式
- VBScript 剪贴板抓取URL并在浏览器中打开
- C#托管堆对象实例包含内容分析
- 修复mysql数据库
- C#实现获取不同对象中名称相同属性的方法
- javascript asp教程第十一课--Application 对象
- 浅析SQL数据操作语句
- SQLServer 数据导入导出的几种方法小结
- 简述MySQL分片中快速数据迁移
- MySQL数据备份之mysqldump的使用详解
- PowerShell中使用Out-String命令把对象转换成字符串输出的例子
- C#实现窗体间传递数据实例
- 用vbs 实现从剪贴板中抓取一个 URL 然后在浏览器中打开该 Web 站点
- VBS教程:对象-正则表达式(RegExp)对象