Spring MVC與Bootstrap基礎教程 - josdem
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基礎SpringMVC
- MVC與三層框架|Spring的基礎應用MVC框架Spring
- Java Web系列:Spring MVC基礎JavaWebSpringMVC
- Spring Cloud基礎教程SpringCloud
- Spring入門學習手冊 6:Spring MVC基礎中的基礎SpringMVC
- 【前端基礎】Bootstrap前端boot
- Bootstrap基礎入門boot
- Spring WebFlux 基礎教程:WebSocket 使用SpringWebUX
- Spring Boot Flyway管理資料庫版本 - josdemSpring Boot資料庫
- 玩轉Bootstrap基礎——表格boot
- Spring MVC入門教程(一)SpringMVC
- Spring MVC入門教程(二)SpringMVC
- 響應式程式設計基礎教程:Spring Boot 與 Lettuce 整合程式設計Spring Boot
- Spring WebFlux 基礎教程:引數校驗SpringWebUX
- Java之Spring基礎與IoCJavaSpring
- bootstrap框架基礎和常用元件boot框架元件
- Spring MVC教程——檢視閱讀SpringMVC
- 使用.NET MVC和BootstrapMVCboot
- 實現MVC基礎框架MVC框架
- Mockito與PowerMock的使用基礎教程Mockito
- Spring Boot基礎教程:EhCache快取的使用Spring Boot快取
- Spring Boot 2.x基礎教程:使用MongoDBSpring BootMongoDB
- BootStrap基礎入門概述總結boot
- Bootstrap系列 -- 11. 基礎表單boot
- bootstrap基礎學習一篇boot
- Shiro和Spring MVC、Mybatis整合教程SpringMVCMyBatis
- 深入分析Spring 與 Spring MVC容器SpringMVC
- tomcat + spring mvc原理外傳:spring mvc與前端的糾葛TomcatSpringMVC前端
- Bootstrap for MVC:Html.Bootstrap().TextBoxFor(model=>model.Name)bootMVCHTML
- Spring Boot 2.x基礎教程:快速入門Spring Boot
- Spring Boot 2.x基礎教程:使用JdbcTemplate訪Spring BootJDBC
- Spring學習筆記之Spring MVC 入門教程Spring筆記MVC
- Spring基礎Spring
- Bootstrap系列 -- 38. 基礎導航條boot
- Spring MVC詳細示例實戰教程【轉】SpringMVC
- 史上最簡單的 Spring MVC 教程(一)SpringMVC
- Spring 框架基礎(06):Mvc架構模式簡介,執行流程詳解Spring框架MVC架構模式
- Spring Boot基礎:Spring Boot簡介與快速搭建(1)Spring Boot