以下源码连接: https://git.oschina.net/jerryxiao/Spring.git
1. 使用Eclipse 创建Maven Web项目。 如下配置pom.xml 文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Spring</groupId>
<artifactId>Spring</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Spring Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.6</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<version>4.1.1.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>Spring</finalName>
</build>
</project>
2. 添加Web APP初始化启动类,主要采用集成 WebMvcConfigurerAdapter 类即可。 该类会创建 org.springframework.web.context.ContextLoaderListener 对象和 org.springframework.web.servlet.DispatcherServlet 对象。 代码如下所示:
/**
* @Project Spring
* @author Xiaowentao
* @Time 2016年7月17日
*/
package tom.spring.init;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import tom.spring.config.RootConfig;
import tom.spring.config.WebConfig;
public class SpringWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] {RootConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] {WebConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
}
该类相当于在web.xml文件中添加如下配置:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/servlet/*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3. 添加Web App配置类, 主要是给JSP文件配置一个ViewResolvet , 代码如下所示:
/**
* @Project Spring
* @author Xiaowentao
* @Time 2016年7月17日
*/
package tom.spring.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.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
/**
* @author Xiaowentao
*
*/
@Configuration
@EnableWebMvc
@ComponentScan(value="tom.spring.controller")
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configure){
configure.enable();
}
}
以上代码相当于在XML中配置 一个 名叫 “viewResolver” 类型为InternalResourceViewResolvet 的Bean. 和在Servlet配置文件中添加 <mvc:default-servlet-handler/>
4. 添加 Servlet配置,开启 Spring Web MVC 功能。如下所示代码, 无需任何实现,添加注解即可:
package tom.spring.config;
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.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@ComponentScan(basePackages={"tom.spring"},
excludeFilters={
@Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class)
})
public class RootConfig {
}
5. 添加控制器,该控制器仅返回一个视图的名称, 比如代码中返回 “home”字符串。DispatcherServlet 会根据返回的字符串去查找对象的视图文件即:.jsp 文件。本例为: home.jsp . 根据之前的配置,视图文件在目录 /WEB-INF/views/ 下。
/**
* @Project Spring
* @author Xiaowentao
* @Time 2016年7月17日
*/
package tom.spring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author Xiaowentao
*
*/
@Controller
@RequestMapping("/")
public class HomeController {
@RequestMapping({"/"})
public String home(){
return "home";
}
}
7. 在目录 /WEB-INF/views/ 创建 home.jsp 文件。 随便写一点内容。 我们已经把首页隐射到 home.jsp视图上了。
注: 去掉项目创建是生成的index.jsp .系统默认会渲染index.jsp文件。