tags:

views:

1007

answers:

3

I've been trying to no avail to set up a simple Struts2 application so I can get on with learning the framework. Basically, what I am expecting to happen is that when an action is triggered that isn't defined, then a default page will be displayed.

This app is being developed in Eclipse.

I have a very simple struts.xml file set up in the WEB-INF/classes directory:

<struts>
<!-- Include webwork default (from the Struts JAR). -->
<include file="struts-default.xml" />

<!-- Configuration for the default package. -->

<package name="default" extends="struts-default">
 <action name="*">
  <result>/test.jsp</result>
 </action>
</package>
</struts>

I have the test.jsp file (just the default jsp template that gets created in Eclipse, ie "Insert title here") at the WebContent level, and the welcome page (which triggers the action) is index.jsp is also at that same level and gets displayed at program startup. It is defined as follows:

 <%@page contentType="text/html" pageEncoding="UTF-8"%>
 <%@ taglib prefix="s" uri="/struts-tags" %>

<html>
  <head>
    <title>GlassFish JSP Page</title>
  </head>
<body>
<h1>Hello World!</h1>
<s:form action="other">
   <s:submit value="Submit" />
</s:form>
</body>
</html>

I've also tried this with other action names, and no matter what, I get back a 404 page not found. I've tried this on both GlassFish and Websphere 6.1.1 with the same results, leading me to suspect that there is something I am missing that is probably right in front of me involving either directory structure (are the paths relative to the struts.xml file or the context root?) or some configuration file.

After some trying, I was able to get this up and running on Eclipse Ganymede on Mac OS X 10.5.6 using Glassfish, but I'm still having no luck with IBM Websphere and Application Server Toolkit (an Eclipse derivative) on Windows XP. I am a bit suspicious that this might have something to do with the browser.

I just tried adding a file called "other.action" to the same directory as my other JSP files mentioned above. Now when I click on the submit button that is linked to the "other" action, the action proceeds successfully to my test.jsp page. I may be incorrect, but I don't believe that I should have to give a file named "someaction.action" for every possible "someaction" that I may need to use (I know I didn't need it on the OS X run of my app).

More updates - I just tried it on Firefox 3.5.2 and running on that browser gave the same results - file not found without the "other.action" file on Websphere, so now I suspect it is a Websphere configuration issue.

A: 

Are you definitely hitting the context root for your application. i.e. in web.xml the application has a name this is normally the same as the context root unless you have packaged the application up into a war file with a different name.

http://yourserver:8080/yourcontextroot

I believe that Stuts 2 comes with some sample web applications and some maven archetypes. It's always a good idea to start with one of these and then customise it for your needs.

http://struts.apache.org/2.1.2/docs/struts-maven-archetypes.html

Struts 2 has an example project called struts-blank-2.x.xx.war. http://mirrors.dedipower.com/ftp.apache.org/struts/examples/struts2-blank-2.0.14.war

You can download it and rename it to .zip and see how they have set the project up.

WEB-INF/web.xml

should have the struts2 servlet and filter

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

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

WEB-INF/classes/struts.xml should be in the classes folder and should point to your struts config file

    ...
    <include file="yourconfig.xml"/>

*WEB-INF/yourconfig.xml in the same folder as struts.xml and contains the mappings for your actions

...
<package name="example" namespace="/example" extends="struts-default">

    <action name="*" class="example.ExampleSupport">
        <result>/example/test.jsp</result>
    </action>

    <!-- Add actions here -->
</package>

All jsp files are in a folder called /example

pjp
A: 

I might be wrong, but for every action, it goes to /test.jsp. Yet you've just created [app name]/index.jsp?? Create a [app name]/test.jsp and point there!? (try removing the slash in the xml file before test.jsp?)

Egwor
Good point - although the index.jsp is likely to be the welcome page from `web.xml`
pjp
Both the index.jsp and test.jsp files exist.
Timbuck
A: 

Using Firefox provided the key that I needed to determine the source of this issue - the Error 404: SRVE0190E message which told me that the problem was a Websphere configuration.

The problem is explained in better detail at the following link:

IBM Websphere WebContainer throws a FileNotFoundException when a request is received for a static file which does not exist on the file system.

And the solution is explained at this link (in this case, setting the com.ibm.ws.webcontainer.invokefilterscompatibility value=true):

Websphere setting up custom properties

For completeness sake, I'll put how to correct this problem here:

-In the administrative console, click "Servers" and under Servers click "Application Servers"

-Click on the server to which the custom property is to be applied

-Under "Configuration" and "Container settings" click "Web Container Settings" and under Web Container Settings click "Web Container"

-Under "Configuration" and "Additional Properties" click "Custom Properties"

-In the Custom Properties page, click "New"

-In the settings page, enter the name of the custom property to be added in the "Name" field and the value to be set for the custom property in the "Value" field. Note that some properties are case sensitive. For this issue, the property is "com.ibm.ws.webcontainer.invokefilterscompatibility" and the value will be "true"

-Click "Apply" or "OK"

-Click "Save" in the "Messages" box that appears Restart the server for the custom property to take effect

Timbuck