zoukankan      html  css  js  c++  java
  • SpringBoot学习记录之整合JSP

    虽然SpringBoot不推荐使用JSP,而是推荐使用Thymeleaf,甚至更加推荐流行的前后端分离,使用Vue框架,但是以后说不定能用到,所以在学习的时候记录一下 。直接上代码

    在项目目录下建一个webapp,然后在webapp下建一个jsp,如图:

    image

    pom.xml如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <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/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.1.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
     
        <groupId>com.thr</groupId>
        <artifactId>spring-boot-jsp</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>spring-boot-jsp</name>
        <description>Demo project for Spring Boot</description>
     
        <properties>
            <java.version>1.8</java.version>
        </properties>
     
        <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>
     
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-jasper</artifactId>
                <scope>provided</scope>
            </dependency>
     
            <!-- servlet-api -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
                <scope>provided</scope>
            </dependency>
     
            <!--jsp -->
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>javax.servlet.jsp-api</artifactId>
                <version>2.2.1</version>
                <scope>provided</scope>
            </dependency>
     
            <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
     
        </dependencies>
     
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
     
            <resources>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.xml</include>
                    </includes>
                </resource>
                <resource>
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>**/*.*</include>
                    </includes>
                </resource>
                <resource>
                    <directory>src/main/webapp</directory>
                    <targetPath>META-INF/resources</targetPath>
                    <includes>
                        <include>**/*.*</include>
                    </includes>
                </resource>
            </resources>
     
        </build>
     
    </project>

    ------------------------------------------------------------------------------------

    pom.xml里面注意要添加以下依赖,jsp才能生效。

            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-jasper</artifactId>
                <scope>provided</scope>
            </dependency>
     
            <!-- servlet-api -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
                <scope>provided</scope>
            </dependency>
     
            <!--jsp -->
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>javax.servlet.jsp-api</artifactId>
                <version>2.2.1</version>
                <scope>provided</scope>
            </dependency>
     
            <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>

    下面这段也要在pom.xml里面加上,否则访问不到页面,该段的意思是:配置编译时将web资源也进行编译;在pom.xml文件的<build>节点中新增内容:

            <resources>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.xml</include>
                    </includes>
                </resource>
                <resource>
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>**/*.*</include>
                    </includes>
                </resource>
                <resource>
                    <directory>src/main/webapp</directory>
                    <targetPath>META-INF/resources</targetPath>
                    <includes>
                        <include>**/*.*</include>
                    </includes>
                </resource>
            </resources>

           同时在resources中的application.properties中配置如下:

    server.port=8080
     
    spring.mvc.view.prefix=/
    spring.mvc.view.suffix=.jsp
     
    

    上面的配置非常的重要,如果出错就可能访问出现404。

    下面是Controller的代码:

    @Controller
    public class IndexController {
     
        @RequestMapping(value = "/index",method = RequestMethod.GET)
        public String index(Model model){
            model.addAttribute("msg","Hello SpringBoot JSP");
            return "index";
        }
    }

    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>
     
    ${msg}
     
    </body>
    </html>

    启动项目然后访问http://localhost:8080/index成功显示界面:

    image

    作者: 唐浩荣
    本文版权归作者和博客园共有,欢迎转载,但是转载需在博客的合适位置给出原文链接,否则保留追究法律责任的权利。
  • 相关阅读:
    android AudioManager类详解
    Android的Media(录音,播放音乐,播放视频等)
    加载大图片报OOM错误
    cmd更换编码类型
    通讯录数据库插入数据
    开机自启加入任务栈
    特殊的广播接收者(界面注册)
    将Image的名字转换为R.image.id
    通过XML文件生成View
    判断某张表是否存在
  • 原文地址:https://www.cnblogs.com/tanghaorong/p/14289988.html
Copyright © 2011-2022 走看看