Spring MVC与Bootstrap基础教程 - josdem

19-01-17 banq
    

build.gradle:

apply plugin: 'groovy'
apply plugin: 'war'
apply plugin: 'jetty'

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenLocal()
    mavenCentral()
}

def springVersion = '4.1.7.RELEASE'
def groovyVersion = '2.4.5'
def aspectjVersion = '1.8.7'
def currentEnvironment = project.hasProperty("environment")?environment:"development"

dependencies {

  compile "org.springframework:spring-webmvc:${springVersion}"
  compile "org.codehaus.groovy:groovy:${groovyVersion}"
  compile 'log4j:log4j:1.2.17'

}

jettyRun{
  contextPath = "jmailer"
  httpPort = 8080
}

jettyRunWar{
  contextPath = "jmailer"
  httpPort = 8080
}

println "Setting environment to: ${currentEnvironment}"

task settingLog4jProperties(type:Copy){
  from "${System.getProperty('user.home')}/.jmailer/log4j-${currentEnvironment}.properties"
  into "src/main/resources/"
  rename { String fileName -> fileName.replace("-${currentEnvironment}", '') }
}

processResources.dependsOn "settingLog4jProperties"

Spring Boot控制器

@Controller
class EmailerController {

  @Autowired
  EmailerService emailerService

  Log log = LogFactory.getLog(this.class)

  @RequestMapping(value = '/', method = GET)
  String index(Map<String, Object> model) {
    log.debug 'Calling index'
    model.put('title', emailerService.getTitle('World!'))
    model.put('msg', emailerService.getDescription())

    return 'index'
  }
}

 服务:

@Service
class EmailerService {

  Log log = LogFactory.getLog(this.class)

  String getTitle(String name) {
    log.debug "GETTING title with name : ${name}"
    "Hello ${name}"
  }

  String getDescription() {
    log.debug "GETTING description"
    "Jmailer is a service for delivering emails"
  }

}

视图:

JSP + JSTL + bootstrap。一个简单的JSP页面,用于显示模型,并包含静态资源,如css和js。

文件:/WEB-INF/views/jsp/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>Jmailer</title>

<spring:url value="/resources/core/css/jmailer.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="#">Email deliver service</a>
    </div>
  </div>
</nav>

<div class="jumbotron">
  <div class="container">
    <h1>${title}</h1>
    <p>
      <c:if test="${not empty msg}">
        About: ${msg}
      </c:if>
    </p>
    <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>Text</p>
      <p>
        <a class="btn btn-default" href="#" role="button">Action</a>
      </p>
    </div>
  </div>


  <hr>
  <footer>
    <p>&copy; josdem 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" />





</body>
</html>

Spring XML配置

文件:/WEB-INF/dispatcher-servlet.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.jos.dem.jmailer" />


  <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>

文件:/WEB-INF/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_3_0.xsd"
  version="3.0">

  <display-name>Jmailer</display-name>
  <description>Emailer deliver service</description>

  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

根目录下创建一个名为.jmailer的文件夹并添加此log4j文件

文件:log4j-development.properties

#
# The logging properties used for eclipse testing, We want to see INFO output on the console.
#
log4j.rootLogger=out

log4j.logger.com.jos.dem=DEBUG,out
log4j.logger.org.springframework=INFO,out
log4j.logger.org.springframework.transaction=DEBUG
log4j.logger.org.springframework.jmx=ERROR,out
log4j.logger.org.springframework.aop=DEBUG
log4j.logger.org.hibernate=ERROR,out
log4j.logger.org.apache.commons.beanutils=ERROR,out
log4j.logger.org.displaytag=ERROR,out
log4j.logger.net.sf=ERROR,out

#Ensure the logs don't add to each other
log4j.additivity.com.tim.one=false
log4j.additivity.org.springframework=false
log4j.additivity.org.springframework.jmx=false
log4j.additivity.org.hibernate=false
log4j.additivity.org.apache.commons.beanutils=false
log4j.additivity.org.displaytag=false
log4j.additivity.net.sf=false

log4j.appender.out=org.apache.log4j.ConsoleAppender
log4j.appender.out.layout=org.apache.log4j.PatternLayout
log4j.appender.out.layout.ConversionPattern=%d %5p [%t] (%F:%L) - %m%n

主目录并键入:

gradle jettyRun

进入浏览器:http:// localhost:8080 / jmailer /

源码下载:

git clone https://github.com/josdem/jmailer-bootstrap.git
git fetch
git checkout setup