zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    npm install --save
    ajax总结
    javascript学习资料
    前端工具学习资料
    php学习资料
    Bootstrap学习资料
    css学习资料
    Express搭建一个Node项目
    网站性能优化
    POJ 1862 Stripies【哈夫曼/贪心/优先队列】
  • 原文地址:https://www.cnblogs.com/wpcnblog/p/11857268.html
Copyright © 2011-2022 走看看