最开始接触java的时候,前端页面基本都是用jsp来写,最近公司项目要使用SpringBoot重构,查看SpringBoot文档,发现SpringBoot不建议使用JSP,因为jsp在使用内嵌servlet容器时会有一些限制

虽然以后项目中也会将jsp替换成Template Engines,而且这都是前端的事情,其实与后端并无比较大的联系,但比较好奇Springboot中对jsp的使用,故根据官方文档进行了简单测试验证

如果使用JSP,则需要将项目打包成war包,jar包不支持JSP。

打开JSP sample,这是一个github上的项目,点击Code标签页,clone or download下载源码,这里下载的代码中包含有很多个springboot项目,其中spring-boot-samples中包含有我们需要的spring-boot-sample-web-jsp

解压下载的文件,spring-boot-samples下项目很多,我们根据需要在eclipse中只导入spring-boot-sample-web-jsp项目,导入时可能出现以下异常

点击finish -- ok,pom.xml文件中出现异常

选择Mark goal validate as ignored in pom.xml忽略掉该信息

点击OK

此时若项目上还有红色的叉号,则在项目名称上右键 -- Maven -- Update Project -- OK。

项目结构

包sample.jsp下有两个类

控制器WelcomeController

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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class WelcomeController { @Value("${application.message:Hello World}")
private String message = "Hello World"; @GetMapping("/")
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");
}
}

启动类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.web.servlet.support.SpringBootServletInitializer; @SpringBootApplication
public class SampleWebJspApplication extends SpringBootServletInitializer { @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SampleWebJspApplication.class);
} public static void main(String[] args) {
SpringApplication.run(SampleWebJspApplication.class, args);
}
}

两个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>

error.jsp

<!DOCTYPE html>
<html lang="en">
<body>
Something went wrong: ${status} ${error}
</body>
</html>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<!-- Your own application should inherit from spring-boot-starter-parent -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-samples</artifactId>
<version>${revision}</version>
</parent>
<artifactId>spring-boot-sample-web-jsp</artifactId>
<packaging>war</packaging>
<name>Spring Boot Web JSP Sample</name>
<description>Spring Boot Web JSP Sample</description>
<properties>
<main.basedir>${basedir}/../..</main.basedir>
<m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot>
</properties>
<dependencies>
<!-- Compile -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- Provided -->
<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>
<!-- Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
io.spring.javaformat
</groupId>
<artifactId>
spring-javaformat-maven-plugin
</artifactId>
<versionRange>
[0.0.6,)
</versionRange>
<goals>
<goal>validate</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

以上为该Demo官网pom文件,但其中内容个人觉得有多余,spring-boot-starter-tomcat和tomcat-embed-jasper在spring-boot-starter-web中已经有引用,所以可以去掉

启动主函数

页面访问:http://localhost:8080/

页面访问:http://localhost:8080/foo

一、本例中启动类继承了SpringBootServletInitializer,重写了configure方法,如果去掉该类的集成,能不能正常运行程序,其实是可以的(添加server.port=8081端口,与8080区分,测试)

二、SpringBootServletInitializer是做什么用处的?

springboot官方文档中 “91.1 Create a Deployable War File”有对该类的具体描述

三、按照当前pom.xml中配置,打包(eclipse中执行package),出现以下异常

[ERROR] Failed to execute goal io.spring.javaformat:spring-javaformat-maven-plugin:0.0.6:validate (default) on project spring-boot-sample-web-jsp: Formatting violations found in the following files:

日志中也给出了建议,使用:spring-javaformat:apply修复该问题

spring-javaformat:apply执行结束后,再次执行package,打包成功,打包过程中,会有很多的jar包下载。

进入到target目录,执行:java -jar .\spring-boot-sample-web-jsp-2.1.2.RELEASE.war,程序启动后,页面访问正常。

四、上面官方文档已经讲,打成可执行jar包,不支持JSP,下面尝试一下,是否如此,首先去掉主函数中集成SpringBootServletInitializer类,去掉重写的方法,pom.xml中war改为jar

  1、运行主函数,jsp可正常访问

  2、执行clean package打包,打包时出现异常,也许此异常与JSP无关,但此处就不在进行处理重新打包,毕竟官网也已经声明可执行jar不支持JSP

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:3.0.0:check (checkstyle-validation) on project spring-boot-sample-web-jsp: You have 2 Checkstyle violations. -> [Help 1]

五、以上demo跑通后,也需要自己新建一个使用jsp的web项目,来运行测试下。

