SpringBoot集成 JSP

前言

SpringBoot集成JSP

由于JSP这种技术本身有很多缺陷,所以SpringBoot默认是不支持JSP的。如果我们需要使用JSP,那需要
SpringBoot整合JSP。在整合JSP的时候,需要注意与原来的操作有所不同。项目需要打成 war 包,因此在创
建项目的时候需要注意。

集成JSP的步骤

创建项目

在集成JSP时,需要在创建Maven项目时选择webapp:File --> New --> Maven Project --> next,接下来输
webapp --> 选中maven-archetype-webapp -- Next

springboot和jsp前后端 springboot的jsp_maven

导入pom依赖

在项目的 pom.xml 文件中导入常规依赖之外,添加JSP和JSTL两个依赖。

<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>com.cyj.springboot</groupId>
	<artifactId>spring-boot-jsp-jstl</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-boot-jsp-jstl Maven Webapp</name>
	<url>http://maven.apache.org</url>

	<!-- Spring Boot 启动父依赖 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.1.RELEASE</version>
	</parent>

	<properties>
		<!-- 项目设置:编码格式UTF-8 -->
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<mybatis-spring-boot>1.2.0</mybatis-spring-boot>
		<mysql-connector>5.1.39</mysql-connector>
		<druid>1.0.18</druid>
	</properties>

	<dependencies>
		<!-- Spring Boot SpringMVC 依赖 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- SpringBoot支持JSP的依赖 -->
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<scope>provided</scope>
		</dependency>
		<!-- JSP页面使用JSTL标签 -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
		</dependency>

		<!-- Spring Boot Test 依赖 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!-- Spring Boot devtools 热部署 依赖 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
		<!-- Junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<!-- SpringBoot插件 -->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
		<!-- SpringBoot项目打包名称 -->
		<finalName>springjspjstl</finalName>
	</build>
</project>
配置视图解析

出于安全的考虑,SpringBoot推荐将JSP页面放入项目的webapp/WEB-INF/view目录下,需要在
application.properties配置文件中加入如下内容:

## SpringBoot支持JSP页面的视图解析配置 
#视图前缀 
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
案例测试

1. 新建控制器类(UserController.java)处理请求,内容如下:

package com.cyj.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UserController {
	/**
	 * http://localhost:8080/indexjsp?name=Jim
	 * 
	 * @param view
	 * @return
	 */
	@RequestMapping("/indexjsp")
	public ModelAndView index(ModelAndView view, String name) {
		System.out.println("成功访问了 indexjsp");
		view.addObject("msg", name + ",欢迎访问SpringBoot+JSP集成项目");
		view.setViewName("index");
		return view;
	}

	/**
	 * http://localhost:8080/jstljsp?name=Tom SpringBoot中使用JSP页面中的JSTL标签
	 * 
	 * @param view
	 * @param name
	 * @return
	 */
	@RequestMapping("/jstljsp")
	public ModelAndView queryAll(ModelAndView view, String name) {
		System.out.println("成功访问了 jstljsp");
		List<String> list = new ArrayList<String>();
		list.add(name);
		list.add("张三");
		list.add("李四");
		view.addObject("list", list);
		view.setViewName("list");
		return view;
	}

	// 静态资源访问路径 http://localhost:8080/js/my.js

}

2. 在webapp/WEB-INF/下新建view目录,然后新建一个list.jsp文件,内容如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML>
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>SpringBoot首页</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	

  </head>
  
  <body>
    This is JSP page:/WEB-INF/view/list.jsp <br>
    
    <s:if test="${not empty list }">
    	<s:forEach items="${list}" varStatus="sta" var="str">
    		${sta.count}  --------->  ${str} <br/>
    	</s:forEach>
    
    </s:if>
    
    
    <s:if test="${empty msg}">
    	<h5>page,request,session,application四个对象属性中msg都为null空</h5>
    </s:if>
  </body>
</html>

3. 定义主模块

package com.cyj;

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

/**
 * Spring Boot 应用启动类
 * 
 * @Description: 主模块
 * @ClassName: Application.java
 * @author ChenYongJia
 * @Date 2017-10-5 下午6:43:59
 * @Email 867647213@qq.com
 */
@SpringBootApplication
public class Application extends SpringBootServletInitializer {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);// 程序启动入口 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
	}

}

4. 启动项目,在浏览器中输入`http://localhost:8080/jstljsp?name=Tom’,效果如下:

springboot和jsp前后端 springboot的jsp_java_02

打war包发布项目
  1. 修改主程序入口函数,若打包成war包,则需要继
    org.springframework.boot.context.web.SpringBootServletInitializer类,覆盖其
    config(SpringApplicationBuilder)方法。具体修改后内容如下:
package com.cyj;

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

/**
 * Spring Boot 应用启动类
 * 
 * @Description: 主模块
 * @ClassName: Application.java
 * @author ChenYongJia
 * @Date 2017-10-5 下午6:43:59
 * @Email 867647213@qq.com
 */
@SpringBootApplication
public class Application extends SpringBootServletInitializer {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);// 程序启动入口 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
	}

	/**
	 * STS中运行不需要 需要把web项目打成war包部署到外部tomcat运行时需要改变启动方式
	 */
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return builder.sources(Application.class);
	}

}
  1. 打包:右键项目 --> Run as --> Maven build... 输入clean package命令 本阶段需要注意的时,需修改pom.xml中项目的打包的方式为war,即
    <packaging>war</packaging>
  2. 运行。
    在放置war包的目录下,按shift键,输入java -jar 包名.war即可