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?
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
.