SpringBoot 中使用JSP

今天,在SpringBoot 中用了下 JSP , 总是不能跳转到正常页面。

依赖:

<!-- servlet依赖. -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>

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

        <!-- tomcat的支持.-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

配置文件 application.properties:

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

控制器:

@Controller
public class HelloController {

    @RequestMapping("/hello")
    public String hello(Model model){
        model.addAttribute("now",DateFormat.getDateTimeInstance().format(new Date()));
        return "hello";
    }
}

hello.jsp :

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    hi jsp 现在时间是 ${now}
</body>
</html>

目录结构:

最终发现了原因:之前我在这个项目上测了 thymeleaf 模板的例子,添加了 thymeleaf 依赖,导致和 jsp 模板引擎冲突了:

thymeleaf 依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

解决方法:我把这个依赖从 pom.xml 里删除,再次运行访问,ok

总结: 模板引擎 依赖不能冲突,当前项目我是把 jsp thymeleaf 依赖全都加进去了,我本来以为我加进去 不会混淆,因为只是加了个jar ,具体用哪个还是配置文件里说了算,但是项目跑起来就冲突了,莫名其妙。

猜你喜欢

转载自blog.csdn.net/qq_33378853/article/details/87611716
今日推荐