spring-boot整合jsp

spring-boot 支持多种模版引擎:

  1. FreeMarker 
  2. Groovy 
  3. Thymeleaf (Spring 官网使用这个) 
  4. Velocity 
  5. JSP (Spring Boot官方不推荐) 

接下来我们教大家如何在spring-boot中使用jsp。

1、pom.xml加入支持jsp的依赖:


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


    javax.servlet
    jstl

jar 改成  war

改完之后别忘了 maven update。

2、application.properties配置文件加入JSP支持:

# 页面默认前缀目录
spring.mvc.view.prefix=/WEB-INF/page/
# 响应页面默认后缀
spring.mvc.view.suffix=.jsp
# 自定义属性,可以在Controller中读取
application.hello=Hello World!

3、创建webapp文件夹以及页面:

spring-boot整合jsp_第1张图片

4、编写jsp页面:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>




sprint-boot jsp


    当前系统时间:
Time:${time }
Message:${message }

5、编写controller代码:

package nc.edu.nuc.Test.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import nc.edu.nuc.Test.entity.Test;
import nc.edu.nuc.Test.service.TestService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestController {
	private static final Logger logger = LoggerFactory.getLogger(TestController.class); 
	
	@Autowired
	private TestService testService;
	
	@RequestMapping("/test")
    @ResponseBody
    public String test(HttpServletRequest request) {
		String name = request.getParameter("name");
		Test findByName = testService.findByName(name);
		logger.info("test....{}",name);
        return findByName.toString();
    }
	
	@RequestMapping("/testJsp")
    public String testJsp(HttpServletRequest request,Model model) {
		String name = request.getParameter("name");
		Test findByName = testService.findByName(name);
		logger.info("testJsp....{}",findByName.toString());
		
		model.addAttribute("time", new Date());
		model.addAttribute("message", findByName.getName());
		
        return "index";
    }

}

这里用法和springmvc的一样,支持modelView和model...

spring-boot整合jsp_第2张图片





你可能感兴趣的:(spring-boot)