您的位置:首页 > 编程语言 > Java开发

Spring-mvc-Handler的返回值用法以及URL映射

2016-07-13 14:56 429 查看
1)返回的是ModelAndView对象?

主要运用于:及传递页面也传递数据



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){

   } 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring mvc url 对象 数据