【发布时间】: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