如何将多个简单文本和输入文本传递给struts动作类

时间:2016-12-20 11:07:51

标签: hibernate jsp struts2

我已经从数据库平均值迭代了记录,而我添加了像单选按钮和文本框这样的输入。如何将迭代值,单选按钮和文本框值传递给动作类。

这是一个示例代码

<s:form action="actionname" theme="css_xhtml" cssClass="form-horizontal">
        <s:iterator value="result">
        <td ><s:property value="Name" /></td>
        <td><s:property value="city" /></td>
       <td><input type="radio" name="rb" value="1"> /></td>
        <td><input type="text" name="txt" > /></td>
        </s:iterator>
<s:submit cssClass="btn btn-primary" value="submit" />
</s:form>

形成这个我在我的jsp中得到的值为

This is a jsp page i got after iteration

2 个答案:

答案 0 :(得分:1)

<强> JSP

<s:form action="actionname" theme="css_xhtml" cssClass="form-horizontal">
         <s:iterator value="result">
      <td><s:hidden name="Name"value="%{Name}"/>
         <s:property value="Name" /></td>
      <td><s:hidden name="city"value="%{city}" />
          <s:property value="city" /></td>
      <td><<s:checkbox name="rb" fieldValue="check"/></td>                                  
      <td><s:textfield name="txt"cssClass="form-control" /></td>
            </s:iterator>
    <s:submit cssClass="btn btn-primary" value="submit" />
    </s:form>

<强>配置

<struts>
        <package name="yourPackageName"  extends="struts-default" >
        <action name="youActionName" class="qualified class path">
        <!-- result declaration-->                      
        </action>       
    </package>  
</struts>

动作类

 public class YourActionClass extends ActionSupport {

    String [] rb=null;
    String [] name=null;
    String [] city=null;
    String [] txt=null;

    public String[] getRb() {
            return rb;
        }



        public void setRb(String[] rb) {
            this.rb = rb;
        }



        public String[] getName() {
            return name;
        }



        public void setName(String[] name) {
            this.name = name;
        }



        public String[] getCity() {
            return city;
        }



        public void setCity(String[] city) {
            this.city = city;
        }



        public String[] getTxt() {
            return txt;
        }



        public void setTxt(String[] txt) {
            this.txt = txt;
        }

    public String execute() throws Exception {      


    for(int j=0;j<name.length;j++)
    {
     System.out.println(Name[j]);
                System.out.println(city[j]);
                System.out.println(rb[j]);
                System.out.println(txt[j]);

    }

            return SUCCESS;
        }
    }

答案 1 :(得分:0)

在我的jsp代码中

<s:form action="actionname" theme="css_xhtml" cssClass="form-horizontal">
         <s:iterator value="result">
      <td><s:hidden name="Name"value="%{Name}"/>
         <s:property value="Name" /></td>
      <td><s:hidden name="city"value="%{city}" />
          <s:property value="city" /></td>
      <td><<s:checkbox name="rb" fieldValue="check"/></td>                                  
      <td><s:textfield name="txt"cssClass="form-control" /></td>
            </s:iterator>
    <s:submit cssClass="btn btn-primary" value="submit" />
    </s:form>

在我的动作类中我使用了字符串数组

String[] rb;
String[] Name;
String[] city;
String[] txt;

然后我使用for循环迭代得到每个值

for (int i = 0; i < Name.length; i++) {
            System.out.println(Name[i]);
            System.out.println(city[i]);
            System.out.println(rb[i]);
            System.out.println(txt[i]);

        }