struts配置中的路径与模式匹配

来源:互联网 发布:翰文网络计划注册码 编辑:程序博客网 时间:2024/04/28 11:09

<!--[if !supportLists]-->³     <!--[endif]-->struts配置中的action,有一个path属性,它表明请求的URI

<!--[if !supportLists]-->³     <!--[endif]-->一般情况下,我们需要在配置文件中明确指定某个特定的URI,如path=“/user/add”

<!--[if !supportLists]-->³     <!--[endif]-->在一些大型应用中,如果能够制定一套严格的路径及其操作规范的话,我们可以利用path的路径模式匹配功能,来简化struts配置文件繁琐的编写工作量

<!--[if !supportLists]-->³     <!--[endif]-->假设有如下规范:

<!--[if !supportLists]-->³     <!--[endif]-->user对象的所有处理Action,均需要以如下的路径进行访问:

<!--[if !supportLists]-->³     <!--[endif]-->/user/add.do – 处理用户添加的有关操作

<!--[if !supportLists]-->³     <!--[endif]-->/user/delete.do – 处理用户删除的有关操作

<!--[if !supportLists]-->³     <!--[endif]-->/user/update.do – 处理用户更新的有关操作

<!--[if !supportLists]-->³     <!--[endif]-->

<!--[if !supportLists]-->³     <!--[endif]-->所有操作(Action)对应的JSP如下:

<!--[if !supportLists]-->³     <!--[endif]-->所有操作成功(失败)之后的转向页面,有如下命名规范:

<!--[if !supportLists]-->³     <!--[endif]-->/user/add.do -> /user/add_success.jsp/user/add_error.jsp

<!--[if !supportLists]-->³     <!--[endif]-->/user/delete.do -> /user/delete_success.jsp/user/delete_error.jsp

<!--[if !supportLists]-->³     <!--[endif]-->

<!--[if !supportLists]-->³     <!--[endif]-->所有操作的输入界面有如下命名规范:

<!--[if !supportLists]-->³     <!--[endif]-->添加操作 -> /user/add_input.jsp

<!--[if !supportLists]-->³     <!--[endif]-->更新操作 -> /user/update_input.jsp

<!--[if !supportLists]-->³     <!--[endif]-->

 

Action配置示例:

 

<action

path="/user/*"

type="com.bjsxt.struts.web.actions.UserAction"

name="userForm"

parameter="method"

<forward name="index" path="/user/index.jsp"/>

<forward name="success" path="/user/{1}_success.jsp"/>

<forward name="error" path="/user/{1}_error.jsp"/>

<forward name="input" path="/user/{1}_input.jsp"/>

</action>

 

解释如下:

 

<!--[if !supportLists]-->³     <!--[endif]-->所有的/user/*.do请求,都将由UserAction这个类来处理,UserAction类继承DispatchAction,它将根据传入的method参数的值,来分发到不同的方法来进行处理

<!--[if !supportLists]-->³     <!--[endif]-->UserAction类中的任何一个方法,都可以返回index/success/error/input等名称的ActionForward

<!--[if !supportLists]-->³     <!--[endif]-->根据请求路径的不同,即使调用相同的返回代码,但其转向也将不同,如:

<!--[if !supportLists]-->²     <!--[endif]-->/user/add.do?method=add请求,将被转发给UserAction类的add方法处理,假设它用mapping.findForward(success);来返回成功页面,这将转向的实际JSP页面是:/user/add_success.jsp

<!--[if !supportLists]-->²     <!--[endif]-->/user/delete.do?method=delete请求,将被转发给UserAction类的delete方法处理,假设它用mapping.findForward(success);来返回到删除成功页面,这将转向的实际JSP页面是:/user/delete_success.jsp,所以,不同URI请求的相同名称的返回页面将是不同的

<!--[if !supportLists]-->²     <!--[endif]-->/user/index.do请求(或者任何一个其它请求,如/user/abcd.do/user/test.do),都因为没有传递method参数,而触发调用UserActionunspecified方法

原创粉丝点击