struts1自定义方法及struts1 Action 配置

Action 继承 DispatchAction 
public class UserAction extends DispatchAction{
private UserBO bo = new UserBOImpl() ;
public ActionForward add(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
  UserForm ff =(UserForm)form;
  UserModel model = new UserModel();
  try{
   BeanUtils.copyProperties(model, ff) ;
  }catch(Exception e){
   e.printStackTrace() ;
  }
  bo.addUserModel(model) ;
  ff.reset(mapping, request) ;
  return mapping.findForward("search") ;
}
public ActionForward update(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
  UserForm ff =(UserForm)form;
  UserModel model = new UserModel();
  try{
   BeanUtils.copyProperties(model, ff) ;
  }catch(Exception e){
   e.printStackTrace() ;
  }
  bo.updateUserModel(model) ;
  return mapping.findForward("search") ; 
  
  
  
  
  
}
}
在提交页面添加隐藏域
<html:hidden property="method" value="add"/>
当提交 到上面的 action 当中后  他就自动 去找 add 这个方法执行了  
还有必须在mxl  当中配置
<action path="/userOperator"   type="com.rhcj.customer.struts.action.UserAction"
    name="userForm"
    scope="request"
    parameter="method"
    validate="false">
   <forward name="search" path="/customer/search.jsp"></forward>
  </action>

parameter="method"  这个属性

在struts-config.xml里的<action  parameter="method" >就可以了
form 里 action.do?method=xxx
就可以了!

 

 

 

struts1 Action 配置

 

action元素的所有配置会被映射到ActionMapping对象中。

下面是action元素的每个属性的作用描述:

attribute

在struts将JSP页面的表单对象封装成一个ActionForm对象后,会将这个ActionForm对象保存在request作用域中或者session作用域中,attribute属性就是作为key被存储到作用域中。

在JSP页面可以直接使用EL表达式去获取ActionForm对象:

${requestScope.对象名.属性}

或者

${sessionScope.对象名.属性}

这里的对象名就是attribute属性定义的值。

注意:当action元素中定义了name属性后,attribute属性的值将被忽略。

如果当前action需要用到ActionForm时,name属性是必须的,attribute属性就是非必须的。

一般情况下,attribute属性值与name属性值的值相同。

cancellable

可 取的值为:true,false,yes,no。在JSP页面的表单中,<html:cancel />标签允许退出当前Action,如果cancellable属性值为真(true,yes),则一切正常。如果cancellable属性为假 (false,no),则点击cancel按钮后发生org.apache.struts.action.InvalidCancelException异常。

默认为false。

catalog

The name of a commons-chain catalog in which to look up a command to be executed as part of servicing this request. Only meaningful if "command" is also specified.

className

某个实现ActionMapping类的子类的全路径名(包括包名)。可以自定义一个ActionMapping的子类。

默认为<action>元素的父节点<action-mappings>的type属性,如果未定义则默认为org.apache.struts.action.ActionMapping

command

The name of a commons-chain command which should be looked up and executed as part of servicing this request.

extends

The path of the ActionConfig that this object should inherit properties from.

forward

某个servlet或者jsp页面或者其他资源的相对路径。如果使用了forward属性,则当前action被触发时,会跳转到forward属性指定的资源,不会继续进行处理,当然也会忽略type属性。

<action path="/login" forward="/form/login.jsp" />

当 用户访问"/login.do"时,会forward到"/form/login.jsp"页面,Struts内部会使用 RequestDispatcher.forward()方法。这是一个很好的隐藏JSP页面的真实路径方法。可以将JSP页面放在WEB-INF目录 下,这样只能通过"*.do"或者被Action调用的形式访问。

include

作用同forward属性,可以包含某个资源,内部是通过RequestDispatcher.include()方法实现。 同样会忽略type属性。

input

当前action的提交页面路径(默认)或者其他资源的相对路径。使用ActionMapping.getInputForward()可以返回input属性指定的资源路径。

当FormBean的验证方法vaildate返回不为null的ActionErrors时,将返回input属性指定的路径,如果没有指定input属性,将抛出异常(java.lang.IllegalArgumentException: The path of an ForwardConfig cannot be null

当在action元素中定义了name属性时,input属性是必须定义的。

name

FormBean的name,应该与<form-bean>元素中的name属性相同,这样才能将FormBean与当前action绑定起来。

如果有了name属性,则必须定义input属性。

attribute属性与name属性类似,但是name是必须的,attribute不是必须的。

Action是通过name属性去寻找自己的FormBean,而不是attribute属性。

parameter

此 属性结合DispatchAction类使用。当parameter属性值为method,则访问链接"/userAction?method=add" 时,在继承了DispatchAction的Action类中会调用自定义的add方法。实现不同的操作,自动调用不同的业务处理方法。不用手动去写if else代码了。

具体,请参看《Struts1.X-DispatchAction类-根据请求参数实现业务分派》

path

以"/"开头,不带文件扩展名的路径,Struts根据path属性来选择相应的Action处理用户的请求。比如当某个请求地址为"show.do",则path属性应该为"/show",这样才能找到合适的Action处理用户的请求。

prefix

指 定填充当前 Action 关联 FormBean 时 ,要添加到请求参数名称的前缀,因此,如果请求参数名为 "username" 并且 prefix 属性被设置为 "search" ,则将对 FormBean 调用一个名为 setSearchUsername() 的方法,只有指定了 name 属性,本属性才有效

roles

以逗号分割的一段role角色名,只有这些角色才能访问当前Action,其他用户访问后会抛出【org.apache.struts.chain.commands.UnauthorizedActionException】异常。

scope

FormBean的作用域,可取的值为request和session,默认为session。如果未定义name属性,则这个属性也就么意义了。

suffix

作用同prefix,suffix为后缀,prefixx为前缀。

type

完整的类名,包括包名,此类应该是org.apache.struts.action.Action类的子类。如果定义了forward属性或者include属性,则type属性被忽略。

unknown

可取的值:true,false,yes,no。当为真时(true,yes),当前这个Action将处理所有未找到相应处理Action的请求。只能有一个Action的unknown属性为真。默认为false。

validate

可取的值:true,false,yes,no。当为真时(true,yes),在进入Action处理类前,是否调用ActionForm Bean的validate()方法对表单数据进行验证。默认为true。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值