上一篇:springboot 1.5.4 web开发(三)

1      Spring Boot添加JSP支持

Spring Boot默认是不支持jsp的,集成使用步骤:

   新建spring-boot-jsp工程,工程源码:

spring-boot相关项目源码,

码云地址:https://git.oschina.net/wyait/springboot1.5.4.git

github地址https://github.com/wyait/spring-boot-1.5.4.git


   pom依赖

<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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

   <modelVersion>4.0.0</modelVersion>

   <parent>

      <!--spring boot项目的parent -->

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-parent</artifactId>

      <version>1.5.4.RELEASE</version>

   </parent>

   <groupId>com.wyait.boot</groupId>

   <artifactId>spring-boot-jsp</artifactId>

   <version>0.0.1-SNAPSHOT</version>

   <packaging>war</packaging>

 

   <dependencies>

      <dependency>

        <!--spring boot 引入Web模块。自动配置:tomcatspringmvcjackson -->

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-web</artifactId>

      </dependency>

      <!--tomcat 的支持. -->

      <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-tomcat</artifactId>

        <!--添加<scope>provided</scope>,因为provided表明该包只在编译和测试的时候用 -->

        <scope>provided</scope>

      </dependency>

      <dependency>

        <groupId>org.apache.tomcat.embed</groupId>

        <artifactId>tomcat-embed-jasper</artifactId>

        <scope>provided</scope>

      </dependency>

      <dependency>

        <groupId>javax.servlet</groupId>

        <artifactId>jstl</artifactId>

      </dependency>

   </dependencies>

   <build>

      <plugins>

        <plugin>

           <!--配置spring bootmaven插件 -->

           <groupId>org.springframework.boot</groupId>

           <artifactId>spring-boot-maven-plugin</artifactId>

        </plugin>

      </plugins>

   </build>

</project>

   配置application.properties,并添加jsp支持

spring boot 1.5.4 整合JSP(四)_springboot web开发

   编写DemoApplication

spring boot 1.5.4 整合JSP(四)_springboot web开发_02

   编写Controller user对象

spring boot 1.5.4 整合JSP(四)_springboot整合jsp_03getter/setter方法自动生成//TODO

@Controller

public class HelloController {

 

   /**

    *

    * @描述:跳转到thymeleaf页面

    * @创建人:wyait

    * @创建时间:201762710:40:22

    * @param map

    * @return

    */

   @RequestMapping("/hello")

   publicString toDemo(ModelMap map) {

      Useruser = new User();

      user.setId(5L);

      user.setAge(26);

      user.setName("张三");

      map.addAttribute("user",user);

      return"hello";

   }

}

   编写jsp页面

<%@ pagelanguage="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>

<metahttp-equiv="Content-Type" content="text/html;charset=UTF-8"/>

<title>This is Spring Bootjsp</title>

</head>

<body>

   <h2>这是一个jsp页面</h2>

   <hr/>

   <p>我叫${user.name },今年${user.age },编号:${user.id}</p>

</body>

</html>

 

   启动,访问:http://127.0.0.1:8080/hello

spring boot 1.5.4 整合JSP(四)_springboot添加jsp支持_04


项目源码,

码云地址:https://git.oschina.net/wyait/springboot1.5.4.git

github地址:https://github.com/wyait/spring-boot-1.5.4.git


spring boot系列文章:

spring boot 1.5.4 概述(一)

spring boot 1.5.4入门和原理(二)

spring boot 1.5.4 之web开发(三)

spring boot 1.5.4 整合JSP(四)

spring boot 1.5.4 集成devTools(五)

spring boot 1.5.4 集成JdbcTemplate(六)

spring boot 1.5.4 集成spring-Data-JPA(七)

spring boot 1.5.4 配置文件详解(八)

spring boot 1.5.4 统一异常处理(九)

spring boot 1.5.4 定时任务和异步调用(十)

spring boot 1.5.4 整合log4j2(十一)

spring boot 1.5.4 整合 mybatis(十二)

spring boot 1.5.4 整合 druid(十三)

spring boot 1.5.4 之监控Actuator(十四)

spring boot 1.5.4 整合webService(十五)

spring boot 1.5.4 整合redis、拦截器、过滤器、监听器、静态资源配置(十六)

spring boot 1.5.4 整合rabbitMQ(十七)

spring boot 1.5.4 集成Swagger2构建Restful API(十八)

spring boot 1.5.9 整合redis(十九