想映射如下URL到Action的指定方法
/user/Account.do -> AccountAction.init()
/user/Account/create.do -> AccountAction.create()
/user/Blog/list.do -> BlogAction.list()
在struts-config.xml中配置如下
<action path="/user/*/*" type="supertest.web.action.{1}Action" parameter="{2}" scope="request"> </action> <action path="/user/*" type="supertest.web.action.{1}Action" parameter="" scope="request"> </action>
再写个BaseAction如下
public abstract class BaseAction extends DispatchAction {
private static final String DispatchActionMethodName = "method";
private static final String DefaultActionMethod = "init";
public abstract ActionForward init(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception;
/**
* Override DispatchAction.getMethodName() for CoC
*/
@Override
protected String getMethodName(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response,
String parameter) throws Exception {
//using DispatchAction's process if the parameter equal 'method'
if (StringUtils.equalsIgnoreCase(parameter, DispatchActionMethodName)) {
return super.getMethodName(mapping, form, request, response, parameter);
}
//using default method if parameter not define
if (StringUtils.isBlank(parameter)) {
return DefaultActionMethod;
}
return parameter;
}
@Override
protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// TODO Auto-generated method stub
return init(mapping, form, request, response);
}
protected ActionForward forward(String path) {
return new ActionForward(path);
}
protected ActionForward redirect(String path) {
return new ActionForward(path, true);
}
protected void bind(Object formBean, HttpServletRequest request) {
try {
BeanUtils.populate(formBean, request.getParameterMap());
} catch (Exception e) {
e.printStackTrace();
}
}
}
这就使用Struts通配符来匹配所有合适的URL了。
如果还要兼容以前的 /user/account.do?method=create 的连接。也是可以的。
在struts-config.xml中还像以前那样配置parameter="method"即可。
<action path="/account" type="supertest.web.action.AccountAction" parameter="method" scope="request"> </action>