JSP Spring Boot在IntelliJ中运行正常,但将WAR部署后页面为空。

huangapple go评论53阅读模式
英文:

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:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;parent&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
        &lt;version&gt;2.3.3.RELEASE&lt;/version&gt;
        &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
    &lt;/parent&gt;
    &lt;packaging&gt;war&lt;/packaging&gt;
    &lt;groupId&gt;com.some&lt;/groupId&gt;
    &lt;artifactId&gt;letdeskWebsite&lt;/artifactId&gt;
    &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
    &lt;name&gt;some Website&lt;/name&gt;
    &lt;description&gt;some Website&lt;/description&gt;

    &lt;properties&gt;
        &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
        &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
        &lt;java.version&gt;11&lt;/java.version&gt;
    &lt;/properties&gt;
    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
            &lt;artifactId&gt;jstl&lt;/artifactId&gt;
            &lt;scope&gt;provided&lt;/scope&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.tomcat.embed&lt;/groupId&gt;
            &lt;artifactId&gt;tomcat-embed-jasper&lt;/artifactId&gt;
            &lt;scope&gt;provided&lt;/scope&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
            &lt;scope&gt;provided&lt;/scope&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
            &lt;artifactId&gt;lombok&lt;/artifactId&gt;
            &lt;version&gt;1.18.12&lt;/version&gt;
            &lt;scope&gt;provided&lt;/scope&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;javax.servlet.jsp.jstl&lt;/groupId&gt;
            &lt;artifactId&gt;jstl-api&lt;/artifactId&gt;
            &lt;version&gt;1.2&lt;/version&gt;
            &lt;exclusions&gt;
                &lt;exclusion&gt;
                    &lt;artifactId&gt;servlet-api&lt;/artifactId&gt;
                    &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
                &lt;/exclusion&gt;
                &lt;exclusion&gt;
                    &lt;artifactId&gt;jsp-api&lt;/artifactId&gt;
                    &lt;groupId&gt;javax.servlet.jsp&lt;/groupId&gt;
                &lt;/exclusion&gt;
            &lt;/exclusions&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
    &lt;build&gt;
        &lt;resources&gt;
            &lt;resource&gt;
                &lt;directory&gt;src/main/webapp&lt;/directory&gt;
            &lt;/resource&gt;
            &lt;resource&gt;
                &lt;directory&gt;src/main/resources&lt;/directory&gt;
            &lt;/resource&gt;
        &lt;/resources&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
                &lt;version&gt;2.3&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;warSourceDirectory&gt;target&lt;/warSourceDirectory&gt;
                    &lt;webXml&gt;src\main\webapp\WEB-INF\web.xml&lt;/webXml&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;
&lt;/project&gt;

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({&quot;/&quot;, &quot;/hello&quot;})
    public String hello(final Model model,
                        @RequestParam(value=&quot;name&quot;,
                                required=false,
                                defaultValue=&quot;World&quot;) final String name) {
        model.addAttribute(&quot;name&quot;, name);
        log.info(&quot;called a home page&quot;);
        return &quot;index&quot;;
    }
}

my jsp (located at src/main/webapp/WEB-INF/jsp/index.jsp):

&lt;!DOCTYPE html&gt;
&lt;html class=&quot;no-js&quot; lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;utf-8&quot;&gt;
&lt;meta http-equiv=&quot;x-ua-compatible&quot; content=&quot;ie=edge&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;title&gt;Hello ${name}!&lt;/title&gt;
&lt;meta class=&quot;foundation-mq&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;css/app.css&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class=&quot;pageWithoutFooter&quot;&gt;
&lt;jsp:include page=&quot;NavigationElements/topNavigation.jsp&quot;/&gt;
&lt;h2 class=&quot;hello-title&quot;&gt;Hello ${name}!&lt;/h2&gt;
&lt;input type=&quot;button&quot; value=&quot;Hi there!&quot;&gt;
&lt;/div&gt;
&lt;jsp:include page=&quot;NavigationElements/footerNavigation.jsp&quot;/&gt;
&lt;/body&gt;
&lt;/html&gt;

webconfig:

@ComponentScan
public class WebConfig implements WebMvcConfigurer {

    @Bean
    public InternalResourceViewResolver getInternalResourceViewResolver(){
        final InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix(&quot;/WEB-INF/jsp/&quot;);
        resolver.setSuffix(&quot;.jsp&quot;);

        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。您可能希望从简单的内容重新开始:

  1. 使用https://start.spring.io/ 生成一个带有Spring Web依赖的基本Maven项目。
  2. 迁移您现有的代码并包含JSP页面。
  3. 打包为带有内嵌Tomcat的可运行JAR。
  4. 验证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.

  1. Use https://start.spring.io/ to generate a basic Maven project with Spring Web dependency.
  2. Move your current code and include the JSPs
  3. Package as running JAR with embedded Tomcat
  4. Check that JSPs are served correctly

huangapple
  • 本文由 发表于 2020年9月3日 04:06:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63712858.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定