Ariel
Ariel

Reputation: 1232

Why is my simple Struts2 example not working?

I am using Glassfish 4.0 with Eclipse Luna 4.4.0 and have downloaded the latest struts2 libraries. I have added all the struts2 jars to the project, just to be sure. I have created a Dynamic Web Project. This is my web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
    <display-name>Struts2Starter</display-name>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <init-param>
            <param-name>actionPackages</param-name>
            <param-value>org.demian.javabrains.action</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

This is my struts.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.devMode" value="true" />
    <package name="basicstruts2" extends="struts-default">
        <action name="getTutorial" class="org.demian.javabrains.action.TutorialAction" method="execute">
            <result name="success">/success.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
    </package>
</struts>

This is my TutorialAction.java:

package org.demian.javabrains.action;

import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;

public class TutorialAction extends ActionSupport {
    private static final long serialVersionUID = 1L;

    public String execute() throws Exception {
        System.out.println("Hello from execute!");
        Date d = new Date();
        long milliseconds = d.getTime();
        if (milliseconds % 2 == 0)
            return SUCCESS;
        else
            return ERROR;
    }
}

The other 2 pages I created are simple .jsp pages: success.jsp and error.jsp, both in the home directory. If I input this url: http://localhost:8080/Struts2Starter/success.jsp, I get the success page so it means that Glassfish is working but if I input this url: http://localhost:8080/Struts2Starter/getTutorial.action I always get http error 404. I followed the instructions on this page: struts2 documentation. Can anyone please tell me what I'm doing wrong?

EDIT

I tried debugging the application with struts2-config-browser-plugin by inserting this link

<a href="<s:url action="index" namespace="config-browser" />">Launch the configuration browser</a>

into my success.jsp page like it says here:http://struts.apache.org/release/2.2.x/docs/debugging-struts.html . When I click on the link it gives another 404 error. I don't understand. It's like Struts2 isn't working at all. Here is the log when I start the server: https://dl.dropboxusercontent.com/u/27349592/glassfish_server_startup.txt

EDIT1

I installed Tomcat 8.0.11 and here's the error message I get when I start the server: https://dl.dropboxusercontent.com/u/27349592/tomcat8_server_startup.txt

Upvotes: 2

Views: 2000

Answers (1)

Ariel
Ariel

Reputation: 1232

I finally managed to make it work! First thing I did was to remove all the jars I included and add only the ones I needed. Here are the jars that I included: https://dl.dropboxusercontent.com/u/27349592/2014-09-05%2022_36_58-Preferences%20%28Filtered%29.png

After this, I configured the libraries in the Properties -> Deployment Assembly of the project: https://dl.dropboxusercontent.com/u/27349592/2014-09-05%2022_37_29-Properties%20for%20Struts2Tutorial.png

My web.xml file is:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
    <display-name>Struts2Tutorial</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

My struts.xml file is:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.devMode" value="true" />
    <package name="basicstruts2" extends="struts-default">
        <action name="getTutorial" class="org.demian.javabrains.action.TutorialAction" method="execute">
            <result name="success">/success.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
    </package>
</struts>

I still don't know exactly what was the problem but I guess that it may be because of the dependencies in the struts2 libraries.

Upvotes: 4

Related Questions