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
- Spring入門學習手冊 6:Spring MVC基礎中的基礎SpringMVC
- Spring Cloud基礎教程SpringCloud
- 【前端基礎】Bootstrap前端boot
- Spring WebFlux 基礎教程:WebSocket 使用SpringWebUX
- Bootstrap基礎入門boot
- Spring Boot Flyway管理資料庫版本 - josdemSpring Boot資料庫
- Spring MVC教程——檢視閱讀SpringMVC
- Shiro和Spring MVC、Mybatis整合教程SpringMVCMyBatis
- 玩轉Bootstrap基礎——表格boot
- 使用.NET MVC和BootstrapMVCboot
- 響應式程式設計基礎教程:Spring Boot 與 Lettuce 整合程式設計Spring Boot
- tomcat + spring mvc原理外傳:spring mvc與前端的糾葛TomcatSpringMVC前端
- Java之Spring基礎與IoCJavaSpring
- Spring WebFlux 基礎教程:引數校驗SpringWebUX
- bootstrap框架基礎和常用元件boot框架元件
- 實現MVC基礎框架MVC框架
- Spring Boot基礎教程:EhCache快取的使用Spring Boot快取
- Spring Boot 2.x基礎教程:使用MongoDBSpring BootMongoDB
- BootStrap基礎入門概述總結boot
- Mockito與PowerMock的使用基礎教程Mockito
- Spring基礎Spring
- Spring 框架基礎(06):Mvc架構模式簡介,執行流程詳解Spring框架MVC架構模式
- Spring Boot 2.x基礎教程:使用JdbcTemplate訪Spring BootJDBC
- Spring Boot 2.x基礎教程:快速入門Spring Boot
- Spring Cloud Alibaba基礎教程:Nacos的叢集部署SpringCloud
- Spring Cloud Alibaba基礎教程:使用Nacos實現服務註冊與發現SpringCloud
- Spring Cloud Alibaba基礎教程:Nacos配置的多檔案載入與共享配置SpringCloud
- spring - mvcSpringMVC
- spring、spring MVC、spring BootMVCSpring Boot
- spring boot與spring mvc的區別是什麼?Spring BootMVC
- Spring Cloud Alibaba基礎教程:使用Sentinel實現介面限流SpringCloud
- Spring Cloud Alibaba 基礎教程:Nacos 生產級版本 0.8.0SpringCloud
- Spring Cloud Alibaba基礎教程:使用Nacos作為配置中心SpringCloud
- Spring Cloud Alibaba基礎教程:Nacos 生產級版本 0.8.0SpringCloud
- Spring Cloud Alibaba基礎教程:Nacos的資料持久化SpringCloud持久化
- 零基礎入門Python教程4節與基礎語法Python