您的位置:首页 > 编程语言 > Java开发

Spring Boot 添加JSP支持【从零开始学Spring Boot】

2018-02-08 17:07 603 查看
这个部分比较复杂,所以单独创建一个工程来进行讲解;     大体步骤:(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文件添加依赖<!-- spring boot parent节点,引入这个之后,在下面和springboot相关的就不需要引入版本了; -->       <parent>              <groupId>org.springframework.boot</groupId>              <artifactId>spring-boot-starter-parent</artifactId>              <version>1.3.3.RELEASE</version>       </parent> 依赖包:<!-- web支持: 1、webmvc; 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 TagLibrary,JSP标准标签库)是一个不断完善的开放源代码的JSP标签库,是由apachejakarta小组来维护的。JSTL只能运行在支持JSP1.2和Servlet2.3规范的容器上,如tomcat 4.x。在JSP2.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 resultof 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> 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> (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=HelloAngel From application (4)编写测试Controller编写类:com.kfit.jsp.controller.HelloController:package com.kfit.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; /** *测试 * @authorAngel(QQ:412887952) * @version v.0.1 */@Controllerpublic class HelloController {             //从 application.properties 中读取配置,如取不到默认值为HelloShanhy   @Value("${application.hello:Hello Angel}")    private String hello;                @RequestMapping("/helloJsp")       public StringhelloJsp(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"%><!DOCTYPEhtml PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><metahttp-equiv="Content-Type"content="text/html; charset=UTF-8"><title>Insert title here</title></head><body>       helloJsp       <hr>       ${hello}      </body></html> (6)编写启动类编写App.java启动类:package com.kfit.jsp; import org.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.boot.context.web.SpringBootServletInitializer; @SpringBootApplicationpublic class App extends SpringBootServletInitializer {//    @Override//    protectedSpringApplicationBuilder configure(SpringApplicationBuilder application) {//           returnapplication.sources(App.class);//    }             public static voidmain(String[] args) {              SpringApplication.run(App.class,args);       }} 右键Run As  Java Application访问:http://127.0.0.1:8080/helloJsp 可以访问到:helloJsp[align=center][/align]Hello Angel Fromapplication 特别说明:针对el表达式,类似${hello}这个对于servlet的版本是有限制的,2.4版本版本以下是不支持的,是无法进行识别的,请注意。
【Spring Boot 系列视频】视频&交流平台:à Spring Boot网易云课堂视频http://study.163.com/course/introduction.htm?courseId=1004329008à Spring Boot交流平台http://412887952-qq-com.iteye.com/blog/2321532 网易云课堂视频最新更新第十一章 Spring Boot 日志1、spring boot日志—理论2、Spring Boot日志-logback3、Spring Boot日志-log4j2第十二章 Spring Boot 知识点21、spring boot 服务配置和部署2、Spring Boot 定制URL匹配规则
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: