首先列举action的属性代码以下:web
<action path="URL"session
type="ActionClass"app
name="userForm"jsp
scope="request"post
attribute="key"orm
input="/register.jsp"xml
forward="/index.jsp"对象
include="/index.jsp"内存
unknown="true"ci
parameter="test"
className="mappingclass"
validate="true">
<forward name="" path=""></forward>
</action>
下面对其属性解析:
path:表明请求的Action的名称,无需指明后缀struts1自动处理
<form action="${pageContext.request.contextPath}/Register.do" method="post">....</form>
当其点击此提交时后缀名称为.do,(因为咱们在会web.xml配置*.do的处理),因此通过mapping的地址映射,交给ActionServlet按照struts1的处理流程处理提交Action的请求。
type:表明请求的Action交给哪一个Action处理,注意此类的名称必定是类的全名称(包括包名)。
name:表明的是请求参数封装的ActionForm的名称。
备注:此名称与<form-beans><form-bean name="名称Form" type="Form类的全名"></form-bean></form-beans>中"名称Form"一致,并经过此form-bean中的type来指定封装的ActionForm类。
scope:表明把formbean的参数封装到那个做用域中,默认的做用域为session。
备注:当咱们没有指定formbean的做用域时,在type指定的Action类中还能够经过如下代码获取:
UserForm userForm = (UserForm) form;
/**
* 因为在Action的属性配置中的scope属性指定了userForm的做用域, 当采用的是默认值(session)时,
* 也能够直接从做用域中获取代码以下:UserForm userForm =
*(UserForm)request.getSession().getAttribute("userForm");
*/
/**备注:因为在开发中为了节省ActionForm占用的内存空间,通常采用scope=”request”配置,所以也能够采用一下的方式获取*/
UserForm userForm = (UserForm) request.getAttribute("userForm");
注意:若aciton的配置中没有配置attribute="key" 属性时,在Action中获取的关键字采用默认的值及name的名称,当配置了attribute="key"属性时,必须经过"key"获取。
attribute:指定formbean存储的key,不设默认为name属性的值。
举例:若是在action的配置中配置了attribute=”uform”,在Action中获取的方式应该为:
/** 经过方法参数中传递的ActionForm获取ActionForm中的对象并强制造型为UserForm */
UserForm userForm = (UserForm) form;
/**因为在action的配置中指明了attribute的属性及属性值,所以应该采用如下方式获取*/
UserForm userForm = (UserForm) request.getAttribute("uform");
input="/register.jsp" 指定formbean的数据是由哪一个页面提供的。
说明:提供此属性的目的在于formbean校验失败时,程序方便跳回formbean的输入页面,经过struts1错误信息标签,显示校验失败信息。
forward="/index.jsp" 指定收到请求时,跳转到相应的jsp页面。
强调:若是配置了此属性,则action将再也不被调用,即接受到此action时,直接调至到index.jsp页面。
include="/index.jsp" 指定收到请求时,进行页面包含。
unknown="true" 若是action把该属性设置为true,则它能够处理客户机发出的全部无效的.do请求,默认值为false。
举例:若是在配置文件中添加以下的action配置,<action path="/**" forward="/index.jsp" unknown="true"/>,当若是在地址栏中发出请求为:http://localhost:8080/20110105struts1_2/regi.do
若是在此配置文件中找不到regi.do的Action就会处理上面配置的action直接跳转到index.jsp页面 (备注:与此action中path名称无关,但必须配置一个path属性)。
parameter="test" 配置action参数,调用actioMapping.getParameter方法能够得到这里配置的参数。
className="mappingclass" <action>标签和全部的配置信息使用哪一个对象封装,默认值为ActionMapping对象。
validate="true" 请求参数封装到formbean中后,是否让struts自动调用formbean的validate方法进行数据校验,默认true。
forward标签
在action配置中,还有一些其余的标签配置,<forward name="" path=""></forward>即在处理完此action以后能够在Action中的execute方法中经过此标签的name属性获取此action,并跳转到相应的path地址中。
举例:若是在action内部咱们配置了forward标签的代码以下:
<forward name="message" path="/message.jsp"></forward>
那么能够再Action返回值中作以下处理:
/** 获取跳转的地址并返回ActionForward */
return mapping.findForward("message");
经过以上配置,在处理完此action后,struts1通过处理后就会跳转到/message.jsp视图层。