上一节完成后,运行起来后,应该能看到index.jsp的HelloWorld内容了,在这一节加入SpringMVC支持。
1、首先新建一个config文件,用来配置SpringMVC的拦截器。
src/main/java-org.aurora.config-ApplicationContextServletConfigruation.java
package org.aurora.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.view.InternalResourceViewResolver; @Configuration public class ApplicationContextSevlteConfigruation { @Bean public ViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/views/"); viewResolver.setSuffix(".jsp"); return viewResolver; } }
2、因为用到了spring-webmvc,spring-webmvc又依赖了cglib,所以在pom.xml中加入这两个依赖。
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency>
<dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> </dependency>
3、将拦截器加入到web容器中,方法和web.xml文件的配置一样,只是不用xml文件格式了。修改src/main/java-org.aurora.config-ApplicationInitialize.java,增加一个dispatcher的监听器。这个监听器拦截所有“/”下的资源。
package org.aurora.config; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.ContextLoaderListener; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; public class ApplicationInitialize implements WebApplicationInitializer { public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); servletContext.addListener(new ContextLoaderListener(rootContext)); /** * Spring mvc dispatcher */ ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); } }
4、下面的问题是到哪里去找SpringMVC的拦截器呢,xml时代是有个默认的地方,默认的名称,比如上面的描述,就应该是到WEB-INF下面去找到一个叫dispatcher-servlet.xml的文件,web容器会自动加载进来,当然,也可以自定义路径,我们这里也自定义了,这个类名叫ApplicationContextServletConfigruation,所以,这个容器中还需要加入一行代码,表示到哪里去寻找这个文件。
package org.aurora.config; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.ContextLoaderListener; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; public class ApplicationInitialize implements WebApplicationInitializer { public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.scan("org.aurora.config"); servletContext.addListener(new ContextLoaderListener(rootContext)); /** * Spring mvc dispatcher */ ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); } }
就是这句rootContext.scan("org.aurora.config");就好比去xml中的dispatcher-*.xml一样,这里使用包扫描方式将“org.aurora.config”下面所有有@Configuration注解的类全部扫描一遍。
5、修改一下index.jsp,这个文件以后不用了,让他自动跳转到home.jsp去。
<jsp:forward page="home" />
6、我们看到,这个页面跳转到了一个叫home的地址,这是个虚拟地址,SpringMVC会将这个地址处理成真实的页面,所以,这里还需要建立一个核心类,这和SpringMVC以前的版本没有什么区别。
src/main/java-org.aurora.controller-HomeController.java
package org.aurora.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class HomeController { @RequestMapping(value = "/home", method = RequestMethod.GET) public String login() { return "home"; } }
7、现在构建一个home.jsp页面,因为我们拦截的是"/WEB-INF/views/"下的所有".jsp"文件,所以需要在WEB-INF目录下建立一个views目录存放jsp文件。src/main/webapp/WEB-INF/views/home.jsp
<html> <head> <title>Home Page</title> </head> <body> <h1>Hello World</h1> </body> </html>
8、测试一下,还是访问http://localhost:8080/index.jsp,发现404错误,找不到home,这个问题是因为src/main/java/org/aurora/config/ApplicationContextSevlteConfigruation.java的配置里只是设置了拦截器,具体是拦截什么没有说明,所以这里需要添加一个注解
package org.aurora.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.view.InternalResourceViewResolver; @Configuration @ComponentScan(basePackages = {"org.aurora.controller"}) public class ApplicationContextSevlteConfigruation { @Bean public ViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/views/"); viewResolver.setSuffix(".jsp"); return viewResolver; } }
就是这一句@ComponentScan(basePackages = {"org.aurora.controller"}),指定到org.aurora.controller包里去扫描所有有@controller注解的类
9、再测试一下,应该正常了。
10、为了日后方便,再加入一个注解@EnableWebMvc,这个注解一次性为我们导入了springmvc需要的很多bean。最终代码如下:
package org.aurora.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.view.InternalResourceViewResolver; @Configuration @EnableWebMvc @ComponentScan(basePackages = {"org.aurora.controller"}) public class ApplicationContextSevlteConfigruation { @Bean public ViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/views/"); viewResolver.setSuffix(".jsp"); return viewResolver; } }