spring boot 二十一 集成 jsp

该博客介绍了Web项目搭建的步骤。首先要添加依赖并配置路径,接着创建src/main/webapp目录并编写jsp文件,最后编写controller和配置文件application.properties。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步加依赖,:
配路径

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
... ...
    <build>
        <resources>
            <resource>
                  <!-- 指定编译打包时 将 src/main/webapp 目录下的 **/*.jsp 文件打包到  META-INF/resources 文件夹下 -->
                <directory>src/main/webapp</directory>
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>**/*.jsp</include>
                </includes>
            </resource>
        </resources>
    </build>

第二步:
创建目录 src/main/webapp 编写jsp 文件

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%
    String path = request.getContextPath();
    String basepath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
    <base href="<%=basepath %>"/>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Insert title here</title>
</head>
<body>
   我的名字叫 ${name}
</body>
</html>

第三步:编写 controller

@Controller
public class JspController {
    //如果想在 @Controller 中返回json 需要使用 @ResponseBody 注解
    @RequestMapping("getName")
    public @ResponseBody Map getName(){
        Map map = new HashMap();

        map.put("id","11");
        map.put("name","zll");
        return map;
    }

    @RequestMapping("/getJsp")
    public String getJsp(Model model){
        model.addAttribute("name","zll");
        return "index";
    }
}

编写配置文件 application.properties

#/斜杠代表相对路径 等价于 src/main/webapp
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值