天天看點

spring boot: 支援jsp,支援freemarker

spring boot: 支援jsp,支援freemarker

支援jsp:

加入依賴

<!--jsp-->
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<!--scope>provided</scope-->
		</dependency>
		<!--jsp-标簽-->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
		</dependency>
      

  

yml配置

spring:
      mvc:
         view:
            prefix: /WEB-INF/jsp/
            suffix: .jsp
      

webapp/WEB-FIN/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
</web-app>
      

支援freemarket

依賴

<!--freemarka-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-freemarker</artifactId>
		</dependency>
      
spring:
        freemarker:
    allow-request-override: false
    cache: false
    check-template-location: true
    charset: utf-8
    content-type: text/html
    expose-request-attributes: false
    expose-session-attributes: false
    expose-spring-macro-helpers: false
    suffix: .ftl
    template-loader-path: classpath:/templates
      

web.xml

可有可無

注冊controller檔案,

如果配置的是freemarker, 用@RestController ,模闆輸出就不能: return "模闆"; 必須要 ModelAndView了

@RequestMapping("/test")
    public String test()
    {
        return "seller/list";
    }
      

改為:

@RequestMapping("/test")
    public ModelAndView test()
    {
        return new ModelAndView ("seller/list"); 

      //如果不行,改為:      
return new ModelAndView ("seller/list.ftl");      
}