提起Java不得不说的一个开发场景就是Web开发,也是Java最热门的开发场景之一,说到Web开发绕不开的一个技术就是JSP,由于目前市面上仍有不少的公司在使用JSP,因此本文就来介绍一下Spring Boot 怎么集成JSP开发,以及生产环境的详细部署方法。html
具体集成方法以下:java
在src/main目录下建立目录webapp/WEB-INF/jsp用于存放jsp页面,以下图:web
application.properties为全局配置文件,里面能够设置不少信息,好比设置日志、设置缓存、设置Spring、Spring SESSION等信息,咱们本文只须要设置JSP的目录文件,以及文件后缀,代码以下:spring
spring.mvc.view.prefix=/WEB-INF/jsp spring.mvc.view.suffix=.jsp
更多application.properties设置信息,查看官方文档:https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-propertiesapache
在pom.xml须要添加3个组件:json
具体代码以下:浏览器
<!--web支持--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--JavaServer Pages Standard Tag Library,JSP标准标签库--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!--内置tocat对Jsp支持的依赖,用于编译Jsp--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency>
注意:tomcat-embed-jasper的scope(做用域)值为provided,表示打包的时候不用打包进去,其余设备会提供,若是打包进去会和外部Tomcat提供的jar冲突,致使项目启动失败。spring-mvc
通过前3步的配置,项目配置部分已经完成的差很少了,接下来就是代码的编写了,代码的编写和Spring MVC同样,分为两部分Java类编写与标识注解,JSP模板建立与编写。为了更好的演示Spring Boot 的功能,咱们会尽量的简化业务逻辑,在这个示例中咱们建立一个Cat类,设置标签hi="Hello Cat",在页面输出标签便可。缓存
package com.hellospringboot.hellospringboot; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/cat") //建立路由规则http://xxxx/cat public class Cat { /** * 默认路由方法 * * @return */ @RequestMapping("") public ModelAndView index() { ModelAndView modelAndView = new ModelAndView("/index"); //设置对应JSP的模板文件 modelAndView.addObject("hi", "Hello,Cat"); //设置${hi}标签的值为Hello,Cat return modelAndView; } }
Spring MVC注解解读tomcat
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1> ${hi} </h1> </body> </html>
这个jsp只作了一件事,把java类里面的${hi}标签的值显示出来。
到此为止,若是您使用的是IDEA开发工具,那么如今就能够运行调试程序了,直接运行Index.jsp或者启动文件,或者启动文件(Application.java)便可,而后在浏览器输入:http://localhost:8080/cat 便可查看。
下载地址:https://tomcat.apache.org/download-90.cgi
以下图所示:
Windows下载:64-bit Windows zip | Linux下载:tar.gz
注意:若是是Windows版下载的免安装版若是放到C盘的话,注意给文件夹分配足够的权限,否则启动以后访问页面显示400或者505相似的问题。
若是要进行生产环境部署,须要单独配置一下Spring Boot 的入口类,须要继承SpringBootServletInitializer类,重写configure方法,由于默认状况外部Tomcat不能读取到Spring Boot 入口类的main方法启动程序加载,使用须要继承,代码以下:
package com.hellospringboot.hellospringboot; 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 HelloSpringBootApplication extends SpringBootServletInitializer{ /** * 重写configure方法,加载启动类 * @param application * @return */ @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(HelloSpringBootApplication.class); } /** * Spring Boot 默认main方法 * @param args */ public static void main(String[] args) { SpringApplication.run(HelloSpringBootApplication.class, args); } }
须要配置一下pom.xml,排除一下内置tomcat的jar包,防止打包以后与外面Tomcat jar 包冲突,致使项目启动失败,配置以下:
<!--排除内置tomcat jar包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
主要的代码是
找到:
jar
修改成:
war
为何须要修改jar包格式为war,由于若是打包为jar包的时候,不会包含JSP文件,因此访问的时候回404,而打包为war的时候回包含JSP文件,因此须要把打包格式修改成war.
jar和war的区别?
因此从jar和war的区别来看,网页程序打包成war格式也是很合适的。
若是须要修改生成文件的文件名称,能够设置build下的finalName属性,代码以下:
<build> <finalName>name</finalName> </build>
选择菜单栏Build => Build Artifacts.. => 点击Rebuild,便可生成war包,如图:
生成完以后,在项目的target目录下找到生成war文件,以下图:
在Host标签内添加Context设置,docBase属性填写war文件名称,配置以下:
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Context path="" docBase="springbootjsp" debug="0" privileged="true" reloadable="true" /> <!-- 其余代码 --> </Host>
运行bin/shutdown.bat文件,启动tomcat
输入地址:http://localhost:8080/cat 访问。
到此项目部署成功,虽然本人部署到Windows服务器了,Linux也是同样的步骤。
虽然上文咱们详细介绍了JSP在Spring Boot 中使用,可是Spring Boot官方并不推荐使用JSP(缘由见下文)。
以下图所示:
Spring官方不推荐使用JSP的缘由有这么几个:
更多详情,点击查看:https://spring.io/blog/2012/10/30/spring-mvc-from-jsp-and-tiles-to-thymeleaf
总结:按照官方的说法,若是你有数百个JSP页面,咱们并不建议您应该当即抛弃它们,从新使用Thymeleaf,可是,若是您开始开发一个新项目,强烈鼓励您比较其余模板引擎和JSP,以确认哪个更适合您。
开发Web应用之JSP篇:http://tengj.top/2017/03/13/springboot5/
欢迎扫码,加入圈子讨论交流