Spring4 In Action-5.2.2-Spring Web应用程序-简单的控制器实现跳转
// 源码下载地址:http://download.csdn.net/download/poiuy1991719/10111794
首先创建启动配置的类:
package spittr.config; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; import spittr.web.WebConfig; /*** * 容器会找到AbstractAnnotationConfigDispatcherServletInitializer这个类(包括这个类的扩展) * 然后用这个类来配置Servlet上下文 */ public class SpitterWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() {//非Web组件,带有@Configuration注解的类用来定义应用上下文 return new Class<?>[] { RootConfig.class }; } @Override protected Class<?>[] getServletConfigClasses() {//指定配置类,带有@Configuration注解的类用来定义应用上下文 return new Class<?>[] { WebConfig.class }; } @Override protected String[] getServletMappings() {// 将DispatcherServlet映射到"/" return new String[] { "/" }; } }
然后编写WebConfig配置类,代替以前的xml配置
package spittr.web; 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.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.InternalResourceViewResolver; /*** * 配置web */ @Configuration //配置类,需要的注解 @EnableWebMvc //启用Spring MVC @ComponentScan("spittr.web") //启用组件扫描,就会扫描到控制器 public class WebConfig extends WebMvcConfigurerAdapter { @Bean public ViewResolver viewResolver() {//配置JSP视图解析器 InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/");//在请求路径前,加入资源路径 resolver.setSuffix(".jsp");//在请求路径后,加入资源名称,比如/WEB-INF/views/home.jsp resolver.setViewClass(org.springframework.web.servlet.view.JstlView.class); return resolver; } @Override public void configureDefaultServletHandling( //处理静态资源 DefaultServletHandlerConfigurer configurer) { configurer.enable(); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { super.addResourceHandlers(registry); } }
编写Root配置类:可以使用其它非Web组件,比如数据库连接:
package spittr.config; import java.util.regex.Pattern; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.context.annotation.Import; import org.springframework.core.type.filter.RegexPatternTypeFilter; import spittr.config.RootConfig.WebPackage; /*** * 引入数据库配置类 自动扫描spittr包,以及子包 */ @Configuration // 配置类,需要的注解 @Import(DataConfig.class) @ComponentScan(basePackages = { "spittr" }, excludeFilters = { @Filter(type = FilterType.CUSTOM, value = WebPackage.class) }) public class RootConfig { public static class WebPackage extends RegexPatternTypeFilter { public WebPackage() { super(Pattern.compile("spittr\.web")); } } }
数据库连接配置:
package spittr.config; import javax.sql.DataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.core.JdbcOperations; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; @Configuration public class DataConfig { @Bean public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql:///spittle"); dataSource.setUsername("root"); dataSource.setPassword("root"); return dataSource; } @Bean public JdbcOperations jdbcTemplate(DataSource dataSource) { return new JdbcTemplate(dataSource); } }
网站应用,基本配置已经OK了,编写控制器:
package spittr.web.spittle; import static org.springframework.web.bind.annotation.RequestMethod.*; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/") // 处理所有主路径"/"的请求 public class HomeController { @RequestMapping(method = GET) public String home(Model model) { return "home";// "home.jsp":因为在WebConfig类里面,设置了自动加入".jsp",所以对应的是"home.jsp" } }
创建页面:(路径:/WEB-INF/views/home.jsp)
<html> <head> <title>Spitter</title> </head> <body> <h1>Welcome to Spitter</h1> </body> </html>