用Struts通配符配置导航

想映射如下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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值