springMVC笔记(二)- SimpleUrlHandlerMapping

SpringMVC里面有许多HandlerMapping,对这块还不太了解,所以决定了解一下这些东东的作用。

先来看第一个:org.springframework.web.servlet.handler.SimpleUrlHandlerMapping

官方的api文档解释如下:

Implementation of the org.springframework.web.servlet.HandlerMapping interface to map from URLs to request handler beans.

简单来说,就是实现了HandlerMapping接口,处理url到bean的映射,比如下面这些映射:

/welcome.html=ticketController

/show.html=ticketController

一个url对应一个bean,但是这个bean有个要求,就是实现Controller接口,下面看一个实例。

1.Controller如下:

 1 package com.springmvc.demo.controller;

 2 

 3 import javax.servlet.http.HttpServletRequest;

 4 import javax.servlet.http.HttpServletResponse;

 5 

 6 import org.springframework.stereotype.Controller;

 7 import org.springframework.web.servlet.ModelAndView;

 8 import org.springframework.web.servlet.mvc.AbstractController;

 9 

10 @Controller

11 public class SimpleController extends AbstractController {

12 

13     @Override

14     protected ModelAndView handleRequestInternal(HttpServletRequest request,

15             HttpServletResponse response) throws Exception {

16         System.out.println("SimpleController...");

17         System.out.println(request.getParameter("username"));

18         ModelAndView mv = new ModelAndView("simpleController");

19         mv.addObject("msg", "hello,SimpleController");

20         return mv;

21     }

22 

23 }

2.为了对比,再建一个Controller,这个没有实现Controller接口,而是使用注解:

 1 package com.springmvc.demo.controller;

 2 

 3 import javax.servlet.http.HttpSession;

 4 

 5 import org.springframework.stereotype.Controller;

 6 import org.springframework.ui.ModelMap;

 7 import org.springframework.validation.BindingResult;

 8 import org.springframework.web.bind.annotation.ModelAttribute;

 9 import org.springframework.web.bind.annotation.RequestMapping;

10 import org.springframework.web.bind.annotation.RequestParam;

11 import org.springframework.web.servlet.ModelAndView;

12 

13 import com.springmvc.demo.model.User;

14 

15 @Controller

16 @RequestMapping("/user")

17 public class UserController {

18     

19     @RequestMapping("/add")

20     public ModelAndView addUser(@RequestParam String username,

21             @RequestParam String password,

22             @RequestParam int id,

23             HttpSession session){

24         

25         ModelAndView mv = new ModelAndView();

26         mv.setViewName("hello");

27         User u = new User();

28         u.setUsername(username);

29         u.setPassword(password);

30         u.setId(id);

31         mv.addObject("user", u);

32         session.setAttribute("user", u);

33         

34         return mv;

35     }

36     

37     /*@ModelAttribute

38     public User getUser(){

39         User u = new User();

40         u.setId(12);

41         return u;

42     }*/

43     

44     @RequestMapping("/preAddUser")

45     public String preAddUser(ModelMap map){

46         User u = new User();

47         map.addAttribute("user", u);

48         return "index";

49     }

50     

51     @RequestMapping("/addUser")

52     public String addUser2(@ModelAttribute("user") User user,BindingResult result,ModelMap map){

53         if(user.getUsername().equals("yang")){

54             map.addAttribute("user", user);

55             return "index";

56         }

57         /*ModelAndView mv = new ModelAndView();

58         

59         mv.setViewName("hello");

60         mv.addObject("user", user);*/

61         

62         return "hello";

63     }

64     

65 }

3.页面simpleController.jsp:

 1 <!DOCTYPE html>

 2 <html>

 3 <body>

 4 

 5 <form action="/springmvc/simpleController" method="post">

 6     <input type="text" name="username"/>

 7     <input type="text" name="password"/>

 8     <!-- <input type="text" name="id"/> -->

 9     <input type="submit" value="submit" />

10 </form>

11 </body>

12 </html>

4.MVC配置文件:

 

 1 <?xml version="1.0" encoding="UTF-8"?>

 2 <beans xmlns="http://www.springframework.org/schema/beans"

 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

 4     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"

 5     xsi:schemaLocation="

 6         http://www.springframework.org/schema/beans

 7         http://www.springframework.org/schema/beans/spring-beans.xsd

 8         http://www.springframework.org/schema/context

 9         http://www.springframework.org/schema/context/spring-context.xsd

10         http://www.springframework.org/schema/mvc

11         http://www.springframework.org/schema/mvc/spring-mvc.xsd">

12 

13     <context:component-scan base-package="com.springmvc.demo.controller" />

14     

15     <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />

16     <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" /> -->

17     <!-- <mvc:default-servlet-handler/> -->

18     <!-- <mvc:annotation-driven /> -->

19     <!-- <mvc:resources location="/resource/images/" mapping="/images/**" /> -->

20     <bean id="simpleController" class="com.springmvc.demo.controller.SimpleController"></bean>

21     

22     <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

23         

24         <property name="urlMap">

25             <map>

26                 <entry key="/simpleController" value-ref="simpleController" />

27             </map>

28         </property>

29     </bean>

30     

31     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

32         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />

33         <property name="prefix" value="/" />

34         <property name="suffix" value=".jsp" />

35     </bean>

36     

37     

38 </beans>

 

测试一:

访问simpleController.jsp,点击提交,访问正常

测试二:

访问/user/add,显示404

总结:

SimpleUrlHandlerMapping只支持实现了Controller接口的bean,且还需要在其属性里进行url路径-->bean的映射,不支持@RequestMapping注解,所以这个SimpleUrlHandlerMapping功能很有限

注:还可以使用mappings属性进行配置,效果是一样的:

<!-- mappings是一Properties对象 -->
<
property name="mappings"> <props> <prop key="/simpleController">simpleController</prop> </props> </property>

  

你可能感兴趣的:(springMVC)