【问题标题】:The controller see .html page but not .jsp控制器看到 .html 页面但没有看到 .jsp
【发布时间】:2018-04-27 10:41:37
【问题描述】:

我是春天世界的新手, 所以我研究了一个 HelloWorld 示例,我编写了以下简单的控制器:

Controller
@RequestMapping("/welcome")
public class HelloWorldController{

   @RequestMapping(method = RequestMethod.GET)
   public ModelAndView helloWorld(){

    ModelAndView model = new ModelAndView("MVC_First_Page");
    model.addObject("msg", "hello world");

    return model;
  }

我收到以下错误:

 Error resolving template "error", template might not exist or might not be accessible by any of the configured Template Resolvers
Caused by: org.thymeleaf.exceptions.TemplateInputException: 

解析模板“错误”时出错,模板可能不存在或可能无法被任何已配置的模板解析器访问

为什么拦截器看不到 MVC_First_Page.jsp 页面?我已将其扩展名更改为 .html 并且可以正常工作。

我应该将此属性添加到 application.properties 文件吗?

  spring.mvc.view.prefix: 
  spring.mvc.view.suffix: .jsp

【问题讨论】:

标签: spring-mvc spring-boot


【解决方案1】:

如果您不需要 Thymeleaf(因为您只是在试验),最简单的方法是只使用 jsp 视图解析器。

你可以在这里找到一个很好的例子:https://hellokoding.com/spring-boot-hello-world-example-with-jsp/

基本上,在您的代码中,您需要从 pom.xml 中删除 Thymeleaf 并添加 jasper 依赖项:

    <!--<dependency>-->
        <!--<groupId>org.springframework.boot</groupId>-->
        <!--<artifactId>spring-boot-starter-thymeleaf</artifactId>-->
    <!--</dependency>-->
  <dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
  </dependency>

然后你必须在application.properties文件中指定视图后缀:

spring.mvc.view.suffix=.jsp

最后,您需要将 JSP 文件移动到 src/main/webapp 目录

【讨论】:

  • 我还应该在 pom.xml 中添加以下依赖项:javax.servletjstl1.2依赖>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多