web.xml
添加:
<servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
spring mvc 默认会去WEB-INF 下寻找 %servlet-name%-servlet.xml这个配置文件, 上例中该文件就为mvc-dispatcher-servlet.xml
如果想手动指定则在添加init-param即可,例如:
<servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/mvc/*.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
mvc-dispatcher-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <!--@Controller, @Service, @Configuration, etc.--> <context:component-scan base-package="com.spring.mvc.controller" /> <!--prefix:指定view的保存路径,suffix指定文件类型--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/pages/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans>
Controller:
@Controller @RequestMapping("/hello") public class HelloController { private static Logger log = Logger.getLogger(HelloController.class); @RequestMapping(method = RequestMethod.GET) public String printWelcome(ModelMap model) { log.info("HelloController.printWelcome"); model.addAttribute("message", "Spring 3 MVC Hello World"); return "hello"; } }
其中 RequestMapping指定这个Controller的根URL, 每个方法上指定访问该方法的方式, 如果有子路径的话也可指定子路径的value。这种方式能很好的实现RESTful的URL
例如:
@Controller @RequestMapping("/movie") public class MovieController { private static Logger log = Logger.getLogger(MovieController.class); @RequestMapping(value = "/", method = RequestMethod.GET) public String getMovieList(ModelMap model) { log.info("MovieController.getMovieList"); model.addAttribute("hi", "this is movie list view"); return "list"; } @RequestMapping(value = "/{category}", method = RequestMethod.GET) public String getMovieCat(@PathVariable String category, ModelMap model) { log.info("MovieController.getMovieCat"); model.addAttribute("category", "select move category:"+category); return "list"; } @RequestMapping(value = "/{category}/{name}", method = RequestMethod.GET) public String getMovie(@PathVariable String name, ModelMap model) { log.info("MovieController.getMovie"); model.addAttribute("movie", name); return "list"; } }
该例子的访问路径可以是这样的:http://localhost:8080/SpringMVCTest/movie/act/Ironman, URL层级直接的关系很清楚。
如果不指定上述的InternalResourceViewResolver的话,那么Controller中返回view需要指定路径,例如:
@Controller public class HomeController { private static Logger log = Logger.getLogger(HomeController.class); @RequestMapping(value = "/home") public String home() { log.info("HomeController: Passing through..."); return "WEB-INF/views/home.jsp"; } }