Struts1中的XML标签

Struts1中的标签

<action path=”URL”

    type=”ActionClass”

    name=”userForm”

    scope=”request”

    attribute=”key”

    input=”/register.jsp”

    forward=”/index.jsp”

    include=”/index.jsp”

    unknown=”true”

    parameter=”test”

    className=”mappingclass”

    validate=”true”>

    <forward name=”” path=”“></forward>

</action>

path表示的是action请求的名称,比如<form action=”${pageContext.request.contextPath}/Register.do” method=”post”>….</form>中,*do后缀无需指明,strut1会自动进行处理。在上面的<form>表单中action标签对应的type应该是type=”/Register“。

type代表该请求应该有哪个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”);

input指定formbean的数据是由哪个页面提供的。
说明:提供此属性的目的在于formbean校验失败时,程序方便跳回formbean的输入页面,通过struts1错误信息标签,显示校验失败信息

forward 指定收到请求时,将请求结果跳转到哪个页面

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视图层。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
	
<!-- 用于定义所有的ActionForm --> 
<struts-config>
    <form-beans>
		<!-- 定义ActionForm,有表单实体类就需要定义,至少指定两个属性: name , type-->  
		<form-bean name="user" type="com.ssh.bean.User"></form-bean>
		<form-bean name="book" type="com.ssh.bean.Book"></form-bean>
	</form-beans>
	<!-- <plug-in className="net.sf.struts.saif.SAIFSpringPlugin">     
          <set-propertyproperty="interceptor-config" value="/WEB-INF/interceptor-config.xml"/>   
     </plug-in> -->
 	<!-- 该元素里配置所有的Action -->  
	<action-mappings>
	<!-- 定义局部forward-->
			<forward name="error" path="/user/error.jsp" ></forward>
			<forward name="success" path="/user/success.jsp"></forward>
			<forward name="reLogin" path="login.jsp"></forward>
		</action>	
		<action name="book" path="/book" type="org.springframework.web.struts.DelegatingActionProxy" parameter="m">
             <forward name="success" path="/book/showBook.jsp"/>
        </action>
    </action-mappings>

 <!-- 配置全局Forward -->  
    <global-forwards>  
        <!-- 配置Forward对象的name 和path 属性 -->  
        <forward name="error" path="/WEB-INF/jsp/error.jsp" />  
    </global-forwards>  
</struts-config>

猜你喜欢

转载自blog.csdn.net/qq3892997/article/details/81777788