目录结构及源码:
目录结构
laolang@laolang-Lenovo-G470:~/code/idea/smvcthree$ tree
.
├── build.gradle
├── settings.gradle
├── smvcthree.iml
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── mkyong
│ │ └── helloworld
│ │ ├── service
│ │ │ └── HelloWorldService.java
│ │ └── web
│ │ └── WelcomeController.java
│ ├── resources
│ │ └── logback.xml
│ └── webapp
│ ├── resources
│ │ └── core
│ │ ├── css
│ │ │ ├── bootstrap.min.css
│ │ │ └── hello.css
│ │ ├── images
│ │ └── js
│ │ ├── bootstrap.min.js
│ │ └── hello.js
│ └── WEB-INF
│ ├── lib
│ │ ├── aopalliance-1.0.jar
│ │ ├── commons-logging-1.2.jar
│ │ ├── jstl-1.2.jar
│ │ ├── logback-classic-1.1.3.jar
│ │ ├── logback-core-1.1.3.jar
│ │ ├── slf4j-api-1.7.7.jar
│ │ ├── spring-aop-4.1.6.RELEASE.jar
│ │ ├── spring-beans-4.1.6.RELEASE.jar
│ │ ├── spring-context-4.1.6.RELEASE.jar
│ │ ├── spring-core-4.1.6.RELEASE.jar
│ │ ├── spring-expression-4.1.6.RELEASE.jar
│ │ ├── spring-web-4.1.6.RELEASE.jar
│ │ └── spring-webmvc-4.1.6.RELEASE.jar
│ ├── spring-core-config.xml
│ ├── spring-mvc-config.xml
│ ├── views
│ │ └── jsp
│ │ └── index.jsp
│ └── web.xml
└── test
├── java
└── resources
22 directories, 27 files
laolang@laolang-Lenovo-G470:~/code/idea/smvcthree$
build.gralde
laolang@laolang-Lenovo-G470:~/code/idea/smvcthree$ cat build.gradle
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse-wtp'
apply plugin: 'jetty'
// JDK 7
sourceCompatibility = 1.7
targetCompatibility = 1.7
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile 'ch.qos.logback:logback-classic:1.1.3'
compile 'org.springframework:spring-webmvc:4.1.6.RELEASE'
compile 'javax.servlet:jstl:1.2'
}
// Embeded Jetty for testing
jettyRun{
contextPath = "spring4"
httpPort = 8080
}
jettyRunWar{
contextPath = "spring4"
httpPort = 8080
}
task copyJars(type: Copy) {
from configurations.compile
into 'src/main/webapp/WEB-INF/lib' // 目标位置
}laolang@laolang-Lenovo-G470:~/code/idea/smvcthree$
HelloWorldService.java
package com.mkyong.helloworld.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
@Service
public class HelloWorldService {
private static final Logger logger = LoggerFactory.getLogger(HelloWorldService.class);
public String getDesc() {
logger.debug("getDesc() is executed!");
return "Gradle + Spring MVC Hello World Example";
}
public String getTitle(String name) {
logger.debug("getTitle() is executed! $name : {}", name);
if(StringUtils.isEmpty(name)){
return "Hello World";
}else{
return "Hello " + name;
}
}
}
WelcomeController.java
package com.mkyong.helloworld.web;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.mkyong.helloworld.service.HelloWorldService;
@Controller
public class WelcomeController {
private final Logger logger = LoggerFactory.getLogger(WelcomeController.class);
private final HelloWorldService helloWorldService;
@Autowired
public WelcomeController(HelloWorldService helloWorldService) {
this.helloWorldService = helloWorldService;
}
@RequestMapping(value = "/", method = RequestMethod.GET)
public String index(Map<String, Object> model) {
logger.debug("index() is executed!");
model.put("title", helloWorldService.getTitle(""));
model.put("msg", helloWorldService.getDesc());
return "index";
}
@RequestMapping(value = "/hello/{name:.+}", method = RequestMethod.GET)
public ModelAndView hello(@PathVariable("name") String name) {
logger.debug("hello() is executed - $name {}", name);
ModelAndView model = new ModelAndView();
model.setViewName("index");
model.addObject("title", helloWorldService.getTitle(name));
model.addObject("msg", helloWorldService.getDesc());
return model;
}
}
logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<logger name="org.springframework" level="debug"
additivity="false">
<appender-ref ref="STDOUT" />
</logger>
<logger name="com.mkyong.helloworld" level="debug"
additivity="false">
<appender-ref ref="STDOUT" />
</logger>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
spring-core-config.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd ">
<context:component-scan base-package="com.mkyong.helloworld.service" />
</beans>
spring-mvc-config.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd ">
<context:component-scan base-package="com.mkyong.helloworld.web" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/views/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
</beans>
index.jsp
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Gradle + Spring MVC</title>
<spring:url value="/resources/core/css/hello.css" var="coreCss" />
<spring:url value="/resources/core/css/bootstrap.min.css" var="bootstrapCss" />
<link href="${bootstrapCss}" rel="stylesheet" />
<link href="${coreCss}" rel="stylesheet" />
</head>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">Project Name</a>
</div>
</div>
</nav>
<div class="jumbotron">
<div class="container">
<h1>${title}</h1>
<p>
<c:if test="${not empty msg}">
Hello ${msg}
</c:if>
<c:if test="${empty msg}">
Welcome Welcome!
</c:if>
<p>
<a class="btn btn-primary btn-lg" href="#" role="button">Learn more</a>
</p>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-4">
<h2>Heading</h2>
<p>ABC</p>
<p>
<a class="btn btn-default" href="#" role="button">View details</a>
</p>
</div>
<div class="col-md-4">
<h2>Heading</h2>
<p>ABC</p>
<p>
<a class="btn btn-default" href="#" role="button">View details</a>
</p>
</div>
<div class="col-md-4">
<h2>Heading</h2>
<p>ABC</p>
<p>
<a class="btn btn-default" href="#" role="button">View details</a>
</p>
</div>
</div>
<hr>
<footer>
<p>© Mkyong.com 2015</p>
</footer>
</div>
<spring:url value="/resources/core/css/hello.js" var="coreJs" />
<spring:url value="/resources/core/css/bootstrap.min.js" var="bootstrapJs" />
<script src="${coreJs}"></script>
<script src="${bootstrapJs}"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</body>
</html>
web.xml
<web-app 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"
version="2.5">
<display-name>Gradle + Spring MVC Hello World + XML</display-name>
<description>Spring MVC web application</description>
<!-- For web context -->
<servlet>
<servlet-name>hello-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hello-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- For root context -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-core-config.xml</param-value>
</context-param>
</web-app>
来源:
这个例子的源码来自:
http://www.mkyong.com/spring-mvc/gradle-spring-mvc-web-project-example/#
不过原文中有一个错误,那就是spring-web-config.xml应该改为spring-mvc-config.xml
原文中提供了下载地址
https://github.com/mkyong/spring4-mvc-gradle-xml-hello-world
还有就是build.gradle是正确的,但是如果你就这样导入idea,会有错误提示,所以还需要处理idea的jar包问题,这个问题的解决参考自:
http://www.cnblogs.com/wenjingu/p/3822989.html
idea导入jar包参考自:
http://www.cnblogs.com/lonelyxmas/archive/2011/07/17/2108895.html
具体操作步骤:
1、idea中新建好gradle项目,并把各部分代码按照正确的位置copy进去
2、在命令行中运行:
gradle build
gradle copyJars
这一步的目的是把所需要的jar提取出来,放到WEB-INF/lib中,当然,在idea中,你在这个目录中放入jar包是没有用的,不过这不是问题
3、idea导入jar包
idea中导入jar包是和eclipse不同的,这个我已经说明的所参考的地方
4、运行项目
gradle jettyRun
5、一些注意事项
1、网速要快 2、网络要稳定 3、要有耐心