spring-boot入门之路-12-添加JSP支持

 大体步骤:

(1) 创建Maven web project;
(2) 在pom.xml文件添加依赖;
(3) 配置application.properties支持jsp
(4) 编写测试Controller
(5) 编写JSP页面
(6) 编写启动类App.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文件添加依赖

 

Xml代码  收藏代码

  1. <!-- spring boot parent节点,引入这个之后,在下面和spring boot相关的就不需要引入版本了; -->  
  2.     <parent>  
  3.        <groupId>org.springframework.boot</groupId>  
  4.        <artifactId>spring-boot-starter-parent</artifactId>  
  5.        <version>1.3.3.RELEASE</version>  
  6.     </parent>  

 

 

依赖包:

 

Xml代码  收藏代码

  1. <!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ........ -->  
  2.        <dependency>  
  3.            <groupId>org.springframework.boot</groupId>  
  4.            <artifactId>spring-boot-starter-web</artifactId>  
  5.        </dependency>  
  6.         
  7.        <!-- servlet 依赖. -->  
  8.        <dependency>  
  9.            <groupId>javax.servlet</groupId>  
  10.            <artifactId>javax.servlet-api</artifactId>  
  11.            <scope>provided</scope>  
  12.        </dependency>  
  13.         
  14.        <!--  
  15.            JSTL(JSP Standard Tag Library,JSP标准标签库)是一个不断完善的开放源代码的JSP标签库,是由apache的jakarta小组来维护的。JSTL只能运行在支持JSP1.2和Servlet2.3规范的容器上,如tomcat 4.x。在JSP 2.0中也是作为标准支持的。  
  16.             
  17.            不然报异常信息:  
  18.            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.)  
  19.               
  20.         -->  
  21.        <dependency>  
  22.            <groupId>javax.servlet</groupId>  
  23.            <artifactId>jstl</artifactId>  
  24.        </dependency>  
  25.         
  26.         
  27.         
  28.        <!-- tomcat 的支持.-->  
  29.        <dependency>  
  30.            <groupId>org.springframework.boot</groupId>  
  31.            <artifactId>spring-boot-starter-tomcat</artifactId>  
  32.            <scope>provided</scope>  
  33.        </dependency>  
  34.        <dependency>  
  35.            <groupId>org.apache.tomcat.embed</groupId>  
  36.            <artifactId>tomcat-embed-jasper</artifactId>  
  37.            <scope>provided</scope>  
  38.        </dependency>  

 

 

Jdk编译版本:

<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>

 

3application.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

编写类:com.kfit.jsp.controller.HelloController:

Java代码  收藏代码

  1. package com.kfit.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. /** 
  10.  * 测试 
  11.  * @author Angel 
  12.  * @version v.0.1 
  13.  */  
  14. @Controller  
  15. public class HelloController {  
  16.         
  17.        // 从 application.properties 中读取配置,如取不到默认值为Hello Shanhy  
  18.     @Value("${application.hello:Hello Angel}")  
  19.     private String hello;  
  20.      
  21.         
  22.        @RequestMapping("/helloJsp")  
  23.        public String helloJsp(Map<String,Object> map){  
  24.               System.out.println("HelloController.helloJsp().hello="+hello);  
  25.               map.put("hello", hello);  
  26.               return "helloJsp";  
  27.        }  
  28. }  

 

 

5)编写JSP页面

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

Html代码  收藏代码

  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)编写启动类

编写App.java启动类:

Java代码  收藏代码

  1. package com.kfit.jsp;  
  2.    
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  5. import org.springframework.boot.context.web.SpringBootServletInitializer;  
  6.    
  7. @SpringBootApplication  
  8. public class App extends SpringBootServletInitializer {  
  9. //    @Override  
  10. //    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {  
  11. //           return application.sources(App.class);  
  12. //    }  
  13.         
  14.        public static void main(String[] args) {  
  15.               SpringApplication.run(App.class, args);  
  16.        }  
  17. }  

 

 <!-- 添加JSP支持,1:添加servlet依赖;2:添加Tomcat 注意需注掉thymeleaf模板引擎依赖,两者不能同时存在-->

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

helloJsp


Hello Angel From application

猜你喜欢

转载自blog.csdn.net/qq_28384019/article/details/89208389