您的位置:首页 > 编程语言 > Java开发

struts2传入数组(checkbox)到后台的处理方式

2012-07-18 11:46 351 查看
这是做一个简单的多选删除功能。

我看到好多人的做法是在jsp将多个选中的删除ID进行字符串拼接,然后传入后台。这样做比较麻烦。用了struts2就可以很好的解决这个问题。

下面是jsp页面的代码:

<form action="test.action" method="post">
<input type="checkbox" name="testid"/>
<input type="checkbox" name="testid"/>
<input type="checkbox" name="testid"/>
<input type="checkbox" name="testid"/>

<input type="submit" value="submit"/>
</form>


struts2中要定义一个String数组来存储选中的checkbox的ID。
下面是struts2的Action的代码:

package com.huaat.weibo.action;

/**
* Test
* @author  jing.yue
* @version 2012/07/17 1.0.0
*/
@Scope("prototype")
@Component("TestAction")
public class TestAction extends BaseAction {

private static final long serialVersionUID = 1754866855088929693L;

//多选的Test的ID
private String[] testid;

/**
* 删除微博
* @return
*/
public String del() {
// TODO Auto-generated method stub
logger.info("TestAction -- del");
try {
logger.info("这是选中的checkbox的大小:" + testid.length);
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}

public void setJsonStr(String jsonStr) {
this.jsonStr = jsonStr;
}

public String[] getTestid() {
return testid;
}

public void setTestid(String[] testid) {
this.testid = testid;
}
}


struts2的配置文件在这里就不多将了。相信大家都知道。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: