【发布时间】:2018-01-17 14:33:52
【问题描述】:
我有一个 springboot 应用程序,在本地运行时可以很好地为网页提供服务,但由于某种原因,当我将部署 war 文件打包到生产环境时返回 404,我收到 404 错误。在我的控制器中,我返回一个 jsp 页面,其中包含来自我的 /resources/static 文件夹的 html 页面。这一切都在本地运行良好。
我正在部署到 tomcat 7.0.52
这是我的pom
<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>demo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<spring.version>4.3.9.RELEASE</spring.version>
<java.version>1.8</java.version>
<maven-compiler.version>3.6.1</maven-compiler.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!-- //////////////////////////////////////////////////////// -->
<!-- SPRING -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>7.0.19</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
<build>
<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.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
`
我的 Springboot 应用的主类
@SpringBootApplication
public class SpringbootApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringbootApplication.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringbootApplication.class, args);
}
}
我的扩展 webmvcConfigureAdapter 的类
@Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{
@Override
public void addViewControllers(ViewControllerRegistry registry) {
System.out.println("in mvc config");
registry.addViewController("/").setViewName("forward:index2.html");
}
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
System.out.println("configure serve handling");
configurer.enable();
}
@Bean
InternalResourceViewResolver internalResourceViewResolver () {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/jsp/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
我的主控制器。
@Controller
public class MainController {
@RequestMapping(value={"/"}, method = RequestMethod.GET)
public String index(){
System.out.println("inside mainController");
return "chatbot";
}
}
最后我的 application.properties 文件看起来像这样。
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
我的项目结构是这样的……
src/main/webapp
-> assets
-> resources
-> static ->index.html
-> WEB-INF
->jsp -> index.jsp
请帮忙。我已经坚持了8个多小时了。 谢谢。
【问题讨论】:
-
您要访问的网址是什么?
-
类似 111.###.###.###:8082/demo(战争文件的名称)
-
尝试将index.html放到webapp下
-
嘿。这实际上有效。如果我想组织我的 html 页面,我只需将它们放在 webapp 下的静态文件夹中?
标签: java maven spring-mvc spring-boot tomcat7