spring MVC+Gradle项目-HelloWorld入门例子

原创 2014年09月29日 17:54:36

①spring MVC + Gradle项目构成




HomeController.java


package cn.spring.sample;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);
		
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		String formattedDate = dateFormat.format(date);
		
		model.addAttribute("serverTime", formattedDate );
		
		return "home";
	}
	
}

home.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
	<title>Home</title>
</head>
<body>
<h1>
	Hello world!  
</h1>

<P>  The time on the server is ${serverTime}. </P>
</body>
</html>

build.gradle

apply plugin: 'java'
apply plugin: 'war'

repositories {
    mavenCentral()
}

String springVersion = '4.1.0.RELEASE'
String slf4jVersion = '1.7.7'

configurations.all {
	resolutionStrategy.eachDependency { DependencyResolveDetails details ->
		if (details.requested.group == 'org.springframework') {
			details.useVersion springVersion
		}
	}
}

dependencies {

	//core spring
	compile ('org.springframework:spring-context:' + springVersion)
	compile ('org.springframework:spring-core:' + springVersion)
	compile ('org.springframework:spring-webmvc:' + springVersion)

	//aspects
	compile 'org.aspectj:aspectjrt:1.8.2'
	compile 'org.aspectj:aspectjweaver:1.8.2'

	//logging
	compile ('org.slf4j:jcl-over-slf4j:' + slf4jVersion)
	compile ('org.slf4j:jul-to-slf4j:' + slf4jVersion)
	compile ('ch.qos.logback:logback-classic:1.1.2')

	//json
	compile 'com.fasterxml.jackson.core:jackson-databind:2.2.3'
	compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.13'
	compile 'org.codehaus.jackson:jackson-core-asl:1.9.13'

	//db access
	compile 'org.springframework:spring-jdbc:' + springVersion
	compile 'org.springframework:spring-orm:' + springVersion
	compile 'org.springframework.data:spring-data-jpa:1.7.0.RELEASE'
	compile 'org.apache.tomcat:tomcat-jdbc:8.0.9'
	compile 'org.hsqldb:hsqldb:2.3.2'


	//Servlet
        compile "javax.servlet:jstl:1.2"

	//testing
	testCompile group: 'junit', name: 'junit', version: '4.11'
	testCompile "org.mockito:mockito-all:1.9.0"
	testCompile "org.hamcrest:hamcrest-core:1.3"
	testCompile("org.spockframework:spock-core:0.7-groovy-2.0") {
		exclude group: 'org.codehaus.groovy', module: 'groovy-all'
	}
	testRuntime("org.spockframework:spock-spring:0.7-groovy-2.0") {
		exclude group: 'org.spockframework', module: 'spock-core'
	}
	testRuntime "cglib:cglib-nodep:2.2.2"
	testRuntime "org.objenesis:objenesis:1.2"
	testCompile 'org.springframework:spring-test:' + springVersion
	testRuntime 'com.jayway.jsonpath:json-path:0.8.1'
}

war {
	baseName = 'sample'
}

test {
	testLogging {
	events "passed", "skipped", "failed"
	}
}
<strong>
</strong>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/root-context.xml</param-value>
	</context-param>
	
	<!-- Creates the Spring Container shared by all Servlets and Filters -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- Processes application requests -->
	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
		
	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>

root-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">

	<!-- Root Context: defines shared resources visible to all other web components -->

</beans>


servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.1.xsd">

	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven />

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
	<resources mapping="/resources/**" location="/resources/" />

	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>

	<context:component-scan base-package="jp.microad.sample" />



</beans:beans>

log4j/xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

	<!-- Appenders -->
	<appender name="console" class="org.apache.log4j.ConsoleAppender">
		<param name="Target" value="System.out" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%-5p: %c - %m%n" />
		</layout>
	</appender>
	
	<!-- Application Loggers -->
	<logger name="jp.microad.sample">
		<level value="info" />
	</logger>
	
	<!-- 3rdparty Loggers -->
	<logger name="org.springframework.core">
		<level value="info" />
	</logger>
	
	<logger name="org.springframework.beans">
		<level value="info" />
	</logger>
	
	<logger name="org.springframework.context">
		<level value="info" />
	</logger>

	<logger name="org.springframework.web">
		<level value="info" />
	</logger>

	<!-- Root Logger -->
	<root>
		<priority value="warn" />
		<appender-ref ref="console" />
	</root>
	
