Spring Boot + JSP(官方不推荐)

本文详细介绍如何在Spring Boot项目中集成JSP,包括添加依赖、配置视图解析器及编写控制器等内容。通过具体步骤说明,帮助开发者快速上手。

官方为啥不推荐jsp,参考:https://spring.io/blog/2012/10/30/spring-mvc-from-jsp-and-tiles-to-thymeleaf,里面写了很多,其中我觉得最重要的应该是jsp不利于前端开发,如果是jsp页面前端要怎么运行?怎么看写出来的效果?反正各种不利于前端开发吧,spring 推荐Thymeleaf,它的模版文件就是html,可以直接在浏览器打开,闲话不多说,先看非要集成jsp的话该怎么做

1. 在pom.xm中加入支持JSP的依赖

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

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

2. 创建src/main/webapp/WEB-INF/views目录,JSP文件就放这里

 

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello</title>
</head>
<body>
    Hello ${name}
</body>
</html>

 

3. 在src/main/resources/application.properties文件中进行解析器的配置

# MVC
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp

 

4. 编写Controller

@Controller
public class SampleController {
    @RequestMapping("/hello")
    public String getListaUtentiView(ModelMap map){
        map.put("name", "Spring Boot");
        return "home";
    }
}

 5. 编写Application类

@SpringBootApplication
public class WebApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(WebApplication.class);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(WebApplication.class, args);
    }
}

6. 以java application方式运行后,就可以访问http://locahost:8080/hello

注意:在IDE中可以java application方式运行,可以打成war包,需要修改pom中packaging为<packaging>war</packaging>,把war把放入tomcat运行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

椰汁菠萝

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

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

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

打赏作者

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

抵扣说明:

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

余额充值