views:

2188

answers:

2

I want to create a struts 2 project in eclipse. I have downloaded the latest struts distribution. But things always become confusing when i try to create a struts project in ECLIPSE. Whenever I create a dynamic web project and add struts libraries to that project, some error or the other pops up.

How do I properly setup an eclipse Struts 2 project ?

To get a simple Hello world page, I did:

  1. created a dynamic web project (procollab)
  2. added struts 2 jars inside WEB-INF/lib
  3. added the same libraries inside project build path
  4. set the output folder for the src in WEB-INF/classes
  5. created a filter in web.xml to send all requests to org.apache.struts2.dispatcher.FilterDispatcher
  6. created a struts.xml in src

Errors I get:

Http 404. I get this for any URL, for example http://localhost:8080 or http://localhost:8080/procollab

I have added the project procollab in tomcat server list also in eclipse. but when I access any static resource directly, I get the page. I have the helloworld.jsp in webcontent folder, and when I go to http:localhost:8080/procollab/helloworld.jsp, I get the page correctly.

Have I set up my environment correctly ? Please help

my struts.xml file in WEB-INF/classes

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd"&gt;

<struts>
    <package name="default" namespace="/" extends="struts-default">
        <action name="SayHello">
            <result>/hello.jsp</result>
        </action>

    </package>

</struts>
+2  A: 

Http 404. I get this for any URL, for example http://localhost:8080 or http://localhost:8080/procollab

So that's the only problem? Well, a 404 just means that there's no resource on the requested URL. Page Not Found. Simple as that. You need to provide/specify the resource yourself, Eclipse won't do that for you or so. It's the code which you have full in control yourself.

I am not sure what you expect to see at http://localhost:8080, so I'll ignore this part. As to the 404 on http://localhost:8080/procollab, you just need to define a <welcome-file> in the web.xml and ensure that this is available by either (in)directly by a servlet or filter mapping, or a physical file in WebContent. That's all.

If you want to make helloworld.jsp the default landing page, you'll need to add the following to the web.xml:

<welcome-file-list>
    <welcome-file>/helloworld.jsp</welcome-file>
</welcome-file-list>

Update: as per the posted struts config, you thus expect that http://localhost:8080/procollab/SayHello.action is been executed when you access http://localhost:8080/procollab. In this case, you need to configure the <welcome-file> as follows:

<welcome-file-list>
    <welcome-file>/SayHello.action</welcome-file>
</welcome-file-list>
BalusC
I have setup a filter for the url pattern /* to go via struts filter dispatcher.
lakshmanan
So? Does struts have anything ready on `/*`? Apparently not, as per the 404.
BalusC
i have created a struts.xml in WEB-INF/classes and there, i have a package under namespace / and an action under it that will bring up a hello world jsp page
lakshmanan
Then you'll need to post more details about the struts config and if any also any inconsistencies in the server startup logs.
BalusC
What I have tried to do is web.xml -> Filter -> struts.xml -> SayHello.action -> /hello.jsp
lakshmanan
I found the mistake I did. I did a stupid mistake in typing the URL. I should append .action after the action name. I mean instead of giving http://localhost:8080/SayHello, I should type http://localhost:8080/SayHello.action :) thanks for all your support
lakshmanan
You're welcome. That's exactly what 404's are meant for. The resource simply does not exist in any way. In future, don't panic, just take it literally and think twice ;) Oh, don't forget to accept the answer. Your `43%` accept rate is also a bit scary and not in spirit of stackoverflow. Take time to review the previously asked questions in all pages in your profile.
BalusC
A: 

Hi there

In the following link there are a course with a chapter how to install and configure a Struts2 application.

The link is:

http://javaee-trainer.blogspot.com/2009/10/struts2-course-chapter-2-installation_04.html

Mark