springboot教程3——模板引擎jsp

 

1 将使用JSP作为模板引擎的Maven依赖导入进来


	org.apache.tomcat.embed
	tomcat-embed-jasper
	provided


	javax.servlet
	jstl

2 在application.properties添加如下内容:

//文件前缀
spring.mvc.view.prefix=/WEB-INF/jsp/
//文件后缀
spring.mvc.view.suffix=.jsp

3 代码演示

SpringBootDemo81Application (启动)

package com.roncoo.education;

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

@SpringBootApplication
public class SpringBootDemo81Application {

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

package com.roncoo.education.controller;

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

/**
 * @author wujing
 */
@Controller
@RequestMapping(value = "/web")
public class WebController {

	@RequestMapping(value = "index")
	public String index(ModelMap map) {
		map.put("title", "thymeleaf hello word");
		return "index";
	}

}

    ServletInitializer.java

package com.roncoo.education;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

/**
 * 在Servlet容器中部署WAR的时候,不能依赖于Application的main函数而是要以类似于web.xml文件配置的方式来启动Spring应用上下文
* 所以此时需要声明这样一个类或者将应用的主类改为继承SpringBootServletInitializer也可以 * * @author wujing */ public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SpringBootDemo81Application.class); } }

application.properties

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

index.jsp

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>




	Spring Boot Demo - FreeMarker
	
	
	


	logo
	

${title}

Spring URL: ${springUrl}
JSTL URL: ${url}

pom.xml



	4.0.0

	com.roncoo.education
	spring-boot-demo-8-1
	0.0.1-SNAPSHOT
	war

	spring-boot-demo-8-1
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		1.4.0.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	

		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.springframework.boot
			spring-boot-starter-tomcat
			provided
		
		
			org.apache.tomcat.embed
			tomcat-embed-jasper
			provided
		
		
			javax.servlet
			jstl
		

		
			org.webjars
			jquery
			2.1.4
		

		
			org.springframework.boot
			spring-boot-devtools
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
				
					root
				
			
		
	



springboot教程3——模板引擎jsp_第1张图片

你可能感兴趣的:(springboot)