springboot整合jsp

前段时间有空看了下springboot的一些东西,感觉springboot使用起来非常方便与简单,因此也写了几个小应用,后来在springboot

怎么使用jsp上面起了疑问,查阅了多方资料,找到过其他人的博客的描述,也找到了spring在github上的给出的例子,看完后稍微改动后成功

整合jsp,于是决定将整合过程记载下来。

无论使用的是那种ide,基本在maven的使用上都是相同的,本文使用的是intelj,创建maven web工程,pom中依赖如下:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.com</groupId>
  <artifactId>springboot-Web</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>springboot-Web Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
    <relativePath/>
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.7</java.version>
    <version.spring>3.2.9.RELEASE</version.spring>
    <version.jackson>2.4.4</version.jackson>
  </properties>

  <dependencies>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--WebJars-->

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-jasper</artifactId>
      <scope>provided</scope>
    </dependency>

  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <!--<version>${spring.boot.version}</version>-->
        <configuration>
          <mainClass>cn.com.SpringBootWebApplication</mainClass>
          <layout>ZIP</layout>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>
                repackage
              </goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <repositories>
    <repository>
      <id>spring-snapshots</id>
      <url>http://repo.spring.io/snapshot</url>
      <snapshots><enabled>true</enabled></snapshots>
    </repository>
    <repository>
      <id>spring-milestones</id>
      <url>http://repo.spring.io/milestone</url>
      <snapshots><enabled>true</enabled></snapshots>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>spring-snapshots</id>
      <url>http://repo.spring.io/snapshot</url>
    </pluginRepository>
    <pluginRepository>
      <id>spring-milestones</id>
      <url>http://repo.spring.io/milestone</url>
    </pluginRepository>
  </pluginRepositories>
</project>

其中最需注意的如下这个依赖,少了这一个不能使用jsp。

    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-jasper</artifactId>
      <scope>provided</scope>
    </dependency>

同时在resources中的application.properties中配置如下:

    spring.view.prefix=/WEB-INF/jsp/
    spring.view.suffix=.jsp
    application.message: Hello Phil
    server.port=8282

最下两个配置可有可无,上面的两个配置中,是配置jsp所在目录,在这里和官网略有不同,官网配的都是spring.mvc.view的配置,但这样

似乎不起作用,有了解的可以与本人探讨一下。

下面就是controller编写,具体不再讲解,仅贴出代码:

package cn.com.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/6.
 */
@Controller
public class IndexController {

    @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";
    }
}

同时在启动类的编写如下:

package cn.com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

/**
 * Created by Administrator on 2016/5/6.
 */
@SpringBootApplication
public class SpringBootWebApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBootWebApplication.class);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootWebApplication.class, args);
    }

}

在这里,使用jsp必须继承SpringBootServletInitializer类,在这使用其他文章的原文:

对于需要部署到传统servlet容器之中的应用,Boot提供了一种方式以编码的方式初始化Web配置。为了使用这一点,Boot提供了可选的WebApplicationInitializer,

它会使用servlet容器来注册应用,这会通过Servlet 3.0 API以编码的方式注册servlet并且会用到ServletContext。通过提供SpringBootServletInitializer的子类,

Boot应用能够使用嵌入的Spring上下文来注册配置,这个Spring上下文是在容器初始化的时候创建的。

如此之后,就可以启动springboot应用使用jsp了。

整合代码下载为:github

时间: 2024-08-30 10:10:48

springboot整合jsp的相关文章

SpringBoot整合Jsp和Thymeleaf (附工程)

