我有一个springBoot应用程序,它使用Thymeleaf来渲染模板。当我从Eclipse运行应用程序时,一切都很完美。但是,当我尝试构建jar并运行它时,一个模板拒绝加载以下异常:
异常处理模板" / XXX / form / importConfiguration":错误 解析模板" / XXX / form / importConfiguration",模板可能不会 任何已配置的模板都存在或可能无法访问 解析器
控制器代码非常简单:
@Controller
public class ImportConfigurationsController extends BaseController
{
private static final Logger log = LoggerFactory.getLogger(ImportConfigurationsController.class);
private static final String PATH_IMPORT_CONFIG = "/XXX/importConfigurations";
private static final String PATH_INIT_IMPORT_CONFIG = "/XXX/initLoadingXMLConfiguration";
private static final String PATH_IMPORT_CONFIG_FROM_XML = "/XXX/importConfigurationFromXML";
private static final String TEMPLATE_IMPORT_CONFIG = "/XXX/form/importConfiguration";
/**
* Loads the import configuration page
*
* @param model
* @return
* @throws Exception
*/
@GetMapping(PATH_IMPORT_CONFIG)
public String importConfiguration(Model model) throws Exception
{
log.trace("Enter Method importConfiguration. Params: {}", model);
log.trace("Return Method importConfiguration.");
return TEMPLATE_IMPORT_CONFIG;
}
我检查了jar文件,html文件确实存在。此外,除此之外的所有其他模板都正常运行。任何人都可以帮我解决这个问题吗?
编辑: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>XXXX</groupId>
<artifactId>XXXX</artifactId>
<version>1.7</version>
<properties>
<jar.filename>XXXX-${version}</jar.filename>
<java.version>1.7</java.version>
<commons.lang3-version>3.1</commons.lang3-version>
<mybatis-version>3.4.1</mybatis-version>
<ojdbc-version>11.2.0.4</ojdbc-version>
<guava-version>19.0</guava-version>
<commons-configuration-version>1.6</commons-configuration-version>
<commons-jxpath-version>1.3</commons-jxpath-version>
<javax-version>2.1</javax-version>
<project.branch>trunk</project.branch>
<deploy.dir>${project.basedir}/deploy</deploy.dir>
<dist.dir>${deploy.dir}/XXXX/${project.branch}</dist.dir>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<build>
<finalName>${jar.filename}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<!-- Run: runs Ant tasks for Maven -->
<id>copy-extra-files</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<!-- Ant targets to be executed -->
<target name="build-distribution">
<echo message="Deleting ${dist.dir} folder" />
<delete dir="${dist.dir}" />
<echo message="Copying application scripts to ${dist.dir} folder" />
<copy todir="${dist.dir}/application">
<fileset dir="scripts" />
</copy>
<echo message="Copying SQL scripts to ${dist.dir}/sql folder" />
<copy todir="${dist.dir}/sql">
<fileset dir="sql" />
</copy>
<echo message="Copying config to ${dist.dir}/config folder" />
<copy todir="${dist.dir}/application/config">
<fileset dir="src/main/resources"
includes="application.properties, messages.properties, log4j2.xml" />
</copy>
<echo message="Copying application's packaged jar file" />
<copy todir="${dist.dir}/application" verbose="true">
<fileset file="target/${jar.filename}.jar" />
</copy>
</target>
</configuration>
</execution>
<execution>
<id>copy-extra-files</id>
<phase>none</phase>
</execution>
</executions>
<inherited>false</inherited>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-el</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons.lang3-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>${ojdbc-version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis-version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava-version}</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>${javax-version}</version>
</dependency>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>${commons-configuration-version}</version>
</dependency>
<dependency>
<groupId>commons-jxpath</groupId>
<artifactId>commons-jxpath</artifactId>
<version>${commons-jxpath-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
更新:
控制器本身似乎有问题。当我尝试在控制器中返回另一个模板的名称,在另一个控制器中完全加载时,我得到了相同的异常。所以出于某种原因,Thymeleaf似乎无法从该控制器加载模板。
答案 0 :(得分:0)
抱歉浪费每个人的时间。像往常一样,事实证明,为什么我遇到这个问题有一个愚蠢的简单原因。在创建新控制器时,似乎我们监督模板路径中有一个前导“/”。因此,路径不是“XXX / path / to / file”,而是“/XXX/path/to.file”。
我加上这个是为了其他可能遇到这个问题的人的利益。
在事后的想法中,我认为应该在某处突出显示,因为错误信息远非描述性的。前导“/”不应该阻止模板加载,如果是这样,错误应该至少表明出错了。