转:http://blog.csdn.net/linxingliang/article/details/52017140

这个部分比较复杂,所以单独创建一个工程来进行讲解;

大体步骤:

(1)       创建Maven web project;

(2)       在pom.xml文件添加依赖;

(3)       配置application.properties支持jsp

(4)       编写测试Controller

(5)       编写JSP页面

(6)       编写启动类App.Java

1,FreeMarker
2,Groovy
3,Thymeleaf (spring 官网使用这个)
4,Velocity
5,JSP (貌似Spring Boot官方不推荐,STS创建的项目会在src/main/resources 下有个templates 目录,这里就是让我们放模版文件的,然后并没有生成诸如SpringMVC 中的webapp目录)
不过本文还是选择大家都熟悉的JSP来举例,因为使用JSP与默认支持的模版需要特殊处理,所以拿来举例更好。

(1)创建Maven web project

使用Eclipse新建一个Maven Web Project ,项目取名为:

spring-boot-jsp

(2)在pom.xml文件添加依赖

<!-- spring boot parent节点,引入这个之后,在下面和springboot相关的就不需要引入版本了; -->

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.3.3.RELEASE</version>

</parent>

依赖包:

<!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ........ -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<!-- servlet依赖. -->

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>javax.servlet-api</artifactId>

<scope>provided</scope>

</dependency>

<!--

JSTL(JSP Standard TagLibrary,JSP标准标签库)是一个不断完善的开放源代码的JSP标签库,是由apache的jakarta小组来维护的。JSTL只能运行在支持JSP1.2和Servlet2.3规范的容器上,如tomcat 4.x。在JSP2.0中也是作为标准支持的。

不然报异常信息:

javax.servlet.ServletException:Circular view path [/helloJsp]: would dispatch back to the current handler URL[/helloJsp] again. Check your ViewResolver setup! (Hint: This may be the resultof an unspecified view, due to default view name generation.)

-->

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>jstl</artifactId>

</dependency>

<!-- tomcat的支持.-->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-tomcat</artifactId>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>org.apache.tomcat.embed</groupId>

<artifactId>tomcat-embed-jasper</artifactId>

<scope>provided</scope>

</dependency>

Jdk编译版本:

<build>

<finalName>spring-boot-jsp</finalName>

<plugins>

<plugin>

<artifactId>maven-compiler-plugin</artifactId>

<configuration>

<source>1.8</source>

<target>1.8</target>

</configuration>

</plugin>

</plugins>

</build>

(3)application.properties配置

上面说了spring-boot 不推荐JSP,想使用JSP需要配置application.properties。 
添加src/main/resources/application.properties内容:

# 页面默认前缀目录

spring.mvc.view.prefix=/WEB-INF/jsp/

# 响应页面默认后缀

spring.mvc.view.suffix=.jsp

# 自定义属性,可以在Controller中读取

application.hello=HelloAngel From application

(4)编写测试Controller

编写类:com.kfit.jsp.controller.HelloController:

package com.kfit.jsp.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

/**

* 测试

* @authorAngel(QQ:412887952)

* @version v.0.1

*/

@Controller

public class HelloController {

// 从 application.properties 中读取配置,如取不到默认值为HelloShanhy

@Value("${application.hello:Hello Angel}")

private String hello;

@RequestMapping("/helloJsp")

public StringhelloJsp(Map<String,Object> map){

System.out.println("HelloController.helloJsp().hello="+hello);

map.put("hello",hello);

return"helloJsp";

}

}

(5)编写JSP页面

在 src/main 下面创建 webapp/WEB-INF/jsp 目录用来存放我们的jsp页面:helloJsp.jsp

<%@ page language="java" contentType="text/html;charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type"content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

helloJsp

<hr>

${hello}

</body>

</html>

(6)编写启动类

编写App.java启动类:

package com.kfit.jsp;

import org.springframework.boot.SpringApplication;

importorg.springframework.boot.autoconfigure.SpringBootApplication;

importorg.springframework.boot.context.web.SpringBootServletInitializer;

@SpringBootApplication

public class App extends SpringBootServletInitializer {

//    @Override

//    protectedSpringApplicationBuilder configure(SpringApplicationBuilder application) {

//           returnapplication.sources(App.class);

//    }

public static voidmain(String[] args) {

SpringApplication.run(App.class,args);

}

}

右键Run As  JavaApplication访问:http://127.0.0.1:8080/helloJsp 可以访问到:

helloJsp


Hello Angel Fromapplication

