5

I have a JSP file with links like below:

<a href="links.do?method=homeAdmin">
<a href="links.do?method=signUp">

And I have an action class LinkAction.java:

public class LinkAction extends org.apache.struts.action.Action {

    public ActionForward signUp(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        return mapping.findForward("signUp");
    }
    public ActionForward homeAdmin(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        return mapping.findForward("homeAdmin");
    }
}

The problem is that it doesn't work. But when I change it to something like this, it works only for the signUp action.

public class LinkAction extends org.apache.struts.action.Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        return mapping.findForward("signUp");
    }

}

I also tried changing it to

public class LinkAction extends DispatchAction { ... }

Here is my struts-config.xml

<action input="/index.jsp"
        name="signUp" 
        path="/links" 
        scope="session" 
        type="Actions.LinkAction">
   <forward name="signUp" path="/signUp.jsp"/>
   <forward name="homeAdmin" path="/homeAdmin.jsp"/>
</action>

I want to use this single action file for all the links in my web page. How do I do it?

Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
Maverick
  • 302
  • 1
  • 5
  • 17

4 Answers4

2

You need to add parameter="method". This identify the request parameter containing the method name.

If you had the next action class:

package actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class LinkAction extends DispatchAction {

    public ActionForward signUp(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        return mapping.findForward("signUp");
    }

    public ActionForward homeAdmin(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        return mapping.findForward("homeAdmin");
    }
}

You will need the following configuration in the struts-config.xml file:

<form-beans>
    <form-bean name="SignUpForm" type="org.apache.struts.action.DynaActionForm">
        <form-property name="fname" type="java.lang.String" />
        <form-property name="lname" type="java.lang.String" />
        <form-property name="usr" type="java.lang.String" />
        <form-property name="pwd" type="java.lang.String" />
    </form-bean>
</form-beans>
<action-mappings>
    <action path="/links"
            type="actions.LinkAction"
            name="SignUpForm"
            input="/index.jsp"
            parameter="method"
            scope="session">
       <forward name="signUp" path="/signUp.jsp"/>
       <forward name="homeAdmin" path="/homeAdmin.jsp"/>
    </action>
</action-mappings>

See also org.apache.struts.actions.DispatchAction.

Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
  • Is it possible for me to add multiple inputs in the same action in struts config? Cause i have links from multiple pages using the same LinkAction.java file. – Maverick Nov 26 '13 at 19:11
  • And yes adding the parameter="method" in struts config worked. – Maverick Nov 26 '13 at 19:28
  • The `input` attribute is used in validation errors and when you `return mapping.getInputForward();` for _forward_ to the input page. With simple links, you can use `html:link` tag. – Paul Vargas Nov 26 '13 at 19:41
1
Struts1 always calls execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response) 

method for processing the request. You can try getting request parameter('method') in the execute() method and proxy it to homeAdmin or signUp based on its value.

Baby Groot
  • 4,637
  • 39
  • 52
  • 71
user872858
  • 770
  • 7
  • 15
1

According to me what i got your question,

1.) is there any way, through one action class, you can handle the multiple task, like : your both of method could work.

public ActionForward signUp(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {

return mapping.findForward("signUp");
}

public ActionForward homeAdmin(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {

return mapping.findForward("homeAdmin");
}

if it is correct then you should go for another type of Action i.e. DispatchAction, because it provide an ability to write more than one method.

for details :

public class LanguageSelectAction extends DispatchAction{

public ActionForward chinese(ActionMapping mapping,ActionForm form,
    HttpServletRequest request,HttpServletResponse response) 
throws Exception {

    request.getSession().setAttribute(
            Globals.LOCALE_KEY, Locale.SIMPLIFIED_CHINESE);

    return mapping.findForward("provide");
}

public ActionForward english(ActionMapping mapping,ActionForm form,
    HttpServletRequest request,HttpServletResponse response) 
throws Exception {

    request.getSession().setAttribute(
            Globals.LOCALE_KEY, Locale.ENGLISH);

    return mapping.findForward("success");
}

public ActionForward german(ActionMapping mapping,ActionForm form,
    HttpServletRequest request,HttpServletResponse response) 
throws Exception {

    request.getSession().setAttribute(
            Globals.LOCALE_KEY, Locale.GERMAN);

    return mapping.findForward("success");
}

public ActionForward france(ActionMapping mapping,ActionForm form,
    HttpServletRequest request,HttpServletResponse response) 
throws Exception {

    request.getSession().setAttribute(
            Globals.LOCALE_KEY, Locale.FRANCE);

    return mapping.findForward("success");
}

}

you can get the detailed here

Piyush
  • 68
  • 7
  • the return mapping.findForward() returns the same string in every method. I want to forward different string according to the method I call. – Maverick Nov 26 '13 at 16:15
  • @Ankit in web.xml, all ready you are mapping your action, now if you see the < forward="provide" path="/requiredpage.jsp"/>, then it is saying that you are returning "provide" in place of "success" string. – Piyush Nov 27 '13 at 03:25
1

As piyush explained above do it with extending your action class with DispatchAction click http://struts.apache.org/release/1.3.x/struts-extras/apidocs/org/apache/struts/actions/DispatchAction.html

and configuration in your config file should be something like this

<action-mappings>
  <action input="/yourpage.jsp" parameter="method" name="yourform" 
    path="/yourAction" scope="session/request" type="yourpackage.youraction">
    <forward name="signup" path="/yourpage.jsp" />
</action>

Bablu
  • 11
  • 2