Spring Boot JSP 视图解析器教程

教程分享 > WEB技术 > JavaScript (1566) 2024-08-07 11:21:12
学习创建和配置使用JSP 模板文件渲染视图层的Spring Boot jsp 视图解析器。本示例使用嵌入式 Tomcat 服务器来运行应用程序。

演示项目源码下载:(访问密码:9987)
spring-boot-demo-jsp-example.zip

源代码结构

此应用程序中的文件作为图像中的给定结构放置。
Spring Boot 应用程序结构
Spring Boot 应用程序结构

Maven 依赖 – pom.xml

此应用程序使用下面给出的依赖项。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.howtodoinjava</groupId>
    <artifactId>spring-boot-demo</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-demo Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!-- Web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Tomcat Embed -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- JSTL -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <!-- To compile JSP files -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>


Spring Boot 应用程序初始化器
生成可部署的战争文件的第一步是提供一个SpringBootServletInitializer子类并覆盖其configure()方法。这利用了 Spring Framework 的 Servlet 3.0 支持,并允许您在应用程序由 servlet 容器启动时对其进行配置。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
 
@SpringBootApplication
public class SpringBootWebApplication extends SpringBootServletInitializer {
 
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBootWebApplication.class);
    }
 
    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootWebApplication.class, args);
    }
}

Spring Controller

控制器类可以将方法映射到应用程序中的特定 URL。在给定的应用程序中,它有两个视图,即“/”和“/next”。

import java.util.Map;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
@Controller
public class IndexController {
 
    @RequestMapping("/")
    public String home(Map<String, Object> model) {
        model.put("message", "HowToDoInJava Reader !!");
        return "index";
    }
     
    @RequestMapping("/next")
    public String next(Map<String, Object> model) {
        model.put("message", "You are in new page !!");
        return "next";
    }
 
}

Spring Boot JSP ViewResolver 配置

要解析JSP文件的位置,您可以有两种方法。

1) 在 application.properties 中添加条目

spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
 
//For detailed logging during development
 
logging.level.org.springframework=TRACE
logging.level.com=TRACE

2)配置InternalResourceViewResolver来服务JSP页面

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
 
@Configuration
@EnableWebMvc
@ComponentScan
public class MvcConfiguration extends WebMvcConfigurerAdapter
{
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/view/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        registry.viewResolver(resolver);
    }
}

JSP 文件

这个 spring boot jsp 示例中使用的两个 JSP 文件如下。
index.jsp

<!DOCTYPE html>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html lang="en">
<body>
    <div>
        <div>
            <h1>Spring Boot JSP Example</h1>
            <h2>Hello ${message}</h2>
             
            Click on this <strong><a href="next">link</a></strong> to visit another page.
        </div>
    </div>
</body>
</html>


next.jsp

<!DOCTYPE html>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html lang="en">
<body>
    <div>
        <div>
            <h1>Another page</h1>
            <h2>Hello ${message}</h2>
             
            Click on this <strong><a href="/">link</a></strong> to visit previous page.
        </div>
    </div>
</body>
</html>

演示

编写完整代码并放入文件夹后,通过执行类中的main()方法运行应用程序SpringBootWebApplication

现在点击 URL:http://localhost:8080/

Spring Boot 应用程序 - 索引
Spring Boot 应用程序 - 索引

点击下一个链接

Spring Boot 应用程序 - 下一个
Spring Boot 应用程序 – 下一个

演示项目源码下载:(访问密码:9987)
spring-boot-demo-jsp-example.zip

https://www.leftso.com/article/875.html

相关文章
学习创建和配置使用JSP 模板文件渲染视图层的Spring Boot jsp 视图解析器
Java编程之Spring Boot中使用jsp视图模板
spring boot 开发技巧,在开发web项目中跳过thyemeleaf模板/js/css等缓存避免每次修改资源文件都需要重启服务器
jsp页面无法识别el表达式的解决方案,今天在写一个springmvc的小demo时,碰到一个问题,在jsp页面中书写为user.username的表达式语言,在浏览器页面中仍然显示为{user...
jsp
Spring Boot 2.0 @JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8") 注解格式化日期失效原因及解决。
jsp中/el表达式中将后台传来的时间戳格式化为年月日时分秒
1.引入相关el表达式标签库&lt;%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%&gtl;&lt;%@
一、项目环境Spring Boot 2.1.2.RELEASEshiro-spring 1.4二、去掉URL jsessionid在shiro配置中,配置关闭url中显示sessionId ...
jsp中EL表达式获取值中含逆转字符的处理当我们在jsp页面使用EL表达式对input设置value的时候&lt;input type='text' value='$
简介Spring Cloud Gateway中的全局异常处理不能直接用@ControllerAdvice来处理,通过跟踪异常信息的抛出,找到对应的源码,自定义一些处理逻辑来符合业务的需求
偶然遇到一个spring boot 的接口错误,Content type 'application/json;charset=UTF-8' not supported经过排查发现是参数里面的对象...
在国内项目中,阿里的fastjson包因其使用简单功能强大很多人喜欢在项目中使用,我也是其中一员
       学习使用Gson JsonReader类,这是一个基于拉的流式JSON解析器,它有助于将JSON作为标记流来读取​GSON1. JsonReader是什么JsonReader是流式...
前言值得一提的是JSON只有数组数据类型
引言AES代表高级加密系统,它是一种对称加密算法