`
sunbin
  • 浏览: 349484 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Spring Boot 添加JSP支持【转】

 
阅读更多

   大体步骤:

(1)            创建Maven web project

(2)            pom.xml文件添加依赖;

(3)            配置application.properties支持jsp

(4)            编写测试Controller

(5)          编写JSP页面

(6)          编写启动类App.Java

 

 

 

1,FreeMarker
2
,Groovy
3
,Thymeleaf (spring 官网使用这个)
4
,Velocity
5
,JSP (貌似Spring Boot官方不推荐,STS创建的项目会在src/main/resources 下有个templates 目录,这里就是让我们放模版文件的,然后并没有生成诸如 SpringMVC 中的webapp目录)
不过本文还是选择大家都熟悉的JSP来举例,因为使用JSP与默认支持的模版需要特殊处理,所以拿来举例更好。

 

 

1)创建Maven web project

使用Eclipse新建一个Maven Web Project ,项目取名为:

spring-boot-jsp

 

2)在pom.xml文件添加依赖

<!-- spring boot parent节点,引入这个之后,在下面和spring boot相关的就不需要引入版本了; -->

    <parent>

       <groupId>org.springframework.boot</groupId>

       <artifactId>spring-boot-starter-parent</artifactId>

       <version>1.3.3.RELEASE</version>

    </parent>

 

依赖包:

<!-- web支持: 1web mvc; 2restful; 3jackjson支持; 4aop ........ -->

       <dependency>

           <groupId>org.springframework.boot</groupId>

           <artifactId>spring-boot-starter-web</artifactId>

       </dependency>

      

       <!-- servlet 依赖. -->

       <dependency>

           <groupId>javax.servlet</groupId>

           <artifactId>javax.servlet-api</artifactId>

           <scope>provided</scope>

       </dependency>

      

       <!--

           JSTLJSP Standard Tag LibraryJSP标准标签库)是一个不断完善的开放源代码的JSP标签库,是由apachejakarta小组来维护的。JSTL只能运行在支持JSP1.2Servlet2.3规范的容器上,如tomcat 4.x。在JSP 2.0中也是作为标准支持的。

          

           不然报异常信息:

           javax.servlet.ServletException: Circular view path [/helloJsp]: would dispatch back to the current handler URL [/helloJsp] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)

            

        -->

       <dependency>

           <groupId>javax.servlet</groupId>

           <artifactId>jstl</artifactId>

       </dependency>

      

      

      

       <!-- tomcat 的支持.-->

       <dependency>

           <groupId>org.springframework.boot</groupId>

           <artifactId>spring-boot-starter-tomcat</artifactId>

           <scope>provided</scope>

       </dependency>

       <dependency>

           <groupId>org.apache.tomcat.embed</groupId>

           <artifactId>tomcat-embed-jasper</artifactId>

           <scope>provided</scope>

       </dependency>

 

Jdk编译版本:

<build>

       <finalName>spring-boot-jsp</finalName>

       <plugins>

           <plugin>

              <artifactId>maven-compiler-plugin</artifactId>

              <configuration>

                  <source>1.8</source>

                  <target>1.8</target>

              </configuration>

           </plugin>

       </plugins>

    </build>

 

3application.properties配置

上面说了spring-boot 不推荐JSP,想使用JSP需要配置application.properties 
添加src/main/resources/application.properties内容:

页面默认前缀目录

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

响应页面默认后缀

spring.mvc.view.suffix=.jsp

自定义属性,可以在Controller中读取

application.hello=Hello Angel From application

 

4)编写测试Controller

编写类:com.kfit.jsp.controller.HelloController:

package com.kfit.jsp.controller;

 

import java.util.Map;

 

import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

 

/**

 * 测试

 * @author Angel(QQ:412887952)

 * @version v.0.1

 */

@Controller

public class HelloController {

      

       //  application.properties 中读取配置,如取不到默认值为Hello Shanhy

    @Value("${application.hello:Hello Angel}")

    private String hello;

   

      

       @RequestMapping("/helloJsp")

       public String helloJsp(Map<String,Object> map){

              System.out.println("HelloController.helloJsp().hello="+hello);

              map.put("hello", hello);

              return "helloJsp";

       }

}

 

5)编写JSP页面

 src/main 下面创建 webapp/WEB-INF/jsp 目录用来存放我们的jsp页面helloJsp.jsp

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

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

    helloJsp

    <hr>

    ${hello}

   

</body>

</html>

 

 

(6)编写启动类

编写App.java启动类:

package com.kfit.jsp;

 

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.context.web.SpringBootServletInitializer;

 

@SpringBootApplication

public class App extends SpringBootServletInitializer {

//    @Override

//    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

//           return application.sources(App.class);

//    }

      

       public static void main(String[] args) {

              SpringApplication.run(App.class, args);

       }

}

 

右键Run As  Java Application访问:http://127.0.0.1:8080/helloJsp 可以访问到:

helloJsp


Hello Angel From application

 

特别说明:针对el表达式,类似${hello}这个对于servlet的版本是有限制的,2.4版本版本以下是不支持的,是无法进行识别的,请注意。

分享到:
评论
3 楼 sunbin 2016-12-14  
西蜀石兰 写道
关键的是controller建立了一个中转吧,这样所有的jsp文件都需要在controller中建立中转,感觉好麻烦。

是的,但是这样也使得系统更加安全。
2 楼 czwanglei 2016-12-03  
这篇文章写的真不错。。
1 楼 西蜀石兰 2016-10-28  
关键的是controller建立了一个中转吧,这样所有的jsp文件都需要在controller中建立中转,感觉好麻烦。

相关推荐

    Spring boot访问jsp页面

    然而,Spring Boot默认并不支持JSP视图解析,因为它的设计理念是推崇RESTful API和使用模板引擎如Thymeleaf或Freemarker。但如果你的项目需要使用JSP,可以通过以下步骤进行配置: 1. 添加`spring-boot-starter-web...

    spring boot 整合jsp与mybatis

    在Spring Boot中集成JSP,我们需要确保`spring-boot-starter-web`依赖已经被包含,因为这个起步依赖包含了对Servlet容器的支持,同时也要配置好静态资源和视图解析器路径。 MyBatis 是一个轻量级的持久层框架,它将...

    19. [视频]Spring Boot 添加JSP支持【从零开始学Spring Boot】

    通过以上步骤,你已经成功地在Spring Boot应用中添加了JSP支持。请注意,由于JSP编译成Servlet,所以首次访问JSP页面可能会稍有延迟。此外,虽然Spring Boot不推荐使用JSP,但在某些场景下,如维护旧项目或与已有的...

    spring boot mybatis jsp

    对于JSP的集成,Spring Boot默认不支持JSP,但可以通过添加Tomcat JSP支持的依赖,并配置相关属性,使其能够在Spring Boot应用中运行。在Controller层,我们可以使用`ModelAndView`对象返回JSP页面,或者通过`@...

    spring boot jsp mvc jpa hibernate mysql 示例 sample

    要启用 JSP 支持,需要添加相应的依赖并配置应用的 classpath 路径。 3. **JPA (Java Persistence API)**:JPA 是 Java 平台上的 ORM(对象关系映射)标准,它提供了统一的 API 来操作数据库。JPA 允许开发者用面向...

    spring boot 支持 jsp 的源代码

    在 Spring Boot 中,默认并不支持 JSP,因为它的默认视图解析器是 Thymeleaf、Freemarker 或者 Velocity 等模板引擎,而不是传统的 JSP。但是,通过一些配置,我们可以让 Spring Boot 项目支持 JSP。 首先,要理解...

    spring boot +jsp.rar

    1. **添加JSP支持**:Spring Boot默认不包含对JSP的支持,因为它的设计理念是倾向于使用模板引擎如Thymeleaf或Freemarker。要使用JSP,你需要引入`spring-boot-starter-web`依赖,并在`pom.xml`或`build.gradle`中...

    Spring Boot with JSP

    - **配置JSP支持**:Spring Boot默认不支持JSP,需要手动添加对JSP的支持。在`pom.xml`中加入`spring-boot-starter-tomcat-jasper`依赖,确保Tomcat能够解析JSP文件。 ```xml &lt;groupId&gt;org.apache.tomcat.embed ...

    idea +spring boot +jsp

    4. **添加JSP支持**:虽然Spring Boot默认不推荐使用JSP,但可以通过以下步骤添加支持: - 在`pom.xml`文件中添加`spring-boot-starter-jsp`依赖。 - 配置`application.properties`,指定JSP的视图解析器,如`...

    在IDEA中构建Spring Boot + JSP项目

    通过这个项目,你可以学习到Spring Boot的快速启动特性、如何在IDEA中创建项目、配置JSP支持以及编写简单的Controller和JSP页面。这只是一个基础的起点,Spring Boot还有许多高级特性,如自动配置、Actuator、Spring...

    spring boot之jsp整合

    在`pom.xml`文件中,我们需要引入Tomcat的JSP支持。Spring Boot默认集成了嵌入式的Tomcat服务器,所以只需要添加`tomcat-embed-jasper`依赖即可: ```xml &lt;groupId&gt;org.apache.tomcat.embed &lt;artifactId&gt;...

    maven spring-boot jsp

    在IT行业中,Maven、Spring Boot和JSP是三个非常重要的技术组件,它们共同构建了现代Web应用程序的基础架构。下面将详细阐述这三个技术及其在给定项目中的应用。 首先,Maven是一个项目管理工具,主要用于Java项目...

    spring boot WEB jsp普通项目例子.zip

    要启用JSP支持,我们需要进行一些额外的配置,例如: - 在`pom.xml`中添加对`spring-boot-starter-tomcat-jasper`的依赖: ```xml &lt;groupId&gt;org.springframework.boot &lt;artifactId&gt;spring-boot-starter-tomcat-...

    详解Spring Boot 添加JSP支持

    下面我将详细解析如何在Spring Boot项目中添加JSP支持,这包括创建Maven Web项目、在pom.xml文件中添加依赖、配置application.properties文件、编写测试Controller、创建JSP页面和编写启动类等步骤。 1. 创建Maven ...

    从零开始学Spring Boot

    1.22 Spring Boot 添加JSP支持 1.23 Spring Boot Servlet 1.24 Spring Boot过滤器、监听器 1.25 Spring Boot 拦截器HandlerInterceptor 1.26 Spring Boot启动加载数据CommandLineRunner 1.27 Spring Boot环境变量...

    Spring-boot jsp demo

    3. **配置 JSP 支持**:由于 Spring Boot 默认不推荐使用 JSP,因此我们需要手动配置。首先,在 pom.xml 文件中添加 tomcat-embed-jasper 和 jstl 依赖。然后,在 application.properties 配置文件中启用 JSP 视图...

    spring boot 42讲配套源码.zip

    第 2-2 课 Spring Boot 项目中使用 JSP/spring-boot-jsp 第 2-3 课 模板引擎 Thymeleaf 基础使用/spring-boot-thymeleaf 第 2-4 课 模板引擎 Thymeleaf 高阶用法/spring-boot-thymeleaf 第 2-5 课 Thymeleaf 页面...

    Spring boot +jsp+mysql空白模板 解决刚开始试用springboot +jsp页面跳转404的问题

    首先,我们需要理解Spring Boot的默认配置并不直接支持JSP作为视图技术。Spring Boot更倾向于使用Thymeleaf、Freemarker或者Mustache等模板引擎。但是,如果项目需求是使用JSP,我们需要手动配置。 1. 添加JSP依赖...

    spring-boot 整合 jsp jdk1.6版本 可用

    Spring Boot以其便捷的“开箱即用”特性深受开发者喜爱,但默认情况下它并不直接支持JSP视图解析。然而,通过一些配置调整和额外的步骤,我们可以在不升级JDK的情况下,在Spring Boot项目中运行JSP页面。 首先,...

    Spring+jsp老项目转Springboot的示例Demo

    Spring Boot默认不支持JSP,但可以通过添加Tomcat和Jasper依赖,并配置一个JspServlet来实现对Jsp的支持。 以下是转换的关键步骤: 1. **添加依赖**:在`pom.xml`中,除了引入Spring Boot的父POM外,还需要引入`...

Global site tag (gtag.js) - Google Analytics