struts2随笔

包含模块 :

<include file=”logi.xml”/>

 

 

默认的 Action 引用:

<default-action-ref name=”error”></default-action-ref >

< action name = "error" >

         < result > error.jsp </ result >

  </ action >

 

这样的话,在客户端访问的时候:

http://localhost:8888/struts2_0100_AcessWebElement/adfffsfsdfdf

就会自动导向 error.jsp 文件。

 

 

 

 

Result Type 的相关知识点:

< default-action-ref name = "error" ></ default-action-ref >

        < action name = "r1" >

            <!-- 默认的 result type, 相当于 forward, 服务器跳转到指定页面(只

            能跳转到页面,如 html,jps,freemaker 等,但不能是 action; 因为是服务器

            端跳转,所以浏览器只发送一个请求,地址栏的地址不发生改变 -->

            < result type = "dispatcher" > r1.jsp </ result >

        </ action >

            <!-- 客户端跳转页面,因为是客户

            端跳转,所以浏览器要发送两个请求,所以地址栏的地址会发生改变 -->

        < action name = "r2" >

            <!--  -->

            < result type = "redirect" > r2.jsp </ result >

        </ action >

        < action name = "error" >

            < result > error.jsp </ result >

        </ action >

 

Global Result :全局转向

< package name = "user" namespace = "/user" extends = "struts-default" >

        < global-results >

            < result name = "error" > /error.jsp </ result >

        </ global-results >

        < action name = "userlogin" class = "com.oristand.actions.UserLoginAction" >

            < result > /user_success.jsp </ result >

        </ action >

  </ package >

 

  < package name = "admin" namespace = "/admin" extends = "user" >

       

        < action name = "adminlogin" class = "com.oristand.actions.AdminLoginAction" >

            < result > /admin_success.jsp </ result >

        </ action >

  </ package >

 

这样的话无论是在 user 包里,还是在 admin 里,都可以访问到 <global-results>

            <result name="error">/error.jsp</result>

        </global-results>

 

动态转向:

 

Struts.xml:

< action name = "user" class = "com.oristand.actions.UserAction" >

            < result > ${result} </ result >

        </ action >

 

UserAction.java:

package com.oristand.actions;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class UserAction extends ActionSupport {

    private int type ;

    private String result ;

    public int getType() {

       return type ;

    }

    public void setType( int type) {

       this . type = type;

    }

    public String getResult() {

       return result ;

    }

    public void setResult(String result) {

       this . result = result;

    }

    @Override

    public String execute() throws Exception {

       if ( type ==1){

           result = "/success.jsp" ;

       }

       else {

           result = "/fail.jsp" ;

       }

       return SUCCESS ;

    }

 

}

 

带参数的结果集

 

结论:一次 request 有且只有一个值栈。所以在服务器端 action 之间互相 forward 的时候,可以共享参数,所以在服务器端跳转的时候不需要传递参数,但是在客户端跳转的时候需要传递相应的参数。

 

Struts.xml:

< action name = "user" class = "com.oristand.actions.UserAction" >

            < result type = "redirect" > /success.jsp?t=${type} </ result >

        </ action >

 

UserAction.java:

package com.oristand.actions;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class UserAction extends ActionSupport {

    private int type ;

    public int getType() {

       return type ;

    }

    public void setType( int type) {

       this . type = type;

    }

    @Override

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值