struts2 使用 Spring Boot - JSP 未呈现

Posted

技术标签:

【中文标题】struts2 使用 Spring Boot - JSP 未呈现【英文标题】:struts2 using Spring Boot - JSP not rendered 【发布时间】:2019-04-04 02:27:12 【问题描述】:

亲爱的, 我正在尝试使用 SpringBoot 迁移 Struts2 Web 应用程序。许多主题都在谈论未渲染和下载的 JSP,但我仍然找不到合适的解决方案。 看起来好像没有调用 ACTION 类,但是找到并下载了 JSP,而不是渲染。

我已经把我的jsp文件放在了

src/main/resources/META-INF/resources

也试过了

webapp 和 webapp/WEB-INF

我也尝试过添加一个 Struts2Configuration 类:

@Configuration
public class Struts2Configuration 
    @Bean
    public ServletRegistrationBean servletRegistrationBean(final DispatcherServlet dispatcherServlet) 
        final ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(dispatcherServlet);
        servletRegistrationBean.setEnabled(false);

        return servletRegistrationBean;
    
    @Bean
    public FilterRegistrationBean someFilterRegistration() 
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new StrutsPrepareAndExecuteFilter());
        registration.addUrlPatterns("*.action");
        registration.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.FORWARD);
        registration.setName("StrutsPrepareAndExecuteFilter");
        return registration;
    

我的 pom.xml 的一部分:

  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
    </parent>
    <groupId>com.mycomp</groupId>
    <artifactId>mycomp-project</artifactId>
    <packaging>war</packaging>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-json-plugin</artifactId>
        </dependency>

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-jaxrs</artifactId>
            <version>1.9.13</version>
        </dependency>
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>$ehcache.version</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mvel</groupId>
            <artifactId>mvel2</artifactId>
            <version>2.1.3.Final</version>
        </dependency>

        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

        <!-- Object-to-XML Mapping (OXM) abstraction and integration with JAXB,
    JiBX, Castor, XStream, and XML Beans. (depends on spring-core, spring-beans,
    spring-context) Define this if you need OXM (org.springframework.oxm.*) -->
        <!--<dependency>-->
            <!--<groupId>org.springframework</groupId>-->
            <!--<artifactId>spring-oxm</artifactId>-->
            <!--<exclusions>-->
                <!--<exclusion>-->
                    <!--<artifactId>commons-lang</artifactId>-->
                    <!--<groupId>commons-lang</groupId>-->
                <!--</exclusion>-->
            <!--</exclusions>-->
        <!--</dependency>-->
        <!--<dependency>-->
            <!--<groupId>com.thoughtworks.xstream</groupId>-->
            <!--<artifactId>xstream</artifactId>-->
            <!--<version>1.4.9</version>-->
        <!--</dependency>-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/ma.glasnost.orika/orika-core -->
        <dependency>
            <groupId>ma.glasnost.orika</groupId>
            <artifactId>orika-core</artifactId>
            <version>1.4.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.11</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>


        <!--<dependency>-->
            <!--<groupId>javax.servlet</groupId>-->
            <!--<artifactId>javax.servlet-api</artifactId>-->
        <!--</dependency>-->
        <!--<dependency>-->
            <!--<groupId>javax.servlet.jsp</groupId>-->
            <!--<artifactId>javax.servlet.jsp-api</artifactId>-->
        <!--</dependency>-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-java8-support-plugin</artifactId>
            <version>2.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>repository.com.google.maps</groupId>
            <artifactId>google-maps-services</artifactId>
            <version>0.1.5</version>
        </dependency>

        <dependency>
            <groupId>oracle-jdbc</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
    <!--
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.spring.platform</groupId>
                <artifactId>platform-bom</artifactId>
                <version>Brussels-RELEASE</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
            <dependency>
                <groupId>org.apache.struts</groupId>
                <artifactId>struts2-bom</artifactId>
                <version>$struts.version</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>
    -->
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>$spring-boot.version</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                            <goal>build-info</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

【问题讨论】:

你把jsp放到webapp/WEB-INF的时候,你在application.properties中配置了吗? 【参考方案1】:

没有错。 spring.mvc.view.prefix - 是 Str

【讨论】:

请阅读***.com/help/how-to-answer,了解如何写出好的答案。正如目前所写的那样,您的答案对其他人几乎没有用处。【参考方案2】:

你应该添加 application.properties

spring.mvc.view.prefix:/WEB-INF/views/
spring.mvc.view.suffix:.jsp

或application.yml

spring:
 mvc:
  view:
   prefix: /WEB-INF/views/
   suffix: .jsp

【讨论】:

谢谢你的回答,我试试。但似乎当我键入 localhost/home.action 时,它并没有通过我的 Struts Action 类(由 struts.xml 引导)。它不会解决问题,对吧?

以上是关于struts2 使用 Spring Boot - JSP 未呈现的主要内容,如果未能解决你的问题,请参考以下文章

Spring Cloud和Spring Boot的区别

Spring Boot简明教程--Spring Boot版本号介绍

Spring boot整合Mybatis

终于把 Spring Boot 3.0 写成书了!

Java--SSH,SSM和Spring Boot框架区别优缺点

Struts2的使用以及Spring整合Struts2