</log4j:configuration>


②gradle构建

gradle build

③spring MVC+gradle运行

http://localhost:8080/sample








关于使用Gradle整合Springmvc构建JavaWeb项目的那点事

关于项目创建关于项目创建 如果你不知道如何创建一个通过Gradle构建的基本web项目,那么本文无法帮助你,你先去把项目创建好。如果你看到这里,我就假设你对gradke已经有了初步的了解,已经创建...
  • u013285335
  • u013285335
  • 2016年01月16日 20:42
  • 7202

Intellij IDEA创建基于Gradle的SpringMVC工程

在创建工程时选择基于Gradle的工程,勾选Web 如果选择使用gradle wrapper导致下载很慢,可以选择本地安装的gradle 添加tomcat(Run->Edit Configuratio...
  • cwt8805
  • cwt8805
  • 2017年02月28日 17:07
  • 4366

IntelliJ IDEA 与 Gradle + Spring项目的初次尝试

Myeclipse的臃肿,各种莫名其妙的refresh可解决的bug,以及迟滞的插件更新,最终促使我走向idea。 最近也在尝试着使用Gradle来构建项目(Maven依赖太过臃肿),所以本篇也是Gr...
  • zzlh110
  • zzlh110
  • 2015年10月02日 20:11
  • 20128

Spring Boot + gradle环境搭建

Spring Boot + gradle环境搭建Spring Boot gradle环境搭建 概述 生成基本的项目结构 1 使用Spring Initializr新建一个项目 2 gradle文件配...
  • u013005791
  • u013005791
  • 2017年06月16日 11:21
  • 4777

一份IDEA + gradle + spring项目的build.gradle清单

group 'com.superserver' version '1.0-SNAPSHOT' // 应用Gradle各插件 apply plugin: 'java' apply plugin: 'w...
  • u010214802
  • u010214802
  • 2017年04月22日 14:39
  • 1030

gradle多模块开发

参考文档:gradle的官方userguide.pdf文档的chapter 55和chapter 56. gradle的多模块或项目开发一定不会比maven差,在我看来!大的项目分成多个模块来开发是常...
  • xiejx618
  • xiejx618
  • 2014年08月10日 14:44
  • 23650

java 搭建基于springboot的ssh(spring + springmvc + hibernate)的gradle项目(基础篇)

之前本人有写过一篇 java 搭建基于springboot的ssm(spring + springmvc + mybatis)的maven项目,从需求量上来看还是很多同学对springboot感兴...
  • liboyang71
  • liboyang71
  • 2017年07月25日 23:26
  • 3386

通过gradle生成SpringMVC实现RESTful返回JSON格式数据

开发工具与配置 Spring 4.3.4.RELEASE Jackson 2.6.7 JDK 1.8 IntelliJ IDEA 16 gradle-3.2.1gradle依赖group '...
  • lufeihh2012
  • lufeihh2012
  • 2017年02月12日 22:52
  • 531

gradle 编译 sprint boot: spring mvc 使用jsp

spring boot 使用 gradle 编译 spring mvc jsp版本 web工程,入门demo。
  • akun21
  • akun21
  • 2014年12月12日 13:50
  • 2107

IDEA+gradle+spring+springMVC+hibernate+JPA+SpringData的web项目

1.build.gradlegroup 'com.lyj' version '1.0-SNAPSHOT'apply plugin: 'java' apply plugin: 'idea' apply ...
  • Colinasd
  • Colinasd
  • 2016年09月29日 21:59
  • 3060
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:spring MVC+Gradle项目-HelloWorld入门例子
举报原因:
原因补充:

(最多只允许输入30个字)