在 Struts 中单击提交按钮时在会话中设置变量 [英] Set A Variable in Session When The Submit Button is clicked in Struts

查看:43
本文介绍了在 Struts 中单击提交按钮时在会话中设置变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个迭代器,它遍历一个列表并创建他们的名字,并为每个人创建一个按钮.我希望当用户单击按钮时输入与会话中单击的名称相同的名称这是我的代码

 <tr><td><s:property value="cityName"/></td></tr><s:if test="%{status == 0}"><tr><td><s:submit method="open" align="center" value="" cssStyle="background: transparent url(image/open.png) no-repeat center top; border: 0px; width: 150px; height: 30px;"><s:set name="button" value="cityName" scope="session"/></s:提交></td></tr></s:if></s:迭代器>

但问题是它只设置最后一个属性,当用户单击按钮时它没有设置它!所以它就像在创建按钮时将 cityName 放在会话中一样!但我只想通过单击不同的按钮在会话中放置不同的东西!任何身体都可以帮助吗?

解决方案

有两种解决方案:

  1. 随表单一起提交值(可为此使用隐藏字段).然后在操作类中设置会话变量.
  2. 发出同步 ajax 请求以设置提交按钮点击时的会话值.

正如 Dave 在加载 jsp 时提到的,这些值已经设置好,您无法从客户端更改它们.>

编辑

好的,正如您所要求的,这是第一种方法的基本思想.这个想法是用各自的 cityName 设置提交按钮的 id,然后 onclick 按钮调用一个 javascript 函数来设置一个隐藏字段的值与点击按钮的 id(即它的 cityName)

<s:hidden id="value_to_submit" name="value_to_submit"/><s:iterator value="packageCities" status="portStatus"><s:form>....<s:if test="%{status == 0}"><tr><td><s:submit id="%{cityName}" onclick="set_hidden_​​value(this.id)" method="open" align="center" value="" cssStyle="background: transparent url(image/open.png)) 无重复中心顶部;边框:0px;宽度:150px;高度:30px;"></s:提交></td></tr></s:if></s:迭代器></s:form>

Javascript

function set_hidden_​​value(myvalue){document.getElementById("value_to_submit").value=myvalue;}

现在您可以在操作类中捕获变量 value_to_submit 的值并在那里设置会话变量.

I have a iterator which goes through a list and create their name and for each of them a button. I want when the user click the button put the same name that has been clicked in the session This is my code

        <s:iterator value="packageCities" status="portStatus">
            <tr>
                <td>
                    <s:property value="cityName" />
                </td>
            </tr>
            <s:if test="%{status == 0}">
                <tr>
                    <td>
                        <s:submit method="open"  align="center" value="" cssStyle="background: transparent url(image/open.png) no-repeat center top; border: 0px; width: 150px; height: 30px;" >
                            <s:set name="button" value="cityName" scope="session"/>
                        </s:submit>
                    </td>
                </tr>
            </s:if>
        </s:iterator>

But The problem is it only sets the last property , And it doesn't set it when user clicked the button! so it like when its creating the button it is putting the cityName in the session! But I only want to put different thing in session by clicking different buttons! Can Any Body Help?

解决方案

There are two solutions:

  1. Submit the value along with the form (a hidden field may be used for this purpose). Then set the session variable in the action class.
  2. Make a synchronous ajax request to set the session value onclick of the submit buttons.

As Dave mentioned when the jsp is loaded the values are already set you cannot change them from client side.

EDIT

Ok, as you requested, here is a basic idea of the first method. The idea is to set the id of submit buttons with the respective cityName and then onclick of button call a javascript function to set a hidden field's value with the clicked button's id (i.e. its cityName)

<s:form>
<s:hidden id="value_to_submit" name="value_to_submit"/>
<s:iterator value="packageCities" status="portStatus"><s:form>
    ....
                <s:if test="%{status == 0}">
                    <tr>
                        <td>
                            <s:submit id="%{cityName}" onclick="set_hidden_value(this.id)" method="open"  align="center" value="" cssStyle="background: transparent url(image/open.png) no-repeat center top; border: 0px; width: 150px; height: 30px;" >
                            </s:submit>
                        </td>
                    </tr>
                </s:if>

    </s:iterator>
</s:form>

Javascript

function set_hidden_value(myvalue){
document.getElementById("value_to_submit").value=myvalue;
}

Now you can capture the value of the variable value_to_submit in your action class and set the session variable there.

这篇关于在 Struts 中单击提交按钮时在会话中设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