SpringMVC的Handler处理及url映射

1.Handler的理解?

一个handler就是一个控制器里的某个方法,而通常情况下,该方法会对应到相应的url

2.每个Handler的返回值?

1)返回的是ModelAndView对象?

ModelAndView代表的是响应的视图,还有一个向该视图传递的数据。比如:

@RequestMapping(value="/getalluser.action")
	public ModelAndView getAllUser(){
		ModelAndView model = new ModelAndView();
		List<UserInfo> list = userInfoService.getList(null);
		model.addObject("users", list);//表示向页面传递的数据
		model.setViewName("/index.jsp");//表示呈现的页面
		return model;
	}

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";

写法:类似于servlet的处理。

@RequestMapping(value=”/aaa.action”)
public void addType(HttpServletRequest req,HttpServletResponse resp){
        //JSON处理
        List<UserInfo> list = userService.getList();
					//序列化的操作
				   JSONArray jsonArray = JSONArray.fromObject(list);
				   String strInfo = 	jsonArray.toString();
				   System.out.println(strInfo);
				   PrintWriter pw = response.getWriter();
				   pw.print(strInfo);
    }

2.URL映射

1)同一个handler处理多个路径的情况下:

@RequestMapping(value={"/index", "/hello"}, method = {RequestMethod.GET})

以上表示的就是可以处理index.actionhello.action的路径。

method表示限定的请求方法

2url风格设定?

restful风格设定:

我们的请求路径的风格为这样:

   /方法名/参数1/参数2

   案例:

@RequestMapping(value="/detail/{id}",method={RequestMethod.GET})
	public ModelAndView detail(@PathVariable(value="id") 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;
	}

意:{}里的参数名和PathVariable里的value里的名字一定要一致。

访问路径:http://localhost:10086/ssmdemo2/detail/81.action红色为ID

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

4url正则表达式匹配

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













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring MVC中的Handler方法是用来处理HTTP请求的方法。在Spring MVC中,通过注解将一个普通的Java方法标记为一个Handler方法,并将其映射到特定的URL路径和HTTP请求方法。 常用的Handler方法注解有: 1. @RequestMapping:用于将Handler方法映射到指定的URL路径和HTTP请求方法。可以设置URL路径、请求方法、请求头等条件。 2. @GetMapping、@PostMapping、@PutMapping、@DeleteMapping:分别用于将Handler方法映射到GET、POST、PUT、DELETE请求。 3. @PathVariable:用于获取URL路径中的变量值,并将其作为方法参数。 4. @RequestParam:用于获取请求参数的值,并将其作为方法参数。 5. @RequestBody:用于获取请求体中的数据,并将其转换为方法参数的类型。 6. @ResponseBody:用于将方法返回值直接作为响应体返回给客户端。 一个典型的Handler方法示例如下: ```java @Controller @RequestMapping("/users") public class UserController { @GetMapping("/{id}") @ResponseBody public User getUserById(@PathVariable("id") Long id) { // 根据id查询用户信息并返回 } @PostMapping @ResponseBody public User createUser(@RequestBody User user) { // 创建用户并返回 } @PutMapping("/{id}") @ResponseBody public User updateUser(@PathVariable("id") Long id, @RequestBody User user) { // 根据id更新用户信息并返回 } @DeleteMapping("/{id}") @ResponseBody public void deleteUser(@PathVariable("id") Long id) { // 根据id删除用户 } } ``` 以上示例中,`@RequestMapping`注解将`UserController`类映射到"/users"路径下。`@GetMapping`、`@PostMapping`、`@PutMapping`、`@DeleteMapping`注解分别将不同的Handler方法映射到不同的HTTP请求方法。`@PathVariable`注解用于获取URL路径中的变量值,`@RequestBody`注解用于获取请求体中的数据。`@ResponseBody`注解将方法返回值直接作为响应体返回给客户端。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值