Maverick
Maverick

Reputation: 292

pass link value to bean

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?

Upvotes: 5

Views: 1185

Answers (4)

Piyush
Piyush

Reputation: 68

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

Upvotes: 1

Paul Vargas
Paul Vargas

Reputation: 42020

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.

Upvotes: 2

Bablu
Bablu

Reputation: 11

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>

Upvotes: 1

user872858
user872858

Reputation: 780

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.

Upvotes: 1

Related Questions