我有一个JSP文件,其中包含如下链接:
<a href="links.do?method=homeAdmin">
<a href="links.do?method=signUp">
我有一个动作类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");
}
}
问题是它不起作用。但当我将其更改为类似的内容时,它只适用于signUp
操作。
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");
}
}
我还试着把它改成
public class LinkAction extends DispatchAction { ... }
这是我的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>
我想将这个单一操作文件用于我的网页中的所有链接。我该怎么做?
您需要添加parameter="method"
。这标识了包含方法名称的请求参数。
如果你有下一个行动类:
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");
}
}
您需要在struts-config.xml
文件中进行以下配置:
<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>
另请参见org.apache.struts.actions.DispatchAction
。
Struts1 always calls execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
用于处理请求的方法。您可以尝试在execute()
方法中获取请求parameter('method')
,并根据其值将其代理到homeAdmin或signUp。
正如piyush上面解释的那样,使用DispatchAction点击扩展操作类http://struts.apache.org/release/1.3.x/struts-extras/apidocs/org/apache/struts/actions/DispatchAction.html
配置文件中的配置应该类似于
<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>
据我所知,
1.)有没有办法,通过一个动作类,你可以处理多个任务,比如:你的两个方法都可以工作。
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");
}
如果它是正确的,那么您应该选择另一种类型的Action,即DispatchAction,因为它提供了编写多个方法的能力。
详细信息:
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");
}
}
您可以在此处获得详细的