moon0602的专栏

日本web开发的一些技术和经验分享

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

①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








阅读更多
上一篇web开发管理环境构筑
下一篇CentOS上安装mysql5.5安装
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