详解Spring Boot 添加JSP支持
大体步骤:
(1)创建maven web project;
(2)在pom.xml文件添加依赖;
(3)配置application.properties支持jsp
(4)编写测试controller
(5)编写jsp页面
(6)编写启动类application.java
1,freemarker
2,groovy
3,thymeleaf (spring 官网使用这个)
4,velocity
5,jsp (貌似spring boot官方不推荐,sts创建的项目会在src/main/resources 下有个templates 目录,这里就是让我们放模版文件的,然后并没有生成诸如 springmvc 中的webapp目录)
不过本文还是选择大家都熟悉的jsp来举例,因为使用jsp与默认支持的模版需要特殊处理,所以拿来举例更好。
(1)创建maven web project
使用eclipse新建一个maven web project ,项目取名为:
spring-boot-jsp
(2)在pom.xml文件添加依赖
<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>spring-boot-jsp</artifactid> <version>0.0.1-snapshot</version> <packaging>war</packaging> <properties> <project.build.sourceencoding>utf-8</project.build.sourceencoding> </properties> <!-- inherit defaults from spring boot --> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.4.0.release</version> </parent> <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(jsp standard tag library,jsp标准标签库)是一个不断完善的开放源代码的jsp标签库,是由apache的jakarta小组来维护的。jstl只能运行在支持jsp1.2和servlet2.3规范的容器上,如tomcat 4.x。在jsp 2.0中也是作为标准支持的。 不然报异常信息: javax.servlet.servletexception: circular view path [/hellojsp]: would dispatch back to the current handler url [/hellojsp] again. check your viewresolver setup! (hint: this may be the result of an unspecified view, due to default view name generation.) --> <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> <dependency> <groupid>org.apache.tomcat.embed</groupid> <artifactid>tomcat-embed-jasper</artifactid> <scope>provided</scope> </dependency> </dependencies> <build> <finalname>spring-boot-jsp</finalname> <plugins> <plugin> <artifactid>maven-compiler-plugin</artifactid> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
(3)application.properties配置
上面说了spring-boot 不推荐jsp,想使用jsp需要配置application.properties。
添加src/main/resources/application.properties内容:
# 页面默认前缀目录 spring.mvc.view.prefix=/web-inf/jsp/ # 响应页面默认后缀 spring.mvc.view.suffix=.jsp # 自定义属性,可以在controller中读取 application.hello=hello angel from application
(4)编写测试controller
package com.example.jsp.controller; import java.util.map; import org.springframework.beans.factory.annotation.value; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; @controller public class hellocontroller { //从application中读取配置,如取不到默认值为hello jack @value("${application.hello:hello jack}") private string hello; @requestmapping("/hellojsp") public string hellojsp(map<string, object> map){ system.out.println("hellocontroller.hellojsp().hello="+hello); map.put("hello", hello); return "hellojsp"; } }
(5)编写jsp页面
在 src/main 下面创建 webapp/web-inf/jsp 目录用来存放我们的jsp页面:hellojsp.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> hellojsp <hr> ${hello} </body> </html>
6)编写启动类
编写application.java启动类:
package com.example; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.boot.web.support.springbootservletinitializer; //import org.springframework.boot.context.web.springbootservletinitializer; @springbootapplication public class application extends springbootservletinitializer { public static void main(string[] args){ springapplication.run(application.class, args); } }
右键run as java application访问:http://127.0.0.1:8080/hellojsp 可以访问到:
hellojsp
hello angel from application
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: Java 并发编程之线程挂起、恢复与终止