新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Springboot如何使用jsp

这篇文章主要讲解了Springboot如何使用jsp,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

“专业、务实、高效、创新、把客户的事当成自己的事”是我们每一个人一直以来坚持追求的企业文化。 成都创新互联是您可以信赖的网站建设服务商、专业的互联网服务提供商! 专注于成都网站建设、成都做网站、软件开发、设计服务业务。我们始终坚持以客户需求为导向,结合用户体验与视觉传达,提供有针对性的项目解决方案,提供专业性的建议,创新互联建站将不断地超越自我,追逐市场,引领市场!

步骤 1 : 视图支持

Springboot的默认视图支持是Thymeleaf,本知识点记录如何让 Springboot 支持 jsp。

步骤 2 : 可运行项目

首先下载一个简单的可运行项目作为演示:链接

下载后解压,比如解压到 E:\project\springboot 目录下

步骤 3 : pom.xml

增加对JSP支持

<?xml version="1.0" encoding="UTF-8"?>

  4.0.0

 com.ryan
 springboot
 0.0.1-SNAPSHOT
 springboot
 springboot
 
  
    org.springframework.boot
    spring-boot-starter-parent
    1.5.9.RELEASE
  

  
    
      org.springframework.boot
      spring-boot-starter-web
    
	  
		   junit
		   junit
		   3.8.1
		   test
	  
	  
	
    
       javax.servlet
       javax.servlet-api 
    
       
           javax.servlet
           jstl
       

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

  
    1.8
  

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

步骤 4 : application.properties

在src/main/resources 目录下增加 application.properties 文件,用于视图重定向jsp文件的位置

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

步骤 5 : HelloController

修改 HelloController,把本来的 @RestController 改为 @Controller。

这时返回"hello"就不再是字符串,而是根据application.properties 中的视图重定向,到/WEB-INF/jsp目录下去寻找hello.jsp文件

package com.ryan.springboot.web;
import java.text.DateFormat;
import java.util.Date;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
  @RequestMapping("/hello")
  public String hello(Model m) {
    m.addAttribute("now", DateFormat.getDateTimeInstance().format(new Date()));
    return "hello";
  }
}

步骤 6 : hello.jsp

在main目录下,新建 -> webapp/WEB-INF/jsp 目录。

随后新建 hello.jsp 文件,在其中使用 EL表达式 显示放在 HelloController 的model中的当前时间。

Springboot如何使用jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Hi JSP. 现在时间是 ${now}

步骤 7 : 启动测试

测试地址是:

http://127.0.0.1:8080/hello

Springboot如何使用jsp

看完上述内容,是不是对Springboot如何使用jsp有进一步的了解,如果还想学习更多内容,欢迎关注创新互联行业资讯频道。


当前标题:Springboot如何使用jsp
文章源于:http://www.wjwzjz.com/article/pgjoph.html
在线咨询
服务热线
服务热线:028-86922220
TOP