Spring MVC与Bootstrap基础教程 - josdem

栏目: Java · 发布时间: 5年前

内容简介:build.gradle:Spring Boot控制器服务:

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

以上所述就是小编给大家介绍的《Spring MVC与Bootstrap基础教程 - josdem》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

现代编译原理

现代编译原理

Andrew W.Appel、Maia Ginsburg / 人民邮电 / 2005-9 / 59.00元

《现代编译原理:C语言描述(英文版)(本科)》全面讲述了现代编译器的各个组成部分,包括:词法分析、语法分析、抽象语法、语义检查、中间代码表示、指令选择、数据流分析、寄存器分配以及运行时系统等。与大多数编译原理的教材不同,《现代编译原理:C语言描述(英文版)(本科)》采用了函数语言和面向对象语言来描述代码生成和寄存器分配,对于编译器中各个模块之间的接口都给出了实际的 C 语言头文件。 全书分成两部分......一起来看看 《现代编译原理》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

MD5 加密
MD5 加密

MD5 加密工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具