Spring Boot简单应用集成jsp-3

Spring Boot集成jsp页面

1.指定存放web资源目录

集成jsp页面需要在src/main下创建webapp目录以存放jsp文件,须在Project Structor中指定该目录为资源目录

 二.使用物理视图访问jsp

1.引入jasper依赖

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

2.注册资源目录

<build>       
 <!-- 注册资源目录 -->
        <resources>
            <resource>
                <directory>src/main/webapp</directory>
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
        </resources>
</build>

3.创建jsp和controller看效果

新建一个index.jsp页面

<%--
  Created by IntelliJ IDEA.
  User: zhouzhongyi
  Date: 2021-09-06
  Time: 9:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8">
    <title>This my index</title>
</head>
<body>
<form action="test/myinfo" method="post">
    姓名:<input type="text" id="name" name="name"/> <br>
    年龄:<input type="text" id="age" name = "age"/>
    <input type="submit" value="提交">
</form>
</body>
</html>

新建一个myinfo.jsp页面

<%--
  Created by IntelliJ IDEA.
  User: zhouzhongyi
  Date: 2021-09-06
  Time: 10:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8">
    <title>My Info</title>
</head>
<body>
    name = ${name}<br>
    age = ${age}
</body>
</html>

controller控制器

 启动执行效果

 三、使用逻辑视图访问jsp

yml中需增加视图配置

 controller只需更改返回的信息为如下图即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我为你提供一个简单Spring Boot集成JSP的示例项目,你可以参考一下: 1. 创建一个Maven项目,pom.xml文件中添加以下依赖: ```xml <!-- Spring Boot Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Tomcat Embed Jasper --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <!-- JSTL --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> ``` 2. 在src/main目录下创建一个webapp目录,然后在webapp目录下创建一个jsp目录,用于存放JSP文件。 3. 在src/main/java目录下创建一个Spring Boot的配置类,例如: ```java @Configuration @ComponentScan @EnableAutoConfiguration public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 4. 在src/main/java目录下创建一个Controller类,例如: ```java @Controller public class HomeController { @RequestMapping("/") public String home(Model model) { model.addAttribute("message", "Hello, World!"); return "home"; } } ``` 5. 在src/main/resources目录下创建一个application.properties文件,用于配置应用程序的属性,例如: ``` spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp ``` 6. 运行应用程序,访问http://localhost:8080/,会显示Hello, World!。 以上就是一个简单Spring Boot集成JSP的示例项目,你可以在此基础上进行开发。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

意向天开

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值