英文:
JSP Spring Boot works when running from IntelliJ, but empty page when running WAR
问题
我在我的Spring Boot应用程序中遇到了一个问题:在IntelliJ中运行或使用mvn clean spring-boot:run
时,它正常工作。
但是,当我使用mvn package
进行打包后,它会提供一个空白页面,忽略了我的JSP文件。
我使用war打包。我计划使用内嵌的Tomcat运行项目。
我的pom.xml:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<packaging>war</packaging>
<!-- 其他内容略 -->
</project>
我的主类:
@SpringBootApplication
public class SomeWebsiteApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SomeWebsiteApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(SomeWebsiteApplication.class, args);
}
}
控制器:
@GetMapping({"/", "/hello"})
public String hello(final Model model,
@RequestParam(value="name",
required=false,
defaultValue="World") final String name) {
model.addAttribute("name", name);
log.info("called a home page");
return "index";
}
我的JSP文件(位于src/main/webapp/WEB-INF/jsp/index.jsp
):
<!DOCTYPE html>
<html class="no-js" lang="en">
<head>
<!-- 其他内容略 -->
</head>
<body>
<div class="pageWithoutFooter">
<!-- 其他内容略 -->
</div>
</body>
</html>
Web配置:
@ComponentScan
public class WebConfig implements WebMvcConfigurer {
@Bean
public InternalResourceViewResolver getInternalResourceViewResolver(){
final InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
}
我花了几个小时试图找到解决办法,但没有成功。
英文:
I have an issue with my spring boot app: it works fine when running form IntelliJ or with mvn clean spring-boot:run
.
However when I package it with man package
it serves an empty page ignoring my jsps.
I'm packaging with war. I plan to run the project with embedded tomcat
my 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<packaging>war</packaging>
<groupId>com.some</groupId>
<artifactId>letdeskWebsite</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>some Website</name>
<description>some Website</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
<exclusions>
<exclusion>
<artifactId>servlet-api</artifactId>
<groupId>javax.servlet</groupId>
</exclusion>
<exclusion>
<artifactId>jsp-api</artifactId>
<groupId>javax.servlet.jsp</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/webapp</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<warSourceDirectory>target</warSourceDirectory>
<webXml>src\main\webapp\WEB-INF\web.xml</webXml>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
My main class:
@SpringBootApplication
public class SomeWebsiteApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SomeWebsiteApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(SomeWebsiteApplication.class, args);
}
}
controller:
@GetMapping({"/", "/hello"})
public String hello(final Model model,
@RequestParam(value="name",
required=false,
defaultValue="World") final String name) {
model.addAttribute("name", name);
log.info("called a home page");
return "index";
}
}
my jsp (located at src/main/webapp/WEB-INF/jsp/index.jsp
):
<!DOCTYPE html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello ${name}!</title>
<meta class="foundation-mq">
<link rel="stylesheet" href="css/app.css">
</head>
<body>
<div class="pageWithoutFooter">
<jsp:include page="NavigationElements/topNavigation.jsp"/>
<h2 class="hello-title">Hello ${name}!</h2>
<input type="button" value="Hi there!">
</div>
<jsp:include page="NavigationElements/footerNavigation.jsp"/>
</body>
</html>
webconfig:
@ComponentScan
public class WebConfig implements WebMvcConfigurer {
@Bean
public InternalResourceViewResolver getInternalResourceViewResolver(){
final InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
I had spent hours trying to find the solution but for no avail
答案1
得分: 1
Spring Boot是一个约定优于配置的框架,而您现在并未遵循通常的约定。如果您计划使用内嵌的Tomcat运行,您应该使用spring-boot:repackage
插件,而不是maven-war-plugin
。这也意味着spring-boot-starter-tomcat
必须是compile
,您的pom.xml
中有很多东西是不需要的。
您当前的设置过于复杂,因为您正在构建一个应该部署到现有Tomcat中的WAR。您可能希望从简单的内容重新开始:
- 使用https://start.spring.io/ 生成一个带有Spring Web依赖的基本Maven项目。
- 迁移您现有的代码并包含JSP页面。
- 打包为带有内嵌Tomcat的可运行JAR。
- 验证JSP页面是否能够正确提供。
英文:
Spring Boot is a convention over configuration framework and you are not following the usual conventions now. If you plan to run with embedded Tomcat you should use spring-boot:repackage
mojo instead of maven-war-plugin
. This also means that spring-boot-starter-tomcat
has to be compile
and you don't need quite a few things from your pom.xml
.
Your current setup is overcomplicated because you are building a WAR that should be deployed into an existing Tomcat. You might want to start fresh with something simpler.
- Use https://start.spring.io/ to generate a basic Maven project with Spring Web dependency.
- Move your current code and include the JSPs
- Package as running JAR with embedded Tomcat
- Check that JSPs are served correctly
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论