Struts框架(二)

  • ActionMapping 

       该元素用于将Action元素定义到ActionServlet类中,它含有0到多个<action/>元素,其格式如下:

<action-mappings>

<action path="Action请求的相对路径" 

type="该Action的对应类的全路径" 

name="该Action绑定的FormBean"

<forward name="指定处理相应请求所对应的地址" path="相对路径"/>

</action>

</action-mappings>

 

       每个action子元素可包含一个或多个forward子元素。除了pathtypename属性外,action还具有如下属性:           

         1)scope:指定ActionForm Bean的作用域(sessionrequest),缺省为session(可选)

         2)input:当Bean发生错误时返回的路径(可选)

        3)classname:指定一个调用这个Action类的ActionMapping类的全名。缺省用org.apache.struts.action.ActionMapping(可选)

       4)include:如果没有forward的时候,它起forward的作用(可选)

        5)validate:若为true,则会调用ActionFormvalidate()方法,否则不调用,缺省为true(可选)。

forward属性也是可选的。

action元素定义举例如下:

<action-mappings>
<action 
 path="/userAction" 
 type="com.amigo.struts.action.UserAction" 
 name="UserForm"
 scope="request"
 validate = "false"  //不需要验证
 input="/index.jsp">  //出错时,如果没有指定,默认转到此界面
 parameter="method" >  //采用动态action
    <forward name="error" path="/user/error.jsp" />
    <forward name="success" path="/user/success.jsp"/>
    <forward name="add" path="/user/addUser.jsp"/>
    <forward name="update" path="/user/updateUser.jsp"/>
    <forward name="list" path="/user/userList.jsp"/>
</action>
</action-mappings>


 

首先,ActionServlet接到请求后调用ForwardActionexecute()方法,execute()根据配置的parameter属性值来forward到那个URI

这样做的效果是:没有任何form被实例化,比较现实的情形可能是formrequest更高级别的范围中定义;或者这个action被用作在应用程序编译好后充当系统参数,只需要更改这个配置文件而不需要重新编译系统。

 

  • ActionForward

        在struts中实现跳转就在Action中用ActionForward,然后再struts-config.xml实现跳转。

AcrionForward默认实现的是转发,使用的是一个request,其中url地址不变;重定向使用的是两个request,其中url地址改变。可以在forward中设置重定向属性为true那么就把ActionForward变为了重定向。

        一,全局ActionForward与重定向

在一个forward中加入redirect则表示一个action中实现了重定向:

<forward name="login" path="/login.jsp" redirect="true"/>

如果想把forward跳转在其他的action中也能使用,那么可以把该forward设置为全局跳转,代码:

<global-forwards>

    <forward name="login" path="/login.jsp" redirect="true"/>

</global-forwards>

       假如Action代码中查找的跳转在action配置中不存在,那么就会到全局forward中去查找。

也可以在Action中使用代码设置重定向:

       注意如果在代码中不能使用ActionForward 对象的setRedirect()方法来做重定向,因为这样会修改struts-config.xml中的属性,但是stuts-config.xml中的属性在运行期间不能更改,所以这种重定向失败。如果不想让struts来做重定向,那么向ActionForward对象返回一个null,之后使用response的sendRedirect()来重定向。

不能写如下代码:

ActionForward af=mapping.findForward("login");

af.setRedirect(false);

//Action中设置重定向

response.sendRedirect("/login.jsp");

return null;

        二、动态ActionForward

       假如一个Action中需要跳转很多个页面,可以使用动态ActionForward来实现,然后struts-config.xml中不需要配置forward。

       举例说明:

            DynaActionForwardTestAction .java

public class DynaActionForwardTestAction extends Action {

 @Override
     public ActionForward execute(ActionMapping mapping, ActionForm form,
     HttpServletRequest request, HttpServletResponse response)
     throws Exception {//动态ActionForward
     String page=request.getParameter("page");
     ActionForward af=new ActionForward();
     af.setPath("/page"+page+".jsp");
     af.setRedirect(true);
  
     return af;
     }

}

         struts-config.xml

<action path="/dynaactionforward"
   type="com.bjsxt.struts.DynaActionForwardTestAction"
   >

</action>



 

 

posted @ 2012-03-19 16:59  转航  阅读(166)  评论(0编辑  收藏  举报