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");}}
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
- gradle 编译 sprint boot: spring mvc 使用jsp
- Spring-Boot使用jsp
- (1) Spring Boot + Gradle +Spring MVC项目搭建
- Spring boot MVC: can't find JSP
- spring boot jsp mvc jpa mysql
- 使用 Gradle 给Spring boot打 jar ,同时也把JSP打进去.
- Spring boot(11) 使用jsp
- 13. Spring Boot 使用JSP
- 使用Spring Boot和Gradle创建项目
- 使用gradle构建Spring boot项目
- [spring-boot] 使用gradle建立项目
- 采用Gradle快速构建基于Spring boot的MVC应用
- 使用Sprint Boot创建微服务
- spring boot学习三:基于jsp的spring mvc示例
- Spring Boot gradle
- spring boot + gradle + mysql
- Spring Boot + Gradle + Thymeleaf
- (11) spring boot使用jsp实例
- ModelMap和ModelAndView
- OpenCV -- 采用Python打开摄像头并截图
- Merge k Sorted Lists
- IOS经典书籍推荐
- ZigZag Conversion
- gradle 编译 sprint boot: spring mvc 使用jsp
- storm优化topology思路
- 黑马程序员-java环境配置path与classpath
- 如何关闭CAD中的*BAK文件?
- Android 系统重启与关机:Java 代码实现(3)
- iOS开发一行代码系列:一行搞定输入框优化
- 4.查找最小的k 个元素
- Android WebView开发问题及优化汇总
- lesson19