喜欢新吗???NO,我只想跟下时势..........
idea13 key:xiejx618/899696950-5TFF3-PCZRH-87DTT-Z7WFU-8Y272
一.创建build.gradle文件和所需目录
vim build.gradle
apply plugin:'war' targetCompatibility = 1.7 version = '1.0' repositories{ mavenCentral() } dependencies{ compile 'org.springframework:spring-webmvc:3.2.5.RELEASE' compile 'com.fasterxml.jackson.core:jackson-databind:2.3.0' providedCompile 'org.apache.tomcat:tomcat-servlet-api:8.0.0-RC5' } tasks.create(name: 'deploy',dependsOn:'war', type: Copy) { from(file("$buildDir/libs/gradletest-1.0.war")) into('/Volumes/3/tomcat8/webapps') }
mkdir -p src/java/main
mkdir -p src/java/webapp
mkdir -p src/java/webapp/WEB-INF
二.导入项目到IntelliJ IDEA 12
三.创建ServletInitializer,MvcConfigurerAdapter,一个Controller,一个jsp
package com.test.config; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; /** * User: xiejx618 * Date: 13-11-25 * Time: 下午2:57 */ public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return null; } @Override protected Class<?>[] getServletConfigClasses() { return new Class<?>[]{AppConfig.class}; } @Override protected String[] getServletMappings() { return new String[]{"/"}; } }
package com.test.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; 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; /** * User: xiejx618 * Date: 13-11-25 * Time: 下午1:53 */ @EnableWebMvc @Configuration @ComponentScan(basePackages={"com.test.web"}) public class AppConfig extends WebMvcConfigurerAdapter{ @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/css/**").addResourceLocations("/css/"); registry.addResourceHandler("/img/**").addResourceLocations("/img/"); registry.addResourceHandler("/js/**").addResourceLocations("/js/"); } @Bean public InternalResourceViewResolver getInternalResourceViewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/"); resolver.setSuffix(".jsp"); return resolver; } }
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title></title> </head> <body> this is the test.jsp page... </body> </html>
四.先启动tomcat 8,跟踪下tomcat输出日志tail -f /Volumes/3/tomcat8/logs/catalina.out ,运行gradle:deploy,如果在IDEA看到BUILD SUCCESSFUL,并且tomcat reload成功(跟踪日志有滚动输出),就可以通过浏览器测试了.这个比maven的plugin要好,直接copy应该要比上传快吧,也不用依赖manage,配什么权限,当然copy不能直接copy到其它机!!!scp可以么???
没有applicationContext.xml,是不是不习惯,看一下配置增强:http://spring.io/blog/2011/06/10/spring-3-1-m2-configuration-enhancements/