zoukankan      html  css  js  c++  java
  • spring boot集成jsp

    我们在使用spring boot进行web项目开发的时候,可能会选择页面用jsp。spring boot默认使用的html的,现在我们来看下如何集成jsp页面进行开发。

    1.pom.xml文件引入所需依赖

    我们要在pom.xml文件中引入jsp页面所需要的jar包,如下:

    <!-- springboot支持jsp -->
            <!--引入springboot内嵌的tomcat对jsp的解析包-->
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-jasper</artifactId>
            </dependency>
            <!-- servlet依赖的jar包 -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
            </dependency>
            <!-- jsp依赖的jar包 -->
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>jsp-api</artifactId>
                <version>2.2</version>
            </dependency>
            <!-- jstl标签依赖的jar包 -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
            </dependency>

    2.配置文件设置前端视图展示为jsp

    在application.properties文件中配置前端视图页面展示为jsp页面,

    #前端视图展示jsp页面
    #springmvc的前缀,页面的所在位置
    spring.mvc.view.prefix=/
    #springmvc的后缀
    spring.mvc.view.suffix=.jsp

    3.编写一个jsp页面

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>首页</title>
    </head>
    <body>
    
    ${msg}
    </body>
    </html>

    我们写一个简单的jsp页面,里面body中打印了一个变量msg。

    4.编写一个访问控制器

    package com.example.demo.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    
    @Controller
    public class JspController {
    
        @GetMapping("/index")
        public String index(Model model){
    
            model.addAttribute("msg","spring boot集成jsp");
            return "index";
        }
    }

    在这里,应为需要返回页面,所以我们用的注解是@Controller。@RestController注解是返回json格式的字符串。相当于@Controller + @ResponseBody。然后启动项目,访问地址http://127.0.0.1:8088/demo/index,效果如下:

    这里我们需要注意下,如果访问的时候,获取不到jsp页面,那么我们需要在pom.xml文件进行修改编译jsp页面后的位置。在build标签下增加resources部分,如下:

        <resources>
                <!--  将xml也编译 -->
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.xml</include>
                    </includes>
                </resource>
                <resource>
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>**/*.*</include>
                    </includes>
                </resource>
    
                <!-- src/main/webapp目录下所有的文件编译到 META-INF/resource下 -->
                <resource>
                    <directory>src/main/webapp</directory>
                    <targetPath>META-INF/resource</targetPath>
                    <includes>
                        <include>**/*.*</include>
                    </includes>
                </resource>
            </resources>
  • 相关阅读:
    两个有序链表的合并
    Perl学习笔记
    生信-基本概念
    生信-序列比较dp[未完成]
    PAT 1091 Acute Stroke [难][bfs]
    PAT 1038 Recover the Smallest Number[dp][难]
    PAT 1078 Hashing[一般][二次探查法]
    PAT 1122 Hamiltonian Cycle[比较一般]
    PAT 1151 LCA in a Binary Tree[难][二叉树]
    PAT 1148 Werewolf
  • 原文地址:https://www.cnblogs.com/wanghq1994/p/12119818.html
Copyright © 2011-2022 走看看