简体   繁体   English

如何赋值 <s:radio> 到另一个变量使用 <s:set> 在struts2

[英]How to assign value of <s:radio> to another variable using <s:set> in struts2

I'm trying to assign the value of radio button ans1 to another variable ans_submit . 我正在尝试将单选按钮ans1的值分配给另一个变量ans_submit

<s:form action="ques2" namespace="/questions" theme="simple">

    Question 1: <s:property value="#session.ques1.question"/><br/><br/>

<s:radio list="#session.ques1.option1" name="ans1" value="%{'option1'}" label="1. "/><br/>
<s:radio list="#session.ques1.option2" name="ans1" value="%{'option2'}" label="2. "/><br/>
<s:radio list="#session.ques1.option3" name="ans1" value="%{'option3'}" label="3. "/><br/>
<s:radio list="#session.ques1.option4" name="ans1" value="%{'option4'}" label="4. "/><br/>

    <s:submit name="submit" value=" Next"/>

</s:form>

<s:set var="ans_submit" value="%{#ans1}" scope="session" />

But when I retrieve ans_submit in action class, it is giving null . 但是当我在动作类中检索ans_submit时,它给出的是null
I have to keep <s:set> tag outside of <s:form> tag. 我必须将<s:set>标记保留在<s:form>标记之外。 Because there is no variable defined as such in my model. 因为在我的模型中没有这样定义的变量。 I don't want to use javascript! 我不想使用JavaScript!

Use a map to populate a radio tag. 使用地图填充单选标签。

<s:radio name="ans1" list="#{'option1': '1. ','option2': '2. ','option3': '3. ','option4': '4. '}" />

In the action class you should have a property for ans1 where the value would be set when you submit the form. 在动作类中,您应该具有ans1的属性,该属性将在您提交表单时设置。 This property is local to the action instance which is created every time when you make a new request. 该属性是操作实例的本地属性,该操作实例在您每次发出新请求时都会创建。 So, you need to save the value in the session. 因此,您需要将值保存在会话中。

session.put("ans1", ans1);  

Now to retrieve this value from session in another action you can initialize the ans1 property 现在,要在另一个操作中从会话中检索此值,您可以初始化ans1属性

ans1 = (String) session.get("ans1");

or you can preset the value in the radio tag using a value attribute. 或者您可以使用value属性在单选标签中预设value

<s:radio name="ans1" list="#{'option1': '1. ','option2': '2. ','option3': '3. ','option4': '4. '}" value="%{#session.ans1}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM