首页> 文章> 详情

Spring Boot JSP 视图解析器教程

教程分享 > Java教程 (1064) 2024-04-17 12:31:29
学习创建和配置使用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中的拦截器和过滤器都是用于在请求到达控制器之前或之后对请求进行处理的。区别如下:拦截器是基于Java的反射机制,而过滤器是基于函数回调。拦截器只能对Spring MVC的请...
Spring Boot 2.0,Spring框架的Spring Boot 中的Spring Boot Actuator变化讲解。并且了解如何在Spring Boot 2.0中使用Actuator...
Spring Boot 2.0 绑定properties属性资源文件 Spring Boot 2.0 读取properties配置文件值 Spring Boot 2.0获取properties配...
引言    通过之前spring boot mybatis 整合的讲解: spring boot mybaties整合  (spring boot mybaties 整合 基于Java注解方式写...
spring boot 1.5整合redis实现spring的缓存框架,spring boot,redis
spring boot入门,spring boot是一个崭新的spring框架分支项目,本文讲解其属性配置相关
spring boot是一个崭新的spring框架分支项目,本文讲解基本的数据库配置
Spring Boot 1.x升级到Spring Boot 2.0迁移指南
Spring Boot 2.0 有哪些新特性_Spring Boot 2.0新功能,在本文中,我们将探讨为Spring Boot 2.0计划的一些更改和功能。我们还会描述这些变化如何帮助我们提高...
Spring Boot validation整合hibernate validator实现数据验证,Spring Boot validation使用说明,Spring Boot validat...
spring boot mybatis 整合使用讲解介绍,spring boot与MyBatis的使用讲解介绍。spring boot mybatis xml mapper方式的入门和通过一个简...
spring boot又一个spring框架的经典项目,本文讲解spring boot入门的环境配置以及第一个项目,Spring Boot 入门教程
Spring Boot 2.0 Redis整合,通过spring boot 2.0整合Redis作为spring缓存框架的实现。