Spring boot整合jsp

  这几天在集中学习 Spring boot+Shiro 框架,因为之前 view 层用 jsp 比较多,所以想在 spring boot 中配置 jsp,但是 spring boot 官方不推荐使用 jsp,因为 jsp 相对于一些模板引擎,性能都比较低,官方推荐使用 thymeleaf,但是 Spring boot 整合 jsp 的过程已经完成,在这里记录一下。

  这篇博文是在 LZ 上篇文章spring boot+mybatis 整合基础上写的,开发工具仍然是 Intellij idea。这篇文章的重点是 Intellij idea 的设置,否则无法正常跳转到 jsp 页面,报 404

一、pom.xml 中加入 tomcat 支持和 jstl 标签库

<!-- tomcat 支持 -->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <!--<scope>provided</scope>-->
</dependency>
<!-- jstl 标签库 -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>

二、在 application.yml 中配置 jsp 路径

spring:
  mvc:
    view:
      # 页面默认前缀目录
      prefix: /WEB-INF/jsp/
      # 响应页面默认后缀
      suffix: .jsp

三、在 src/main 下面创建 webapp/WEB-INF/jsp 目录用来存放我们的 jsp 页面。

index.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Spring Boot Sample</title>
</head>

<body>
Time: ${time}
<br>
Message: ${message}
</body>
</html>

page1.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Spring Boot Sample</title>
</head>

<body>
<h1>${content}</h1>
</body>
</html>

四,编写 controller 测试

package com.test.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

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

/**

  • @author xiaodongdong
  • @description: 测试 controller 跳转到 jsp 页面
  • @create 2017-11-13 11:36
    /

@Controller
public class PageController {

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 从 application.yml 中读取配置,如取不到默认值为Hello Jsp</span>
@Value("${application.hello:Hello Jsp}"<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">private</span> String hello = "Hello Jsp"<span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 默认页&lt;br/&gt;
 * @RequestMapping("/") 和 @RequestMapping 是有区别的
 * 如果不写参数,则为全局默认页,加入输入404页面,也会自动访问到这个页面。
 * 如果加了参数“/”,则只认为是根页面。
 * 可以通过localhost:8080或者localhost:8080/index访问该方法
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
@RequestMapping(value </span>= {"/","/index"<span style="color: rgba(0, 0, 0, 1)">})
</span><span style="color: rgba(0, 0, 255, 1)">public</span> String index(Map&lt;String, Object&gt;<span style="color: rgba(0, 0, 0, 1)"> model){
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 直接返回字符串,框架默认会去 spring.view.prefix 目录下的 (index拼接spring.view.suffix)页面
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 本例为 /WEB-INF/jsp/index.jsp</span>
    model.put("time", <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Date());
    model.put(</span>"message", <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.hello);
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> "index"<span style="color: rgba(0, 0, 0, 1)">;
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 响应到JSP页面page1
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
@RequestMapping(</span>"/page1"<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> ModelAndView page1(){
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 页面位置 /WEB-INF/jsp/page/page.jsp</span>
    ModelAndView mav = <span style="color: rgba(0, 0, 255, 1)">new</span> ModelAndView("page/page1"<span style="color: rgba(0, 0, 0, 1)">);
    mav.addObject(</span>"content"<span style="color: rgba(0, 0, 0, 1)">, hello);
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> mav;
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 响应到JSP页面page1(可以直接使用Model封装内容,直接返回页面字符串)
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
@RequestMapping(</span>"/page2"<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String page2(Model model){
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 页面位置 /WEB-INF/jsp/page/page.jsp</span>
    model.addAttribute("content", hello + "(第二种)"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> "page/page1"<span style="color: rgba(0, 0, 0, 1)">;
}

}

  如果你百度,大部分教程也是这么告诉你的,没什么新鲜的,但是 LZ 试了几次都没有成功,有的资料说 jar 包不支持 jsp,jsp 需要运行在 servletContext 中,war 包需要运行在 server 服务器中如 tomcat(这里的 jar 和 war 指的是 pom.xml 中 <packaging>jar</packaging> 的设置),那开发环境中岂不是很麻烦,其实只要设置下 Intellij idea 就可以了。

  Intellij idea 工具栏 File->Project Structure,在弹出的页面中选 Modules,中间一栏选 Web(没有则按“+”号新建),然后设置 Deployment Descriptors 和 Web Resource Directories(这个变量应该是默认就有的),其中 Deployment Descriptors 指向 项目名称 /src/main/webapp/WEB-INF/web.xml,目前是没有 web.xml 的,会自动创建,Web Resource Directories 默认是有的,不用修改。

  然后就可以测试了,浏览器输入 http://localhost:8080  http://localhost:8080/page1 http://localhost:8080/page2 查看效果。

  

既然 Spring boot 不推荐使用 jsp,那只能换成 thymeleaf 了,当然其他的比如 freemarker 也可以选。