【问题标题】:Maven Module with Spring Boot带有 Spring Boot 的 Maven 模块
【发布时间】:2016-03-31 08:50:47
【问题描述】:

我正在尝试将 Maven 配置为使用带有多个模块的 Spring Boot,这是我的结构:

- Parent
------ WebClient
------------ scr/main/java/config ---> Config Files
------------ scr/main/java/resources/WEB-INF ---> Template Files
------ Core
------ Services
------ Server

我有一个放置 ViewResolver 的配置文件:

@Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter  {

@Bean
public ViewResolver setupViewResolver() {
    // View Resolver
    UrlBasedViewResolver viewResolver = new UrlBasedViewResolver();
    viewResolver.setPrefix("/WEB-INF/");
    viewResolver.setSuffix(".jsp");
    viewResolver.setViewClass(JstlView.class);
    return viewResolver;
}

这是我的父 pom.xml 模块和依赖项:

<modules>
    <module>core</module>
    <module>services</module>
    <module>server</module>
    <module>webclient</module>
</modules>


<dependencyManagement>
    <dependencies>
        <!-- =========================== Spring ======================= -->

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

        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-core</artifactId>
            <version>2.2.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session</artifactId>
            <version>1.1.0.RELEASE</version>
        </dependency>

        <!-- Spring Security and MVC dependences -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>4.1.0.RC1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>4.1.0.RC1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <version>8.5.0</version>
        </dependency>
    </dependencies>

</dependencyManagement>

在我的服务器的 POM 文件中,我有这个依赖项:

<dependencies>
    <dependency>
        <groupId>mygroup</groupId>
        <artifactId>core</artifactId>
    </dependency>

    <dependency>
        <groupId>mygroup</groupId>
        <artifactId>services</artifactId>
    </dependency>

    <dependency>
        <groupId>mygroup</groupId>
        <artifactId>webclient</artifactId>
    </dependency>

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

    <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session</artifactId>
    </dependency>

我的 SpringBootApplication 如下所示:

@SpringBootApplication
@ComponentScan({"config"})
public class ServerRunner {

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

当我启动我的应用程序时,我看到我的映射工作正常,似乎一切正常,但问题是 Spring 找不到我的模板:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
There was an unexpected error (type=Not Found, status=404).
/WEB-INF/index.jsp

我的配置有什么问题?我应该把我的模板文件放在哪里? 感谢您的帮助!

已编辑:

如果我将模板文件放在服务器项目 (server\src\main\webapp) 中,它就可以工作!那么...如何让服务器读取 webclient 项目模板?我需要那个子模块中的模板。

编辑 2: 已解决避免“jsp”文件,请参阅答案和 cmets

【问题讨论】:

  • 您收到的消息是警告消息。它说您的视图渲染器遇到错误并触发重定向到未实现的默认映射/error。真正的错误是触发此重定向的错误。在此消息之前向我们提供您的日志详细信息。
  • 我的服务器控制台没有任何错误:2016-03-31 11:05:14.562 INFO 8824 --- [main] server.ServerRunner : Started ServerRunner in 4.628 seconds (JVM running for 5.379)
  • 我认为错误是错误页面上打印的Not Found
  • 服务器的pom是什么意思?您的服务器的 pom 是否继承自父 POM?作为建议,在使用 Boot 时不要使用 Spring 项目特定的版本。只需从 Spring Boot 的父 pom 继承并将版本号留空,Spring 会处理它们。
  • 小心你在webapps 文件中的内容。 Do not use the src/main/webapp directory if your application will be packaged as a jar. Although this directory is a common standard, it will only work with war packaging and it will be silently ignored by most build tools if you generate a jar.您可以在以下位置找到更多信息:http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-spring-mvc-static-content

标签: spring maven spring-mvc spring-boot


【解决方案1】:

我认为这与 JSP limitations described in Boot reference documentation 有关 - 您不能只从类路径中的任何位置读取 JSP(而这适用于其他模板引擎)。

【讨论】:

  • 感谢您的回答布赖恩!我不知道这个限制。但我想我需要别的东西......如果我改变我的 ViewResolver 并尝试提供一个 html 模板文件,我会得到同样的错误Not Found: /WEB-INF/index.html
  • 你可以尝试不使用自定义解析器,不使用自定义配置(只是一个百里香启动器和src/main/resources/templates 中的一个模板?正如 Drew1208 所指出的,webapp 位置可能是一个问题
  • 如果我把我的模板文件放在server/src/main/resources/templates thymeleaf 可以找到它并且它可以工作......但是如果我把文件放在webclient/src/main/resources/templates 仍然无法工作(template might not exist or might not be accessible by any of the configured Template Resolvers)我需要我的“webclient”项目中的模板文件,有什么解决方案吗?
  • 确保您的客户端 JAR 将这些模板放在正确的位置 (classes/templates?) 并且您的 Web 项目依赖于您的客户端 jar 的正确版本(您可以 jar -tvf 客户端 jar包含在您的可执行 jar/war 中)
猜你喜欢
  • 1970-01-01
  • 2014-08-26
  • 2017-05-31
  • 1970-01-01
  • 1970-01-01
  • 2017-07-06
  • 1970-01-01
  • 2014-01-10
  • 2019-08-26
相关资源
最近更新 更多