Spring Boot 添加JSP支持

大体步骤:

(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节点,引入这个之后,在下面和spring boot相关的就不需要引入版本了; -->

<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 Tag Library,JSP标准标签库)是一个不断完善的开放源代码的JSP标签库,是由apache的jakarta小组来维护的。JSTL只能运行在支持JSP1.2和Servlet2.3规范的容器上,如tomcat 4.x。在JSP 2.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 result of 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=Hello Angel 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;

/**

* 测试

* @version v.0.1

*/

@Controller

public class HelloController {

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

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

private String hello;

@RequestMapping("/helloJsp")

public String helloJsp(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//DTD HTML 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;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.context.web.SpringBootServletInitializer;

@SpringBootApplication

public class App extends SpringBootServletInitializer {

//    @Override

//    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

//           return application.sources(App.class);

//    }

public static void main(String[] args) {

SpringApplication.run(App.class, args);

}

}

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

helloJsp


Hello Angel From application

Spring Boot 添加JSP支持【转】的更多相关文章

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

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

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

    转:http://blog.csdn.net/linxingliang/article/details/52017140 这个部分比较复杂,所以单独创建一个工程来进行讲解: 大体步骤: (1)     ...

  3. spring boot 添加jsp支持注意事项

    1.一定要使用war包<packaging>war</packaging>2.将provided改为compile,具体如下: <dependency> <g ...

  4. Spring Boot 添加Shiro支持

    前言: Shiro是一个权限.会话管理的开源Java安全框架:Spring Boot集成Shiro后可以方便的使用Session: 工程概述: (工程结构图) 一.建立Spring Boot工程 参照 ...

  5. Spring boot 解析jsp支持jsp热部署

    解析jsp并且支持jsp热部署 参考地址:https://www.wanpishe.top/detail?blogId=39875536-7d45-48ec-a7b5-f36b85c3a235

  6. 81. Spring Boot集成JSP疑问【从零开始学Spring Boot】

    [原创文章,转载请注明出处] 针对文章: ()Spring Boot 添加JSP支持[从零开始学Spring Boot] 有网友提了这么一些疑问: 1.Spring Boot使用jsp时,仍旧可以打成 ...

  7. Spring boot整合jsp

    这几天在集中学习Spring boot+Shiro框架,因为之前view层用jsp比较多,所以想在spring boot中配置jsp,但是spring boot官方不推荐使用jsp,因为jsp相对于一 ...

  8. Springboot 系列(十七)迅速使用 Spring Boot Admin 监控你的 Spring Boot 程序,支持异常邮件通知

    1. Spring Boot Admin 是什么 Spring Boot Admin 是由 codecentric 组织开发的开源项目,使用 Spring Boot Admin 可以管理和监控你的 S ...

  9. spring boot集成jsp

    我们在使用spring boot进行web项目开发的时候,可能会选择页面用jsp.spring boot默认使用的html的,现在我们来看下如何集成jsp页面进行开发. 1.pom.xml文件引入所需 ...

随机推荐

  1. laravel中facade serviceprovider的理解

    一个serviceprovider就是一个解决某个功能的公用模块,实际上可以直接用在di里注册然后从di中取出,为啥还要搞个facade呢? 有几个方面的原因 1.把实例化移入到serviceprov ...

  2. wcf会话、实例化、并发

    在asp.net中含有会话,是保存值,供所有的程序使用,同样在wcf中也有会话,供多个客户端使用. 会话的支持通常在契约定义的开始标出,如下 [ServiceContract(Namespace = ...

  3. Exception in thread "main" java.lang.UnsatisfiedLinkError: org.apache.hadoop.io .nativeio.NativeIO$Windows.createDirectoryWithMode0(Ljava/lang/String;I)V

    首先,遇到这个问题的一个原因是windows环境中没有配置hadoophome.配置之后加入winutils工具 第二个原因,pom中执行的hadoop的版本与window环境中的hadoop的版本不 ...

  4. 【Unity笔记】提示框ToolTips大小自适应,及其闪烁的问题

    需求:制作了一个提示框,当鼠标移入背包格子内,显示提示框,且提示框位置跟随鼠标移动.当鼠标移出背包格子,隐藏提示框. 制作提示框ToolTips 因为提示框的大小要求随着显示的文本内容长度而自动大小适 ...

  5. 电子印章在Odoo的实现步骤

    1. 首先用PS制作一个电子印章,具体步骤可参考 http://www.jb51.net/photoshop/173568.html 2. 给Odoo中的pdf添加印章的原理,就是利用Odoo的QWe ...

  6. Hibernate 查询说明

    1.Criteria setFetchSize实际上与分页操作并没有关系,它的作用是在查询中进行分批数据返回,其中的值就是每次分批查询的记录数,主要是为了提高查询性能的. 举个例子来说:如果你这次查询 ...

  7. FileTable使用总结

    sqlsever2012以后才开始支持FileTable,具体的FileTable的介绍可以参考微软的官网 http://msdn.microsoft.com/zh-cn/library/gg5090 ...

  8. js 的数值限制可能引起的问题

    源于:https://raw.github.com/ruanyf/jstutorial/gh-pages/grammar/number.md 1. 根据国际标准IEEE 754,64位浮点数格式的64 ...

  9. jce_policy安装【java密码扩展无限制权限策略文件安装】

    下载与JDK或JRE对应版本的jce文件包,当前机器的jdk为1.8,所以下载jce_policy-8.zip. 下载地址:http://www.oracle.com/technetwork/java ...

  10. JDK工具(查看JVM参数、内存使用情况及分析等)

      在JDK的bin目录下有很多命令行工具: 我们可以看到各个工具的体积基本上都稳定在27kb左右,这个不是JDK开发团队刻意为之的,而是因为这些工具大多数是jdk\lib\tools.jar类库的一 ...