LookupDispatchAction主要是通过请求参数来决定执行的方法:
例如:
import java.util.HashMap;
import java.util.Map;
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.LookupDispatchAction;
import com.linkhome.struts.form.LrForm;
public class LoginAction extends LookupDispatchAction {
// 重写getKeyMethodMap方法
protected Map getKeyMethodMap() {// 用一个map保存资源文件key和方法名的映射
Map map = new HashMap();
map.put("info.tag.login", "login");
map.put("info.tag.reg", "reg");
return map;
}
public ActionForward login(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
System.out.println("登陆");
return null;
}
public ActionForward reg(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
System.out.println("注册");
return null;
}
}
ApplicationResources.properties
info.login=Login info.reg=Reg
login.jsp
<html:form action="/login">
account : <html:text property="account" />
<br />
password : <html:text property="password" />
<br />
<html:submit property="method">
<bean:message key="info.login" />
</html:submit>
<html:submit property="method">
<bean:message key="info.reg" />
</html:submit>
</html:form>
struts-config.xml
<action attribute="loginForm" input="/login.jsp" name="loginForm" path="/login" parameter="method" scope="request" type="com.struts.action.LrAction"> <set-property property="cancellable" value="true" /> </action>