Spring Boot 添加JSP支持

来源:互联网 发布:1加到100的c语言程序 编辑:程序博客网 时间:2024/06/05 18:56

转:http://412887952-qq-com.iteye.com/blog/2292471

Spring Boot集成JSP疑问:http://412887952-qq-com.iteye.com/blog/2315957


这个部分比较复杂,所以单独创建一个工程来进行讲解;


      大体步骤:


(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文件添加依赖

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>com.example</groupId>  
  5.     <artifactId>spring-boot-jsp</artifactId>  
  6.     <version>0.0.1-SNAPSHOT</version>  
  7.     <packaging>war</packaging>  
  8.   
  9.     <properties>  
  10.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  11.     </properties>  
  12.   
  13.     <!-- Inherit defaults from Spring Boot -->  
  14.     <parent>  
  15.         <groupId>org.springframework.boot</groupId>  
  16.         <artifactId>spring-boot-starter-parent</artifactId>  
  17.         <version>1.4.0.RELEASE</version>  
  18.     </parent>  
  19.     <dependencies>  
  20.         <!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ........ -->  
  21.         <dependency>  
  22.             <groupId>org.springframework.boot</groupId>  
  23.             <artifactId>spring-boot-starter-web</artifactId>  
  24.         </dependency>  
  25.         <!-- servlet 依赖. -->  
  26.         <dependency>  
  27.             <groupId>javax.servlet</groupId>  
  28.             <artifactId>javax.servlet-api</artifactId>  
  29.             <scope>provided</scope>  
  30.         </dependency>  
  31.         <!-- JSTL(JSP Standard Tag Library,JSP标准标签库)是一个不断完善的开放源代码的JSP标签库,是由apache的jakarta小组来维护的。JSTL只能运行在支持JSP1.2和Servlet2.3规范的容器上,如tomcat   
  32.             4.x。在JSP 2.0中也是作为标准支持的。 不然报异常信息: javax.servlet.ServletException: Circular   
  33.             view path [/helloJsp]: would dispatch back to the current handler URL [/helloJsp]   
  34.             again. Check your ViewResolver setup! (Hint: This may be the result of an   
  35.             unspecified view, due to default view name generation.) -->  
  36.         <dependency>  
  37.            <groupId>javax.servlet</groupId>  
  38.            <artifactId>jstl</artifactId>  
  39.        </dependency>  
  40.         <!-- tomcat 的支持. -->  
  41.         <dependency>  
  42.             <groupId>org.springframework.boot</groupId>  
  43.             <artifactId>spring-boot-starter-tomcat</artifactId>  
  44.             <scope>provided</scope>  
  45.         </dependency>  
  46.         <dependency>  
  47.             <groupId>org.apache.tomcat.embed</groupId>  
  48.             <artifactId>tomcat-embed-jasper</artifactId>  
  49.             <scope>provided</scope>  
  50.         </dependency>  
  51.     </dependencies>  
  52.     <build>  
  53.        <finalName>spring-boot-jsp</finalName>  
  54.        <plugins>  
  55.            <plugin>  
  56.               <artifactId>maven-compiler-plugin</artifactId>  
  57.               <configuration>  
  58.                   <source>1.8</source>  
  59.                   <target>1.8</target>  
  60.               </configuration>  
  61.            </plugin>  
  62.        </plugins>  
  63.     </build>  
  64. </project>  


(3)application.properties配置


上面说了spring-boot 不推荐JSP,想使用JSP需要配置application.properties。 
添加src/main/resources/application.properties内容:
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # 页面默认前缀目录  
  2. spring.mvc.view.prefix=/WEB-INF/jsp/  
  3. # 响应页面默认后缀  
  4. spring.mvc.view.suffix=.jsp  
  5. # 自定义属性,可以在Controller中读取  
  6. application.hello=Hello Angel From application  
(4)编写测试Controller

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.example.jsp.controller;  
  2.   
  3. import java.util.Map;  
  4.   
  5. import org.springframework.beans.factory.annotation.Value;  
  6. import org.springframework.stereotype.Controller;  
  7. import org.springframework.web.bind.annotation.RequestMapping;  
  8.   
  9. @Controller  
  10. public class HelloController {  
  11.     //从application中读取配置,如取不到默认值为hello jack  
  12.     @Value("${application.hello:hello jack}")  
  13.     private String hello;  
  14.   
  15.     @RequestMapping("/helloJsp")  
  16.     public String helloJsp(Map<String, Object> map){  
  17.         System.out.println("HelloController.helloJsp().hello="+hello);  
  18.         map.put("hello", hello);  
  19.         return "helloJsp";  
  20.     }  
  21. }  


5)编写JSP页面


在 src/main 下面创建 webapp/WEB-INF/jsp 目录用来存放我们的jsp页面:helloJsp.jsp

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>Insert title here</title>  
  8. </head>  
  9. <body>  
  10.     helloJsp  
  11.     <hr>  
  12.     ${hello}  
  13.      
  14. </body>  
  15. </html>  
6)编写启动类


编写Application.java启动类:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.example;  
  2.   
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  5. import org.springframework.boot.web.support.SpringBootServletInitializer;  
  6. //import org.springframework.boot.context.web.SpringBootServletInitializer;  
  7.   
  8.   
  9. @SpringBootApplication  
  10. public class Application extends SpringBootServletInitializer {  
  11.   
  12.     public static void main(String[] args){  
  13.         SpringApplication.run(Application.class, args);  
  14.     }  
  15. }  

右键Run As  Java Application访问:http://127.0.0.1:8080/helloJsp 可以访问到:

helloJsp
Hello Angel From application

0 0
原创粉丝点击