简体   繁体   中英

Struts 2 application with Tomcat Maven plugin returns 404 error

First of all, there are MANY questions on SO about this particular application setup and error. However, most if not all seemingly duplicate questions were resolved by adding missing jars. I am using Maven for my dependencies and anyways, my list of External Libraries looks complete.

That being said, in my case, the mystery comes from that fact that the Tomcat Maven plugin seems to be working properly based on the

控制台输出

However, visiting http://localhost:8080/sure/hello gives me the heart wrenching "Resource not available" error. This leads me to conclude that it's a mapping issue but both my web.xml and struts.xml look pretty good to me.

web.xml

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

struts.xml

<struts>
    <package name="default" namespace="/" extends="struts-default">
        <action name="hello" class="com.crace.HelloAction">
            <result name="success">/web/WEB-INF/index.jsp</result>
        </action>
    </package>

    <constant name="struts.devMode" value="true" />
</struts>

pom.xml

<groupId>com.crace.strangewave</groupId>
<artifactId>Sure</artifactId>
<version>1.0-SNAPSHOT</version>
<name>sure</name>
<packaging>war</packaging>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <configuration>
                <port>8080</port>
                <path>/</path>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.0.0</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <showDeprecation>true</showDeprecation>
                <showWarnings>true</showWarnings>
                <fork>true</fork>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-core</artifactId>
        <version>2.5.5</version>
    </dependency>

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
    </dependency>
</dependencies>

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
    <head>
        <title>Hello</title>
    </head>
    <body>
        <s:property value="greeting"/>
    </body>
</html>

HelloAction.java

public class HelloAction extends ActionSupport {

    private String greeting;

    private static final Logger logger = Logger.getLogger(HelloAction.class);

    public String execute() throws Exception {

        setGreeting("Hello World!");

        // logs debug message
        if (logger.isDebugEnabled()) {
            logger.debug("execute()!");
        }

        // logs exception
        logger.error("This is Error message", new Exception("Testing"));


        return SUCCESS;
    }

    public String getGreeting() {
        return greeting;
    }

    public void setGreeting(String greeting) {
        this.greeting = greeting;
    }

As for logging, the only thing I could find for this embedded version renders messages such as:

0:0:0:0:0:0:0:1 - - [10/Jun/2017:13:45:38 -0400] "GET /sure/hello HTTP/1.1" 404 971 http-bio-8080-exec-3 1
0:0:0:0:0:0:0:1 - - [10/Jun/2017:13:45:38 -0400] "GET /sure/hello HTTP/1.1" 404 971 http-bio-8080-exec-5 2

Edit: I am providing a screenshot of the Tomcat manager that shows that my war is being properly deployed to the server. Again, this is a mapping issue (hence the 404) but I cannot see where I am going wrong with the mapping!

Tomcat经理应用

try this url http://localhost:8080

if you still get Resource not available error you can try following:

  1. Right Click on server and click properties

  2. If location is workspace metadata click on switch location so it will be like

    服务器属性

  3. click apply/Ok

  4. Now double click on server.
  5. choose server location as use tomcat installation

    请参见图像上的黄色高光

  6. save restart server and try.

I do believe you need to configure Tomcat Maven plugin differently depending on whether you want to run "tomcat7:deploy" and "tomcat7:run-war". I was trying to do the latter. But I was setting up my project to run the former command. The latter command requires VERY little configuration. The differences are too numerous to enumerate so if anyone is interested, just compare the pom in my question to my answer.

pom.xml

<groupId>com.crace</groupId>
<artifactId>sure</artifactId>
<version>1.0-SNAPSHOT</version>
<name>sure</name>
<packaging>war</packaging>

<build>
    <finalName>sure</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.0.0</version>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-core</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
    </dependency>
</dependencies>

The only thing is, I don't believe the addition of these settings was causing my error. But I spent a lot of time fiddling with these superfluous configuration settings. So I guess it's important to note in the end.

As far as I can tell, the only issue was that I had put index.jsp (my result page) inside of my WEB-INF folder. Moving it one level up to my web folder and changing the path in my struts.xml did the trick.

struts.xml

<struts>
    <package name="default" namespace="/" extends="struts-default">
        <action name="hello" class="com.crace.HelloAction">
            <result name="success">/index.jsp</result>
        </action>
    </package>
</struts>

EDIT: Roman C, from his comments on my question, helped me realize that my previous solution was a mere workaround. Looking again at everything, I realized that my project structure was all screwed up. Struts was looking for my jsp files inside of my webapp folder. So I just moved my WEB-INF folder inside my webapp folder and then put my jsp files inside and voila! It works. My struts.xml now looks like:

<struts>
    <package name="default" namespace="/" extends="struts-default">
        <action name="hello" class="com.crace.HelloAction">
            <result name="success">/WEB-INF/content/hello.jsp</result>
        </action>
    </package>
</struts>

And my project structure now looks like:

Project Structure

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM