gradle 编译 sprint boot: spring mvc 使用jsp

来源:互联网 发布:零基础学seo 编辑:程序博客网 时间:2024/05/16 15:19

1.build.gradle: 重点是 dependencies 部分。

修改后右键工程-->gradle-->Refresh All

首次编译下载jar包,比较慢;如果失败重新执行Refresh All。

buildscript {    repositories {        maven { url "https://repo.spring.io/libs-release" }        mavenLocal()        mavenCentral()    }    dependencies {        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.0.RELEASE")    }}apply plugin: 'war'apply plugin: 'eclipse'//apply plugin: 'idea'apply plugin: 'spring-boot'jar {    baseName = 'bpo-pos-web'    version =  '0.1.0'}repositories {    mavenLocal()    mavenCentral()    maven { url "https://repo.spring.io/libs-release" }}dependencies {compile("org.springframework.boot:spring-boot-starter-web")compile("org.springframework.boot:spring-boot-starter-tomcat")compile("org.apache.tomcat.embed:tomcat-embed-jasper")compile("javax.servlet:jstl")testCompile("org.springframework.boot:spring-boot-starter-test")}task wrapper(type: Wrapper) {    gradleVersion = '1.11'}

2.java 包结构



3. src/main/resources/config/application.properties

spring.view.prefix: /WEB-INF/jsp/spring.view.suffix: .jspapplication.message: Hello Phil

4. jsp: welcome.jsp 文件路径需要与application.properties一致。

本例中路径是:src/main/webapp/WEB-INF/jsp/welcome.jsp

<!DOCTYPE html><%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><html lang="en"><body><c:url value="/resources/text.txt" var="url"/><spring:url value="/resources/text.txt" htmlEscape="true" var="springUrl" />Spring URL: ${springUrl} at ${time}<br>JSTL URL: ${url}<br>Message: ${message}</body></html>

5.Controller:  src/main/java/sample/jsp/WelcomeController.java

package sample.jsp;import java.util.Date;import java.util.Map;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class WelcomeController {@Value("${application.message:Hello World}")private String message = "Hello World";@RequestMapping("/")public String welcome(Map<String, Object> model) {model.put("time", new Date());model.put("message", this.message);return "welcome";}@RequestMapping("/foo")public String foo(Map<String, Object> model) {throw new RuntimeException("Foo");}}

6.启动类:src/main/java/sample/jsp/SampleWebJspApplication.java

package sample.jsp;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.boot.context.web.SpringBootServletInitializer;@SpringBootApplicationpublic class SampleWebJspApplication extends SpringBootServletInitializer {@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(SampleWebJspApplication.class);}public static void main(String[] args) throws Exception {SpringApplication.run(SampleWebJspApplication.class, args);}}

7.测试类:src/test/java/sample/jsp/SampleWebJspApplicationTests.java

package sample.jsp;import static org.junit.Assert.assertEquals;import static org.junit.Assert.assertTrue;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.test.IntegrationTest;import org.springframework.boot.test.SpringApplicationConfiguration;import org.springframework.boot.test.TestRestTemplate;import org.springframework.http.HttpStatus;import org.springframework.http.ResponseEntity;import org.springframework.test.annotation.DirtiesContext;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.web.WebAppConfiguration;@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes = SampleWebJspApplication.class)@WebAppConfiguration@IntegrationTest("server.port:0")@DirtiesContextpublic class SampleWebJspApplicationTests {@Value("${local.server.port}")private int port;@Testpublic void testJspWithEl() throws Exception {ResponseEntity<String> entity = new TestRestTemplate().getForEntity("http://localhost:" + this.port, String.class);assertEquals(HttpStatus.OK, entity.getStatusCode());assertTrue("Wrong body:\n" + entity.getBody(),entity.getBody().contains("/resources/text.txt"));}}

8.单元测试:右键SampleWebJspApplicationTests.java-->Run As(或者Debug As)-->JUnit Test


9.正式启动:右键SampleWebJspApplication.java-->Run As(或者Debug As)-->Java Application


10. 浏览器输入:localhost:8080,显示成功信息。

0 0