19. Spring Boot 添加JSP支持【从零开始学Spring Boot】的更多相关文章

  1. (19)Spring Boot 添加JSP支持【从零开始学Spring Boot】

    [来也匆匆,去也匆匆,在此留下您的脚印吧,转发点赞评论: 您的认可是我最大的动力,感谢您的支持] 看完本文章您可能会有些疑问,可以查看之后的一篇博客: 81. Spring Boot集成JSP疑问[从 ...

  2. Spring Boot 添加JSP支持【转】

    Spring Boot 添加JSP支持 大体步骤: (1)            创建Maven web project: (2)            在pom.xml文件添加依赖: (3)     ...

  3. 59. Spring Boot Validator校验【从零开始学Spring Boot】

    大纲: (1) 入门例子: (2) 国际化: (3) 在代码中添加错误信息: (1) 入门例子: Validator主要是校验用户提交的数据的合理性的,比如是否为空了,密码长度是否大于6位,是否是纯数 ...

  4. (3)Spring Boot热部署【从零开始学Spring Boot】

    在编写代码的时候,你会发现我们只是简单把打印信息改变了下,就需要重新部署,如果是这样的编码方式,那么我们估计一天下来之后就真的是打几个Hello World之后就下班了.那么如何解决热部署的问题呢?那 ...

  5. 72.spring boot讨论群【从零开始学Spring Boot】

    [从零开始学习Spirng Boot-常见异常汇总] 如果您碰到什么问题,您可以加群进行探讨,在群里有加入的都是Spring Boot志同道合的朋友: Spring Boot QQ交流群:193341 ...

  6. (6)Spring Boot datasource - mysql【从零开始学Spring Boot】

    在任何一个平台都逃离不了数据库的操作,那么在spring boot中怎么接入数据库呢? 很简单,我们需要在application.properties进行配置一下,application.proper ...

  7. (42)Spring Boot多数据源【从零开始学Spring Boot】

    我们在开发过程中可能需要用到多个数据源,我们有一个项目(MySQL)就是和别的项目(SQL Server)混合使用了.其中SQL Server是别的公司开发的,有些基本数据需要从他们平台进行调取,那么 ...

  8. (27)Spring Boot Junit单元测试【从零开始学Spring Boot】

    Junit这种老技术,现在又拿出来说,不为别的,某种程度上来说,更是为了要说明它在项目中的重要性. 那么先简单说一下为什么要写测试用例 1. 可以避免测试点的遗漏,为了更好的进行测试,可以提高测试效率 ...

  9. 86. Spring Boot集成ActiveMQ【从零开始学Spring Boot】

    在Spring Boot中集成ActiveMQ相对还是比较简单的,都不需要安装什么服务,默认使用内存的activeMQ,当然配合ActiveMQ Server会更好.在这里我们简单介绍怎么使用,本节主 ...

  10. 85. Spring Boot集成RabbitMQ【从零开始学Spring Boot】

    这一节我们介绍下Spring Boot整合RabbitMQ,对于RabbitMQ这里不过多的介绍,大家可以参考网络上的资源进行安装配置,本节重点是告诉大家如何在Spring Boot中使用Rabbit ...

随机推荐

  1. excel模板解析前后设计变化,以及我对此的看法和感受

    前言:近期也在做Excel模板的解析工作,目前来说,应该是第三版了.我最开始做的,就是垒鸡窝,虽然考虑了1.0提出的关于excel解析的一些建议和问题(单个模板),但是真的毫无设计可言.就几个工具类, ...

  2. POJ 2976 Dropping tests(01分数规划入门)

    Dropping tests Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11367   Accepted: 3962 D ...

  3. Windows系统——后缀为.zip.00X的zip分卷解压

    Windows下后缀为*.zip.001文件的解压方法: 后缀为*.zip.001文件用winrar无法解压, 解决办法是在windows下打开命令行界面, 输入:copy /B xx.zip.001 ...

  4. oracle 修改数据 保险方法

    oracle 中修改比较安全的方法:(pl/sql) 第一种方法: select * from temp where id=9 for update; 第二种方法: select t.*,rowid ...

  5. Optimal Marks(optimal)

    Optimal Marks(optimal) 题目描述 定义无向图边的值为这条边连接的两个点的点权异或值. 定义无向图的值为无向图中所有边的值的和. 给定nn个点mm条边构成的图.其中有些点的权值是给 ...

  6. Bzoj2829 信用卡凸包

    Time Limit: 10 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 333  Solved: 155 Description Input ...

  7. 【HDOJ5975】Aninteresting game(BIT原理)

    题意:给定n个区间,第i个区间的范围是[i-lowbit(i)+1,i].一共有q组询问,询问有两种: 1 x y:询问sigma lowbit(i) (x<=i<=y) 2.x:询问有几 ...

  8. LOJ#2244. 「NOI2014」起床困难综合症

    $n \leq 1e5$个位运算操作,$m \le 2^{30}$,问$0-m$中谁进行完所有操作值最大,输出这个最大值. cfA题难度?当送分题就不管了 and相当于几个位取0,or相当于几个位取1 ...

  9. 在LINQ TO SQL 中使用MVC3中的DataAnnotations 【MetadataType】

    原文发布时间为:2011-04-07 -- 来源于本人的百度文章 [由搬家工具导入] http://stackoverflow.com/questions/1535662/asp-net-mvc-li ...

  10. js5:框架的使用,使框架之间无痕连接

    原文发布时间为:2008-11-08 -- 来源于本人的百度文章 [由搬家工具导入] <html> <head> <base target="js4" ...