Struts2学习——1200带参数的结果集

背景

本小节的内容并没有什么特殊的应用背景,而是一些技术细节。

带参数的结果集

1. 分析

index.jsp

<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>

<% String context = request.getContextPath(); %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>
向结果传参数
<ol>
    <li><a href="user/user?type=1">传参数</a></li>
</ol>

</body>
</html>

从代码分析可得,有一个超链接,这个超链接内容表明,名称空间为/user,action名称为user,传了一个参数type,值为1。

页面效果如下所示:
这里写图片描述

接下来就可以看看struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<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 type="redirect">/user_success.jsp?t=${type}</result>
        </action>       
    </package>

</struts>

由代码易得。action是UserAction,最后如果返回了success,result是一个重定向类型,客户端跳转。这一系列的分析并不难,最重要的就是这个带参数的客户端跳转。

UserAction

package com.bjsxt.struts2.user.action;

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
    public String execute() throws Exception {
        return "success";
    }

}

UserAction很正常,一个接收参数的type和返回success的语句。

2. 何时跳转要带参数

这里写图片描述

如果action1是如上图用forward方式,服务器端跳转到action2的,那么action1中接收到的参数,也可以带到action2中。

但是如果是如下的方式,那么参数就不可能带过去。

这里写图片描述

所以,如果是这种情况,还想要让action2还收到参数,就比如在返回给客户端的那条消息中,加入参数,这便是本文demo的情况。

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" %>
<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!
    from valuestack: <s:property value="t"/><br/>
    from actioncontext: <s:property value="#parameters.t"/>
    <s:debug></s:debug>
</body>
</html>

那既然我们已经把参数,带过去了, 看看最后的页面效果是怎么样的?

这里写图片描述

from valuestack: <s:property value="t"/><br/>

这句代码取不出值来

from actioncontext: <s:property value="#parameters.t"/>

这句代码就取出来了,为什么呢?

3. 原因解答

前一句代码的意思是,从当个action的value stack里面取key=t的那个value,但是,经过redirect之后,跳转到的是一个jsp页面,根本就不是action,value stack肯定是为空的,页面中的debug也可以看到。而stack context却不是空的,里面的parameter就是装着参数,取出parameter中的t对应的value,是可以取出来的。

本小节的内容就是这么多

若有不足之处,请不吝赐教

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值