• springboot集成jsp


    以下整合jsp使用的开发工具为intellij idea。其他开发工具目录结构相同

    在pom.xml文件中加入注释部分的依赖

    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
     
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
     
            <!-- 添加servlet依赖模块 -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <scope>provided</scope>
            </dependency>
            <!-- 添加jstl标签库依赖模块 -->
            <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>
            <!-- 使用jsp引擎,springboot内置tomcat没有此依赖 -->
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-jasper</artifactId>
                <scope>provided</scope>
            </dependency>
     
        </dependencies>

    其中最主要的,提供jsp引擎的就是

    tomcat-embed-jasper这个依赖(一定要加)

    然后修改配置文件中的Jsp文件访问路径(视图解析)

    在application.properties文件中加入

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

    配置完成后在webapp/WEB-INF/jsp文件夹下放jsp文件(必须有webapp/WEB-INF这个包,否则访问不到)

    下面是我的项目目录

    最后再建立一个控制器进行访问

    @Controller
    public class IndexController {
        @RequestMapping("/index")
        public String index(){
            return "index";
        }
    }

    访问结果如下,成功显示jsp页面

    也可以这样:

    spring.mvc.view.prefix=/
    spring.mvc.view.suffix=.jsp

    /代表是src/mian下的webapp下,如果需要放在其他目录下,则可以配置如/WEB-INF/jsp/
    紧接着,需要在main下新建一个webapp的文件夹用来存放*.jsp文件(index.jsp)。

    配置了上面信息后,项目运行后还不能访问index.jsp,因为找不到index.jsp,还需要在配置文件中配置资源文件的路径信息。

    <resources>
                <resource>
                    <directory>src/main/webapp</directory>
                    <targetPath>META-INF/resources</targetPath>
                    <includes>
                        <include>**/*.*</include>
                    </includes>
                </resource>
            </resources>
        </build>
  • 相关阅读:
    我的第一篇博客
    1.2 位于Shell脚本第一行的#!
    1.1 一个简单的脚本
    JDK与CGlib动态代理的实现
    解决Sublime Text3中文显示乱码问题
    多线程讲解
    (转)Spring中@Async用法总结
    (转)spring boot注解 --@EnableAsync 异步调用
    Spring Boot中的注解
    (转)如何用Maven创建web项目(具体步骤)
  • 原文地址:https://www.cnblogs.com/wpcnblog/p/11857268.html
Copyright © 2020-2023  润新知