SpringMVC搭建,通过java配置


环境:JDK1.8、tomcat7.0、spring4.2.5.RELEASE

目录:

代码:

  pom.xml

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 3     <modelVersion>4.0.0modelVersion>
 4     <groupId>com.wangtaigroupId>
 5     <artifactId>spittrartifactId>
 6     <packaging>warpackaging>
 7     <version>0.0.1-SNAPSHOTversion>
 8     <name>spittr Maven Webappname>
 9     <url>http://maven.apache.orgurl>
10     <properties>
11         <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
12         <maven.compiler.source>1.7maven.compiler.source>
13         <maven.compiler.target>1.7maven.compiler.target>
14         <spring.version>4.2.5.RELEASEspring.version>
15     properties>
16     <dependencies>
17         <dependency>
18             <groupId>org.springframeworkgroupId>
19             <artifactId>spring-coreartifactId>
20             <version>${spring.version}version>
21         dependency>
22         <dependency>
23             <groupId>org.springframeworkgroupId>
24             <artifactId>spring-webartifactId>
25             <version>${spring.version}version>
26         dependency>
27         <dependency>
28             <groupId>org.springframeworkgroupId>
29             <artifactId>spring-webmvcartifactId>
30             <version>${spring.version}version>
31         dependency>
32         <dependency>
33             <groupId>javax.servletgroupId>
34             <artifactId>javax.servlet-apiartifactId>
35             <version>3.1.0version>
36             <scope>providedscope>
37         dependency>
38         <dependency>
39             <groupId>junitgroupId>
40             <artifactId>junitartifactId>
41             <version>3.8.1version>
42             <scope>testscope>
43         dependency>
44     dependencies>
45     <build>
46         <finalName>spittrfinalName>
47     build>
48 project>

  SpittrWebAppInitializer.java

 1 package spittr.config;
 2 
 3 import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
 4 
 5 public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{
 6 
 7     @Override
 8     protected Class<?>[] getRootConfigClasses() {
 9         return new Class<?>[] {RootConfig.class};
10     }
11 
12     @Override
13     protected Class<?>[] getServletConfigClasses() {
14         return new Class<?>[] {WebConfig.class};
15     }
16 
17     @Override
18     protected String[] getServletMappings() {
19         return new String[] {"/"};
20     }
21 
22 }

  RootConfig.java

 1 package spittr.config;
 2 
 3 import org.springframework.context.annotation.ComponentScan;
 4 import org.springframework.context.annotation.ComponentScan.Filter;
 5 import org.springframework.context.annotation.Configuration;
 6 import org.springframework.context.annotation.FilterType;
 7 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
 8 
 9 @Configuration
10 @ComponentScan(basePackages = {"spittr"}, excludeFilters = {@Filter(type=FilterType.ANNOTATION, value = EnableWebMvc.class)})
11 public class RootConfig  {
12 
13 }

  WebConfig.java

 1 package spittr.config;
 2 
 3 import org.springframework.context.annotation.Bean;
 4 import org.springframework.context.annotation.ComponentScan;
 5 import org.springframework.context.annotation.Configuration;
 6 import org.springframework.web.servlet.ViewResolver;
 7 import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
 8 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
 9 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
10 import org.springframework.web.servlet.view.InternalResourceViewResolver;
11 
12 @Configuration
13 @ComponentScan("spittr.web")
14 @EnableWebMvc
15 public class WebConfig extends WebMvcConfigurerAdapter{
16 
17     @Bean
18     public ViewResolver resolver(){
19         
20         InternalResourceViewResolver resolver = new InternalResourceViewResolver();
21         resolver.setPrefix("/");
22         resolver.setSuffix(".jsp");
23         resolver.setExposeContextBeansAsAttributes(true);
24         return resolver;
25     }
26     
27     @Override
28     public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
29         configurer.enable();
30     }
31     
32 }

TestController.java

 1 package spittr.web;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.RequestMethod;
 6 
 7 @Controller
 8 public class TestController {
 9 
10     @RequestMapping(value="test1", method=RequestMethod.GET)
11     public String test(){
12         return "index";
13     }
14 }

web.xml

1 DOCTYPE web-app PUBLIC
2  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
3  "http://java.sun.com/dtd/web-app_2_3.dtd" >
4 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5          xmlns="http://java.sun.com/xml/ns/javaee"
6          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
7          id="WebApp_1531879816720" version="3.0">
8     <display-name>Archetype Created Web Applicationdisplay-name>
9 web-app>

index.jsp

1 <html>
2 <body>
3 <h2>Hello World!h2>
4 body>
5 html>