代码结构
WebConfig.java
package _5BuildingSpringwebapplications.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;
@Configuration
@EnableWebMvc
@ComponentScan("_5BuildingSpringwebapplications")
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 configurer) {
configurer.enable();
}
}RootConfig.java
package _5BuildingSpringwebapplications.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;
import _5BuildingSpringwebapplications.AutoScan;
@Configuration
//@ComponentScan(basePackages={"_5BuildingSpringwebapplications"},
// excludeFilters={
// @Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class)
// })
@ComponentScan(basePackageClasses = {AutoScan.class},
excludeFilters={
@Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class)
})
public class RootConfig {
}SpittrWebAppInitializer.java
package _5BuildingSpringwebapplications.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { RootConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { WebConfig.class };
}
}HomeController.java
package _5BuildingSpringwebapplications.web;
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="/", method=RequestMethod.GET)
public String home() {
return "home";
}
}home.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Spittr</title> <link rel="stylesheet" type="text/css" href="<c:url value="/resources/style.css" />"> </head> <body> <h1>Welcome to Spittr</h1> <a href="<c:url value="/spittles" />">Spittles</a> | <a href="<c:url value="/spitter/register" />">Register</a> </body> </html>
运行结果
AutoScan.java
package _5BuildingSpringwebapplications;
public class AutoScan {
}辅助文件
gradle.properties
activeMQVersion=5.7.0 aspectJVersion=1.7.2 commonsLangVersion = 3.1 ehcacheVersion=2.7.4 ehcacheJCacheVersion=1.4.0-beta1 h2Version=1.4.182 hamcrestVersion = 1.3 hibernateVersion=4.1.6.Final hibernateEntityManagerVersion=4.0.1.Final hibernateValidatorVersion = 5.0.1.Final jacksonVersion=2.4.3 javaxMailVersion=1.4.7 jspApiVersion = 2.1 jspElVersion = 2.2.4 jstlVersion = 1.2 junitVersion=4.11 log4jVersion=1.2.14 mockitoVersion=1.9.5 servletApiVersion = 3.1.0 slf4jVersion = 1.7.5 springAMQPVersion=1.0.0.RELEASE springDataJpaVersion=1.3.2.RELEASE springSecurityVersion = 4.1.3.RELEASE springVersion=4.3.3.RELEASE springWebflowVersion=2.4.1.RELEASE systemRulesVersion=1.5.0 thymeleafVersion = 2.1.3.RELEASE tilesVersion = 3.0.1
build.gradle
/*
* This build file was auto generated by running the Gradle ‘init‘ task
* by ‘Administrator‘ at ‘16-11-5 下午5:16‘ with Gradle 3.0
*
* This generated file contains a sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* user guide available at https://docs.gradle.org/3.0/userguide/tutorial_java_projects.html
*/
// Apply the java plugin to add support for Java
apply plugin: ‘java‘
// In this section you declare where to find the dependencies of your project
repositories {
// Use ‘jcenter‘ for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
// In this section you declare the dependencies for your production and test code
dependencies {
compile ‘org.slf4j:slf4j-api:1.7.21‘
compile("org.springframework:spring-context:${springVersion}")
compile("org.springframework:spring-jdbc:${springVersion}")
compile group: ‘org.springframework‘, name: ‘spring-web‘, version: "${springVersion}"
compile group: ‘org.springframework‘, name: ‘spring-webmvc‘, version: "${springVersion}"
compile("org.aspectj:aspectjweaver:${aspectJVersion}")
compile("log4j:log4j:${log4jVersion}")
compile group: ‘commons-dbcp‘, name: ‘commons-dbcp‘, version: ‘1.4‘
testCompile ‘junit:junit:4.12‘
testCompile("junit:junit:${junitVersion}")
testCompile("org.mockito:mockito-core:${mockitoVersion}")
testCompile("org.springframework:spring-test:${springVersion}")
}本文出自 “梦里不知身是客” 博客,转载请与作者联系!
原文:http://tenfee.blog.51cto.com/6353835/1871246