SpringBoot使用JSP(官网Demo)的更多相关文章

  1. Java 银联支付官网demo测试及项目整合代码

    注:原文来源与 < Java 银联支付官网demo测试及项目整合代码  > 银联支付(网关支付B2C) 一.测试官网demo a)下载官网开发包,导入eclipse等待修改(下载的开发包没 ...

  2. jqgrid--api,官网demo,编辑

    api参考: http://blog.csdn.net/hurryjiang/article/details/7551477 官网demo: http://www.trirand.com/blog/j ...

  3. webpack官网demo起步中遇到的问题

    在webpack官网demo一开始搭建中 

  4. application.properties /application.yml官网查看配置;springboot application.properties 官网查看,info yml 查看;springboot.yml查看info;springboot.yml查看Actuator监控中心info

    官网查看: https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#appendix 查看info ...

  5. eclipse springboot 官网demo启动 SpringApplication类找不到

    网上有很多类似的,我这种情况是:maven的问题,,, 我自己下载了maven并集成到了eclipse上,导致java.lang.NoClassDefFoundError: org/springfra ...

  6. 苹果官网 demo The Elements 阅读随笔

    The Elements https://developer.apple.com/library/ios/samplecode/TheElements/Introduction/Intro.html# ...

  7. Datatable 导出到execl 官网demo

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content ...

  8. knockout——官网demo

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  9. Vertx上传 官网Demo Java版

    package io.vertx.example.web.upload; import io.vertx.core.AbstractVerticle; import io.vertx.example. ...

随机推荐

  1. 客户端缓存机制 - Cookie详解

    Cookie 作者:Stanley 罗昊 [转载请注明出处和署名,谢谢!] Cookie不是内置对象,所以用的时候需要new出来,Cookie是由服务端产生的,再发送给客户端保存,它不是内置对象,却是 ...

  2. RabbitMQ消息队列(六)-消息任务分发与消息ACK确认机制(.Net Core版)

    在前面一章介绍了在.Net Core中如何使用RabbitMQ,至此入门的的部分就完成了,我们内心中一定还有很多疑问:如果多个消费者消费同一个队列怎么办?如果这几个消费者分任务的权重不同怎么办?怎么把 ...

  3. 【Java基础】【18Map集合&模拟斗D主X排和F排】

    18.01_集合框架(Map集合概述和特点) A:Map接口概述 查看API可以知道: 将键映射到值的对象 一个映射不能包含重复的键 每个键最多只能映射到一个值 B:Map接口和Collection接 ...

  4. RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.2->WinForm版本新增新的角色授权管理界面效率更高、更规范

    角色授权管理模块主要是对角色的相应权限进行集中设置.在角色权限管理模块中,管理员可以添加或移除指定角色所包含的用户.可以分配或授予指定角色的模块(菜单)的访问权限.可以收回或分配指定角色的操作(功能) ...

  5. Flask入门之完整项目搭建

    一.创建虚拟环境 1,新建虚拟环境 cmd中输入:mkvirtualenv 环境名 2,在虚拟环境安装项目运行所需要的基本模块 pip install flask==0.12.4 pip instal ...

  6. [PHP] 魔术方法__get __set __sleep __wakeup的实际使用

    1.__get __set是在给不可访问属性赋值和读取时,调用 2.__sleep 是在序列化对象的时候调用 3.__wakeup是在反序列化对象的时候调用 4.可以在序列化对象的时候 , 只序列化指 ...

  7. Java基础:HashMap中putAll方法的疑惑

    最近回顾了下HashMap的源码(JDK1.7),当读到putAll方法时,发现了之前写的TODO标记,当时由于时间匆忙没来得及深究,现在回顾到了就再仔细思考了下 @Override public v ...

  8. CentOS7.3 yum install MySQL5.7

    安装环境:阿里云服务器 + CentOS7.3 测试工具:Navicat for MySQL 参考博客:https://blog.csdn.net/qq_38417808/article/detail ...

  9. Sublime 无法安装插件的解决办法

    1,打开命令面板 Ctrl + Shift + P  输入:pi  回车 按回车后,出现异常如下图: 解决办法: 1,点击Preferences----Brows Packages ---会到安装目录 ...

  10. 利用MingW检验程序运行内存

    今天zhx老师在讲课的时候提到了一种检验程序内存的方法 一般计算内存的方法就是手算,手动计算代码中每个变量所占的内存然后加起来 具体可以参考这篇文章 zhx老师讲的方法可以实现全自动化计算内存 具体怎 ...