Result

 

1.常用四种类型:

      a)          dispatcher(默认)

      服务器跳转(普通转发),就是forward到一个JSP或者HTML或者其他结果页面,不能是Action

                   视图请求地址是             http://localhost:8080/myweb/r1/r1.jsp

                   浏览器最后显示的地址是  http://localhost:8080/myweb/r1/r1.jsp

       b)         redirect

       重定向到结果视图

                   视图请求地址是             http://localhost:8080/myweb/r2/r2.jsp

                   浏览器最后显示的地址是  http://localhost:8080/myweb/r2.jsp

        c)         chain

        服务器forward(转发)到一个另一个Action 

                   视图请求地址是             http://localhost:8080/myweb/r3/r1.jsp

                   浏览器最后显示的地址是  http://localhost:8080/myweb/r1/r1.jsp 

         d)         redirectAction

        重定向到另外一个Action 

                   视图请求地址是             http://localhost:8080/myweb/r4/r2.jsp

                   浏览器最后显示的地址是  http://localhost:8080/myweb/r2.jsp

 

                   重点是上面两种

<struts>
    <constant name="struts.devMode" value="true" />
    <package name="resultTypes" namespace="/r" extends="struts-default">
        <action name="r1">
            <result type="dispatcher">/r1.jsp</result>
        </action>
        
        <action name="r2">
            <result type="redirect">/r2.jsp</result>
        </action>
        
        <action name="r3">
            <result type="chain">r1</result>
        </action>
        
        <action name="r4">
            <result type="redirectAction">r2</result>
        </action>
        
    </package>

 

如果chain是不同的命名空间:

<result type="chain">
            <param name="actionName">dashboard</param>
            <param name="namespace">/secure</param>
        </result>

 

 

2.全局结果集

 

Global_Results

<struts>
    <constant name="struts.devMode" value="true" />
    <package name="user" namespace="/user" extends="struts-default">
        
        
        <global-results>
            <result name="mainpage">/main.jsp</result>
        </global-results>
        
        <action name="index">
            <result>/index.jsp</result>
        </action>
        
        <action name="user" class="com.bjsxt.struts2.user.action.UserAction">
            <result>/user_success.jsp</result>
            <result name="error">/user_error.jsp</result>
        </action>        
    </package>
    
    <package name="admin" namespace="/admin" extends="user">
        <action name="admin" class="com.bjsxt.struts2.user.action.AdminAction">
            <result>/admin.jsp</result>
        </action>
    </package>
</struts>

 

Global_Results是在package标签下。

意思就是说这个namespace下所有的action共享,当某一个action找不到对应的result时,就找到了它。

 

extends

 

同时这个extends 是表示namespace继承的意思,可以得到它继承namespace的所有配置

默认的都是继承于 

 extends="struts-default"

 

它的源码在struts-core-2.1.6.jar包里面

里面配置了很多的拦截器等。

但是在实际开发中,这种继承关系如果多了就会很复杂

所以实际上一般用在

比如说有好几个模块,用户管理,学生管理,老师管理,权限管理等

它们有公用页面

比如返回首页,错误信息返回等,

可以使用到继承

 

 

$这种写法

用在可以在配置文件里往值栈里面取值

一个UserAction.java里面的内容都会存放在值栈里面

可以去读value Stack 里的内容

 

 

可以在类中用一个属性用来保存一个结果

这个属性的值可以动态确定

在struct里面可以用$符号把结果取出来

 

 

3.动态结果

        在action中保存一个属性,存储具体的结果位置location

 

index.jsp:

<body>
   动态结果
  一定不要忘了为动态结果的保存值设置set get方法
<ol>
    <li><a href="user/user?type=1">返回success</a></li>
    <li><a href="user/user?type=2">返回error</a></li>
</ol>
    
</body>

 

struct.xml :

通过$符号取出值栈里面的内容

<struts>
    <constant name="struts.devMode" value="true" />
    <package name="user" namespace="/user" extends="struts-default">
        
        <action name="user" class="com.bjsxt.struts2.user.action.UserAction">
            <result>${r}</result>
        </action>        
    </package>
        
</struts>

 

UserAction.java

为这个属性动态指定location

@Override
    public String execute() throws Exception {
        if(type == 1) r="/user_success.jsp";
        else if (type == 2) r="/user_error.jsp";
        return "success";
    }


/user_success.jsp
<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
    <%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>UserSuccess</title>
</head>
<body>
    User Success!
    <s:property value="r"/>
    <s:debug></s:debug>
</body>
</html>

 

 

4.传递参数

struct中间带参数

重定向才需要传递

使用${}

 

 <package name="user" namespace="/user" extends="struts-default">
        
        <action name="user" class="com.bjsxt.struts2.user.action.UserAction">
            <result type="redirect">/user_success.jsp?t=${type}</result>
        </action>        
    </package>

redirect如何能把数据从一个视图传递到另一个视图?
链接 用action向其他视图传递参数(redirect才需要传递)

 

posted on 2014-12-06 18:03  飞鸟快跑  阅读(812)  评论(0编辑  收藏  举报