前言 本篇文章主要讲述SpringBoot整合Jsp以及SpringBoot整合Thymeleaf,实现一个简单的用户增删改查示例工程.事先说明,这两个是单独整合的,也就是两个工程.如需其中一个,只需看相应部分的介绍即可.若需工程源代码,可以直接跳到底部,通过链接下载工程代码. SpringBoot整合Jsp 开发准备 环境要求 JDK: 1.7或以上 SQL: MySql 这里我们需要在mysql中建立一张用户表,用于存储用户的信息. 数据库脚本如下: CREATE TABLE `t_user

springboot整合jsp踩坑

springboot以其高效的开发效率越来越多的用在中小项目的开发,并且在分布式开发中的使用也很广泛,springboot官方推荐的前端框架却是thymeleaf,并且默认不支持jsp,而大部分java开发人员最熟悉的前端开发工具却是jsp,自己在工作中就遇到这样的情况,因此在网上找一些整合的demo,但是依然踩了很多坑,记录下来以供学习. 代码及配置如下: 1.pom.xml,这个是本人整合中遇到的最大的坑,主要是spring-boot-starter-parent的版本,自己先去官网查,官方

SpringBoot学习7:springboot整合jsp

springboot内部对jsp的支持并不是特别理想,而springboot推荐的视图是Thymeleaf,对于java开发人员来说还是大多数人员喜欢使用jsp 1.创建maven项目,添加pom依赖 <!--springboot项目依赖的父项目--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</ar

springboot整合jsp 遇到的问题

1,在idea中新建jsp文件 首先需要在springboot项目 在src 中webapp /WEB-INF/JSP 当我右键想新建一个jsp文件时默认没有 File->Project Struction ->modules 按加号 新增web,双击web resource directory 修改路径 web resource directory path为 当前项目目录/src/webapp ->WEB-INF->JSP ok 创建jsp文件 2,jsp报错 Servlet.

springboot 整合jsp过程中的一些问题

出现一个奇葩的空指针异常 java.lang.NullPointerException: null at org.apache.catalina.authenticator.AuthenticatorBase.getJaspicProvider(AuthenticatorBase.java:1192) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:465) at org.a

springboot学习入门简易版五---springboot2.0整合jsp(11)

springboot对jsp支持不友好,内部tomcat对jsp不支持,需要使用外部tomcat,且必须打包为war包. 1 创建maven项目 注意:必须为war类型,否则找不到页面. 且不要把jsp页面存放在resources(原因:可能被别人访问,其次不在classes类路径中),因此,一般自行创建目录存放(一般/WEB-INF/下.  2 pom文件 <packaging>war</packaging> <!-- 注意为war包!!! --> <!-- s

为什么整合jsp后必须通过spring-boot:run方式启动?

背景 在Spring Boot - 整合Jsp/FreeMarker这篇文章中,我们用了两种启动方式 mvn clean spring-boot:run main方法启动测试发现,通过maven启动能够正常渲染jsp页面,而通过main方法启动无法渲染,本文分析下原因. 分析 我们代码没有调整,只是启动方式不同,那么怀疑是classpath不一致! mvn启动classpath /Users/wanye/Code/springboot/target/classes//Users/wanye/.m

SpringBoot系列十二:SpringBoot整合 Shiro

1.概念:SpringBoot 整合 Shiro 2.具体内容 Shiro 是现在最为流行的权限认证开发框架,与它起名的只有最初的 SpringSecurity(这个开发框架非常不好用,但是千万不要 以为 SpringSecurity 没有用处,它在 SpringCloud 阶段将发挥重大的作用).但是现在如果要想整合 Shiro 开发框架有一点很遗憾, SpringBoot 没有直接的配置支持,它不像整合所谓的 Kafka.Redis.DataSource,也就是说如果要想整合 Shiro 开

Spring Boot入门系列六( SpringBoot 整合thymeleaf)

SpringBoot 整合thymeleaf 一.什么是Thymeleaf模板 Thymeleaf是一款用于渲染XML/XHTML/HTML5内容的模板引擎.类似JSP,Velocity,FreeMaker等,它也可以轻易的与Spring MVC等Web框架进行集成作为Web应用的模板引擎.与其它模板引擎相比,Thymeleaf最大的特点是能够直接在浏览器中打开并正确显示模板页面,而不需要启动整个Web应用.它的功能特性如下: @Controller中的方法可以直接返回模板名称,接下来Thyme