简体   繁体   English

我的项目找不到 jsp 文件,我认为问题出在 application.properties 文件中,但不知道如何解决

[英]My project can't find jsp file, I think the problem is in application.properties file, but dont know how to fix it

My project can't find jsp file.我的项目找不到 jsp 文件。 I try to add in application.properties these rows, but this didn't work我尝试在 application.properties 中添加这些行,但这不起作用

spring.view.prefix=/templates/
spring.view.suffix=.jsp

When i my controller return html page everything is ok, but this doesnt work with jsp This is my application.properties当我的 controller 返回 html 页面时一切正常,但这不适用于 jsp 这是我的应用程序。

spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url=jdbc:sqlserver://localhost\\SQLEXPRESS:1433;databaseName=AdminToolWeb
spring.datasource.username=mssqllogin
spring.datasource.password=mssqllogin$
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql = true
spring.view.prefix=/templates/
spring.view.suffix=.jsp

Тhese are my files in the project. Т这些是我在项目中的文件。 I try to call Movie.jsp我尝试调用 Movie.jsp

The error is错误是

org.thymeleaf.exceptions.TemplateInputException: Error resolving template [Movies], template might not exist or might not be accessible by any of the configured Template Resolvers
    at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:607) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:362) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:189) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1370) ~[spring-webmvc-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1116) ~[spring-webmvc-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1055) ~[spring-webmvc-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:998) ~[spring-webmvc-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:890) ~[spring-webmvc-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) ~[tomcat-embed-core-9.0.12.jar:9.0.12]

and this is my pom.xml这是我的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
            http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zetcode</groupId>
    <artifactId>SpringBootSubmitFormEx</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
    </parent>

    <dependencies>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>6.1.0.jre8</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>



    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

You still seem to use Thymeleaf as template engine.您似乎仍然使用 Thymeleaf 作为模板引擎。 To use JSP templates you need to add tomcat-embed-japser as dependency:要使用 JSP 模板,您需要添加tomcat-embed-japser作为依赖项:

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

Additionally I think the templates need to be in a src/main/webapp folder of your project and not the resources folder.此外,我认为模板需要位于项目的src/main/webapp文件夹中,而不是资源文件夹中。

And as @CostiCiudatu mentioned the properties must be spring.mvc.view.prefix and spring.mvc.view.suffix (with the spring.mvc prefix).正如@CostiCiudatu 提到的,属性必须是spring.mvc.view.prefixspring.mvc.view.suffix (带有spring.mvc前缀)。

You can do this configuration programmatically as well by adding a custom view resolver bean in any of you @Configuration classes:您也可以通过在任何@Configuration类中添加自定义视图解析器 bean 以编程方式进行此配置:

@Bean
public InternalResourceViewResolver viewResolver() {
  final InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
  viewResolver.setPrefix("/templates/");
  viewResolver.setSuffix(".jsp");
  viewResolver.setViewClass(JstlView.class);
  return viewResolver;
}

UPDATE:更新:

After taking a look at the repository it turned out that you still had the Thymeleaf dependency in the pom.xml and thus on the classpath which caused the problems with JSP templates.查看存储库后,发现您在 pom.xml 中仍然有 Thymeleaf 依赖项,因此在导致 Z2D7FCA4F5778E4B40B4606914719BB22 模板问题的类路径上。

I created a pull request that addresses this issue and configures your project as described in this answer https://stackoverflow.com/a/60213442/4323935 as you apparently want to use both JSP and Thymeleaf templates. I created a pull request that addresses this issue and configures your project as described in this answer https://stackoverflow.com/a/60213442/4323935 as you apparently want to use both JSP and Thymeleaf templates.

However - unless there is a very good reason to use multiple template engines - I'd recommend to stick with one template engine only.但是 - 除非有充分的理由使用多个模板引擎 - 我建议只坚持使用一个模板引擎。 Thymeleaf is a reasonable default for Spring Boot and there should be a good reason to diverge from this default. Thymeleaf 是 Spring 引导的合理默认值,应该有充分的理由偏离此默认值。 Using multiple template engines is a non standard use-case and might cause problems in the future (eg, with Spring updates. etc.)使用多个模板引擎是一个非标准用例,将来可能会导致问题(例如,使用 Spring 更新等)

I think you forgot the .mvc.我想你忘记了.mvc. part, those properties are supposed to be:部分,这些属性应该是:

spring.mvc.view.prefix = /templates/
spring.mvc.view.suffix = .jsp

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改我的application.properties文件? - How I can change my application.properties file? 如何解决 eclipse ide 的这个问题? 特别是在 application.properties - How can I fix this problem for the eclipse ide? Specifically in the application.properties 如何在 spring application.properties 中指定我的 ssl 密钥库文件的本地文件路径? - How do I specify local file path to my ssl keystore file in spring application.properties? 如何查看 application.properties 文件中的属性所指的位置? - How can I see where the properties in application.properties file refer to? java 如何从我的战争文件中外部化 application.properties - java how externalise application.properties from my war file 我应该如何为Spring创建一个Java application.properties文件? - How should I create a Java application.properties file for Spring? 如何更改application.properties文件中的配置文件 - How to change profile in application.properties file 如何模拟一个application.properties文件? - How to mock an application.properties file? 如何在 spring web 应用程序中找到 application.properties,虽然位置被提及为 classpath:/application.properties 但文件丢失? - How to find the application.properties in spring web app, while location is mentioned as classpath:/application.properties but file is missing? 为什么我的Java应用程序找不到我的属性文件 - Why can't my Java Application find my properties file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM