Spring Boot 集成JSP

来源:互联网 发布:mac版的飞秋 编辑:程序博客网 时间:2024/06/05 07:14

最近项目上准备使用springboot来搭建环境,但是对于jsp时代过来的人,让springboot兼容jsp就变成了很重要的一件事。因为springboot官方并不建议使用jsp,所以并不能直接通过添加起步依赖的方式,使其支持jsp作为模板引擎,只能通过自己来进行配置

一、项目环境

java 1.8
IntelliJ IDEA 2017.2.4

二、创建项目

IntelliJ IDEA中集成了Spring Boot,使用IDEA创建Spring Boot项目如图所示:

这里写图片描述

需要选择一下使用的JDK的版本,然后next。

这里写图片描述

因为要集成jsp,所以项目的打包方式只能选择war包的方式,或许这也是spring boot不推荐使用jsp作为模板引擎的原因;另外,artifact只能使用小写,所以上图中的驼峰命名是不支持的,改为小写的“springbootjspstart”之后才可以next。

这里写图片描述

这个地方需要选择项目的一个起步依赖,因为我们要搭建的是一个web项目,所以要勾选“web”选项,这个里面是集成了一个嵌入式的tomcat和spring mvc,其他的依赖需要就选择,也可以后面在pom.xml中添加。
这里写图片描述

这里直接finish就可以完成项目的基础项目的创建。项目的结构如下:

这里写图片描述

三、集成jsp

1.修改pom.xml

自动生成的pom文件如下:

<?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>    <groupId>com.tkmttt</groupId>    <artifactId>springbootjspstart</artifactId>    <version>0.0.1-SNAPSHOT</version>    <packaging>war</packaging>    <name>springbootjspstart</name>    <description>Demo project for Spring Boot</description>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.7.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <java.version>1.8</java.version>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-tomcat</artifactId>            <scope>provided</scope>        </dependency>        <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>        </plugins>    </build></project>

这里面已经有了嵌入式的tomcat依赖,测试依赖,和spring-boot-starter-web(包含了: Spring mvc; restful;jackjson支持;aop 等等)。但为了支持JSP还需要加入以下依赖:

  • servlet (jsp本质就是servlet)
  • JSTL(jsp的标签库)
  • tomcat-embed-jasper(tomcat的jsp解析引擎)
    添加好依赖的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>    <groupId>com.example</groupId>    <artifactId>jspstart</artifactId>    <version>0.0.1-SNAPSHOT</version>    <packaging>war</packaging>    <name>jspstart</name>    <description>Demo project for Spring Boot</description>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.7.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <java.version>1.8</java.version>    </properties>    <dependencies>        <!-- 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-->        <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>        <!--tomcat的jsp解析引擎-->        <dependency>            <groupId>org.apache.tomcat.embed</groupId>            <artifactId>tomcat-embed-jasper</artifactId>            <!--<scope>provided</scope>这个一定要去掉-->        </dependency>        <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>        </plugins>    </build></project>

应用修改:
这里写图片描述
下载依赖包:
这里写图片描述

2.修改application.properties

因为springboot默认不是采用jsp作为模板引擎的,所以我们需要修改他的配置文件application.properties,让他指定使用jsp,并配置spring解析的前缀后缀,在配置文件中加入:

# 页面默认前缀目录spring.mvc.view.prefix=/WEB-INF/jsp/# 响应页面默认后缀spring.mvc.view.suffix=.jsp# 自定义属性,可以在Controller中读取application.hello=hellooooooooo

3.增加webapp文件夹

我们在application.properties设置了页面的前缀为:/WEB-INF/jsp/,即在WEB-INF文件夹下的jsp文件夹里去找对应名称的jsp文件,但是我们的项目结构中并没有WEB-INF文件夹,所以我们得自己创建一个:

这里写图片描述

我们还需要在WEB-INF下新建一个web.xml文件,或者也可以去其他web项目下复制一个,内容如下:

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app>  <display-name>Archetype Created Web Application</display-name></web-app>

为了演示效果,我需要一个创建一个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><h1>jsp页面</h1>这里是后台传过来的数据${hello}</body></html>

4.创建HelloController

创建一个Controller拦截对 http://localhost:8080/hello 地址的访问,指向我们创建的hello.jsp

这里写图片描述

代码:

package com.tkmttt.springbootjspstart;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import java.util.Map;@Controllerpublic class HelloController {    //从application.properties中读取自定义的配置,如取不到默认值则显示“lalalala”    @Value("${application.hello:lalalala}")    private String hello;    @RequestMapping("/hello")    public String helloJsp(Map<String, Object> map){        System.out.println("进入了此方法");        map.put("hello", hello);        return "hello";    }}

5.测试

使用嵌入式的tomcat启动项目:

这里写图片描述

访问 http://localhost:8080/hello ,页面显示如下:

这里写图片描述

测试成功。

至于如何将项目导出成war包,并在服务器的tomcat中部署可以参考《Spring Boot 实战》第八章内容

原创粉丝点击