【发布时间】:2014-10-31 16:00:55
【问题描述】:
我正在尝试使用 spring boot、gradle 和 tomcat 实现一个基本的 hello world web 应用程序。我有以下java类。当我执行“./gradlew clean tomcatRunWar”时,tomcat刚刚启动但无法访问。
可能是什么问题?
build.gradle
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath 'org.gradle.api.plugins:gradle-tomcat-plugin:0.9.8'
classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.1.6.RELEASE'
}
}
apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'
apply plugin: 'tomcat'
mainClassName = "au.com.aeas.config.WebAppInitializer"
war {
baseName = ''
version = '0.0.0'
}
repositories {
mavenLocal()
mavenCentral()
}
configurations {
providedRuntime
}
dependencies {
tomcat "org.apache.tomcat.embed:tomcat-embed-core:7.0.42",
"org.apache.tomcat.embed:tomcat-embed-logging-juli:7.0.42"
tomcat("org.apache.tomcat.embed:tomcat-embed-jasper:7.0.42") {
exclude group: 'org.eclipse.jdt.core.compiler', module: 'ecj'
}
compile 'org.springframework.security:spring-security-config:3.2.5.RELEASE'
compile 'org.springframework.security:spring-security-web:3.2.5.RELEASE'
compile("org.springframework.boot:spring-boot-starter-web:1.1.6.RELEASE")
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat:1.1.6.RELEASE")
}
task wrapper(type: Wrapper) { gradleVersion = '1.6' }
-
WebAppInitializer.java
@EnableAutoConfiguration @ComponentScan("au.com.aeas.web.controller") @Controller public class WebAppInitializer extends WebMvcConfigurerAdapter { public static void main(String[] args) throws Exception { new SpringApplicationBuilder(WebAppInitializer.class).run(args); } @Bean public ApplicationSecurity applicationSecurity() { return new ApplicationSecurity(); } @Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver(); internalResourceViewResolver.setPrefix("WEB-INF/views/"); internalResourceViewResolver.setSuffix(".jsp"); return internalResourceViewResolver; } @Bean public AuthenticationSecurity authenticationSecurity() { return new AuthenticationSecurity(); } @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter { @Autowired private SecurityProperties security; @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().anyRequest().fullyAuthenticated().and() .formLogin(); } } @Order(Ordered.HIGHEST_PRECEDENCE + 10) protected static class AuthenticationSecurity extends GlobalAuthenticationConfigurerAdapter { @Override public void init(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().withUser("user").password("password") .roles("USER"); } } } -
HelloController.java
@Controller public class HelloController { @RequestMapping(value = "/hello", method = RequestMethod.GET) public ModelAndView hello() { ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("message", "charith"); modelAndView.setViewName("hello"); return modelAndView; } } -
你好.jsp
<html> <head> <title>Hello world page</title> </head> <body> <h1>${message}</h1> </body> </html>
【问题讨论】:
-
控制台有异常吗?您尝试访问时收到的错误消息是什么?
标签: spring tomcat gradle spring-boot