tags:

views:

459

answers:

3

Currently when you hit the root context of my application Struts2 throws an error:

ex. http://localhost:8080/

returns

HTTP ERROR 404

Problem accessing /. Reason:

There is no Action mapped for namespace / and action name .

Is there a way to do a default redirect in Struts2?

A: 

You can specify a welcome-file-list in web.xml and specify index.jsp, for example:

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

Then in index.jsp, you can put this at the top:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
<META HTTP-EQUIV="Refresh" CONTENT="0;URL=initLogin.action">

Assuming you have something like this in struts.xml:

<action name="*Login" method="{1}" class="com.abc.LoginAction">
    <result name="login">/login.jsp</result>
    <result name="register">/register.jsp</result>
    <result name="success">/success.jsp</result>
</action>

This will redirect to your LoginAction class with an init action.

So then if I went to http://localhost:8080/MyApp, this is the default action it would go to.

dcp
Thanks for the quick reply. I was hoping for a better way than a meta refresh redirect but this will work fine.
rhigdon
@rhigdon - You're quite welcome, glad to help. Please don't forget to mark as answered if this solution solves your problem :).
dcp
+2  A: 

In my app I just do the following to redirect to a chosen action:

<action name="*">
    <result type="redirectAction">
        <param name="actionName">portal</param>
    </result>
</action>

Just make sure you place this as the last action in your struts.xml configuration file and then it will catch everything that couldn't be matched earlier on.

This way the redirect happens within struts2, this is invisible to the user.

Neil Foley
A: 

Is there no better thing than the meta refresh? Such as in struts there was a jsp:forward... that seems to not work in struts2. I don't know how much I am comfortable with the meta refresh. And don't know different it is than using any java provided redirect, guess I could use that dispatcher class which maybe in the jsp, not sure though.

notTheBestJavaGuy