如何将多个复选框传递给操作 [英] How to pass multiple checkboxes to the Action

查看:92
本文介绍了如何将多个复选框传递给操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Struts 2和Freemarker.这是我的动作课:

I am using Struts 2 and Freemarker. This is my action class:

package test;

import java.util.ArrayList;
import java.util.List;

public class WelcomeAction {

    private String userName;
    private String gender;
    private List<String> fruits;
    private String fruit;


    public String execute() {
//      if(!userName.equals("a"))
//      {
//          return "fail";
//      }
//      else {
//          return "SUCCESS";           
//      }
        return "SUCCESS";
    }   

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }


    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getFruitsPicked() {
        return fruit;
    }

    public void setFruitsPicked(String fruitsPicked) {
        this.fruit = fruitsPicked;
    }

    public List<String> getFruits() {
        return fruits;
    }

    public void setFruits(List<String> fruits) {
        this.fruits = fruits;
    }

    public WelcomeAction(){
        fruits = new ArrayList<String>();
        fruits.add("apples");
        fruits.add("oranges");
        fruits.add("pears");
        fruits.add("peaches");
    }
}

这就是我的.ftl中的内容:

<input type="checkbox" list="fruits" name="friut[]" value="apples" /> Apples<br /> 
<input type="checkbox" list="fruits" name="friut[]" value="oranges" /> Oranges<br /> 
<input type="checkbox" list="fruits" name="friut[]" value="pears" /> Pears<br /> 
<input type="checkbox" list="fruits" name="friut[]" value="peaches" /> Peaches<br />

这是我尝试打印的方式:

This is how I tried printing:

<#list fruits as item>${item}</#list>

但是上面的命令会打印出我在构造函数中添加的列表中的所有项目.当然,我只希望提交表单时检查的项目.

But above command prints all the items in my list that I added in my constructor. Of course, I only want the items that were checked when the form was submitted.

推荐答案

name="friut[]"应该为name="fruits". JavaBean属性名称是fruits,而不是fruit(也不是friut ...注意拼写错误).我不知道[]对于Struts有意义,更好地说,对于ParametersIntercaptor使用的OGNL/ValueStack.setValue. (尽管它理解fruits[0],这对于在现有列表中设置元素很有用.).因此,最后Struts忽略了参数,因此最终得到了原始列表. list="fruits"来自哪里?

name="friut[]" should be name="fruits". The JavaBean property name is fruits, not fruit (nor friut... note the typo). I don't know about that [] is meaningful for Struts, better said, for OGNL/ValueStack.setValue that the ParametersIntercaptor uses. (It understands fruits[0] though, which is useful to set an element in an existing list.). So at the end Struts has ignored the parameters, so you end up with the original list. Where does list="fruits" come from?

这篇关于如何将多个复选框传递给操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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