SpringBoot入门 --- 使用Jsp页面和使用模板freemarker

spring-boot 支持多种模版引擎包括:
1,FreeMarker
2,Groovy
3,Thymeleaf (Spring 官网使用这个)
4,Velocity
5,JSP (貌似Spring Boot官方不推荐,STS创建的项目会在src/main/resources 下有个templates 目录,这里就是让我们放模版文件的,然后并没有生成诸如 SpringMVC 中的webapp目录)

1. 返回jsp

a.配置文件pom.xml
 <!--继承springboot默认包-->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.8.RELEASE</version>
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>

  <dependencies>
    <!--添加一个特殊的web应用依赖-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>1.5.8.RELEASE</version>
    </dependency>

    <!--使用devtools来实现热部署-->
   <!-- <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <scope>provided</scope>
      <optional>true</optional>
    </dependency>-->

    <!--显示jsp需要用到的依赖-->
    <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>
    <defaultGoal>install</defaultGoal>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.0.2</version>
        <configuration>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>

        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>

            <dependencies>
                <!-- springc热部署 -->
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>springloaded</artifactId>
                    <version>1.2.6.RELEASE</version>
                </dependency>
            </dependencies>
            <configuration>
                <mainClass>com.zgd.springboot.SpringBootApp</mainClass>
            </configuration>
        </plugin>

    </plugins>
  </build>

我这里使用了spring提供的springloaded热部署插件,具体使用可以参考我的前面的文章
Idea上的SpringBoot的热部署配置, idea的自动编译设置

b. 在com.zgd.springboot包下创建App类
package com.zgd.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootApp {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootApp.class,args);
    }
}

这里我使用的是@SpringBootApplication,它继承了@ComponentScan, 会扫描他所在的包下面的所有子包和类的注解

c. Controller层

在包com.zgd.springboot.controller包下创建一个类

package com.zgd.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

@Controller
@RequestMapping("/hello")
public class HelloController {

    @RequestMapping("/page")
    public String page(Model model){
        System.out.println("page被调用了");
        model.addAttribute("time",new Date());
        model.addAttribute("msg","springboot jsp的方式");
        return "page";
    }
}
d. jsp文件

现在后端代码都写完了,就差一个jsp页面了 , 在src/webapp/WEB-INF/jsp文件夹下,写一个page.jsp页面

<%@ page language="java" pageEncoding="UTF-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title>Spring Boot Sample</title>
    </head>

    <body>
    Time: ${time}
    <br>
    Message: ${msg}
    </body>
</html>
e. 配置文件

既然是用的SpringBoot中嵌套的SpringMvc, 自然需要配置前端控制器

我这里用的application.yml,也可以用.properties , 有兴趣的可以看我的另一个文章:
SpringBoot的properties和yml两种配置方式, 配置注入参数, 以及配置文件读取失效的问题

resources/application.yml

spring:
  mvc:
    # 视图解析器配置
    view:
      #前缀
      prefix: /WEB-INF/jsp/
      #后缀
      suffix: .jsp

现在启动app类的main方法, 然后访问 localhost:8080/hello/page
结果却是这样的页面:
这里写图片描述

费尽心思找bug, 后来发现是SpringBoot对jsp不友好,他并不推荐jsp的方式.

解决方式如下:

  1. pom文件的package为war
  2. 不要直接启动main方法, 使用mvc的命令启动项目mvn spring-boot:run 或者idea右侧的maven菜单
    这里写图片描述

现在再访问:

这里写图片描述

返回JSP的方式就告一段落

2. 模板freemarker

其实和上面大体框架都差不多

1.首先改pom.xml

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>

    <!--        <dependency> -->
    <!--            <groupId>org.springframework.boot</groupId> -->
    <!--            <artifactId>spring-boot-starter-web</artifactId> -->
    <!--        </dependency> -->
    <!--        <dependency> -->
    <!--            <groupId>org.springframework.boot</groupId> -->
    <!--            <artifactId>spring-boot-starter-tomcat</artifactId> -->
    <!--        </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> -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

2.我们可以将pom.xml配置成jar,而不是war
3.可以将webapp文件夹都删除了
4.在resourses目录下, 创建一个文件夹templates ,这是默认的位置,如果不想放在这,或者不想用这个文件夹的名字,可在配置文件中配置, 我这里特意取名为mytemplates
5.修改配置文件, 通过template-loader-path指定我们放模板的文件夹



#spring:
#  mvc:
#    # 视图解析器配置
#    view:
#      #前缀
#      prefix: /WEB-INF/jsp/
#      #后缀
#      suffix: .jsp

spring:
  freemarker:
    # 模板后缀
    suffix: .ftl
    cache: false
    encoding: utf-8
    content-type: text/html
    # 模板文件加载路径
    template-loader-path: classpath:/mytemplates/

6.将刚刚的page.jsp,删除第一行<%@ page language="java" pageEncoding="UTF-8" %>,改文件名为page.ftl , 放在mytemplates下, ftl格式是freemarker的模板格式.
/resourses/mytemplates/page.ftl:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title>Spring Boot 通过freemarker的方式</title>
    </head>

    <body>
    <h3>Spring Boot 通过freemarker的方式</h3>
    Time: ${time}
    <br>
    Message: ${msg}
    </body>
</html>

6.controller层的model.addAttribute("time",new Date()); ,直接传过去一个Date会报异常, 需要转为new Date().toString()

package com.zgd.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

@Controller
@RequestMapping("/hello")
public class HelloController {

    @RequestMapping("/page")
    public String page(Model model){
        System.out.println("page被调用了");
        model.addAttribute("time",new Date().toString());
        model.addAttribute("msg","springboot freemarker的方式");
        return "page";
    }

}

访问 localhost:8080/hello/page ,正常显示
这里写图片描述

猜你喜欢

转载自blog.csdn.net/zzzgd_666/article/details/80379579