如果一个Action继承了DispatchAction,那么其对应的url里面就必须包含有指定方法名的参数,否则会抛出异常。本文介绍一种无需指定方法名也可以顺利访问Action的解决方案:
struts-config.xml代码:
<action
path="/index"
input="/fail.jsp"
scope="request"
parameter="status"
type="ip.struts.action.IndexAction" >
IndexAction.java代码:
public class IndexAction extends DispatchAction {
@Override
protected ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
//默认使用index()方法处理没有指定方法名的请求
return this.index(mapping, form, request, response);
}
public ActionForward index(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
//do sth...
return null;
}
}