web中的checkbox以及strtus2的CheckboxInterceptor
来源:互联网 发布:单片机正弦波 编辑:程序博客网 时间:2024/06/06 12:27
checkbox标签可以创建一个checkbox
当这个checkbox标签没有指定value属性的时候:<input type="checkbox" name="math"/>:
如果提交表单时没有被选中,那么req.getParameter("math")得到的值为null。
如果提交表单时被选中,那么req.getParameter("math")得到的值为on。
当这个checkbox标签指定value属性的时候:<input type="checkbox" name="math" value="true"/>:
如果提交表单时没有被选中,那么req.getParameter("math")得到的值为null。
如果提交表单时被选中,那么req.getParameter("math")得到的值为true。
我们可以在后台判断checkbox的值是不是null来确定其是否选中,但是struts2的checkbox标签和CheckboxInterceptor提供了便利。
查看struts2的checkbox标签的源代码:
<input type="checkbox"name="intresting"value="true"id="intresting"/>
<inputtype="hidden"id="__checkbox_intresting"name="__checkbox_intresting"value="true" />
注意,它多加了一个隐藏域。
CheckboxInterceptor的处理:
public String intercept(ActionInvocation ai) throws Exception {
Map parameters = ai.getInvocationContext().getParameters();
Map<String, String[]> newParams = new HashMap<String, String[]>();
Set<Map.Entry> entries = parameters.entrySet();
for (Iterator<Map.Entry> iterator = entries.iterator(); iterator.hasNext();) {
Map.Entry entry = iterator.next();
String key = (String)entry.getKey();
if (key.startsWith("__checkbox_")) {
String name = key.substring("__checkbox_".length());
Object values = entry.getValue();
iterator.remove();
if (values != null && values instanceof String[] && ((String[])values).length > 1) {
LOG.debug("Bypassing automatic checkbox detection due to multiple checkboxes of the same name: #1", name);
continue;
}
// is this checkbox checked/submitted?
if (!parameters.containsKey(name)) {
// if not, let's be sure to default the value to false
newParams.put(name, new String[]{uncheckedValue});
}
}
}
parameters.putAll(newParams);
return ai.invoke();
}
隐藏域的值总是提交的,它将获取隐藏域的name,并删除此name-value,然后查看在parameters是否包含此name,如果是,那么该checkbox是选中的,其值为true。不必处理,如果parameters中不包含此name,那么该checkbox没有被选中,那么添加name和false到parameters中。
这样,如果checkbox是选中的那么在后台得到的值为true。
如果是非选中的那么在后台得到的值为false。
radio和checkbox是一样的,需要注意的是,只需在创建radio的时候将其中一个radio设置为选中,那么在后台得到的值总不为null。
- web中的checkbox以及strtus2的CheckboxInterceptor
- strtus2 web资源获取
- strtus2.1版本以上的web.xml配置
- strtus2中的default-action-ref无效的解决方法
- 初学strtus2的问题解决
- strtus2的学习笔记
- Strtus2的基本使用
- Strtus2再爆漏洞,以及修复方法
- Strtus2 struts-cleanup的用途
- Strtus2的一些配置描述
- Strtus2和SpringMVC的区别
- Strtus2 简单的小列子
- web中 checkbox的全选
- CTreeCtrl中的checkbox的获取~
- MFC中的CheckBox的使用
- ribbon中的checkbox的使用
- strtus2里的struts.xml的配置
- js取消checkbox以及select的选中
- Android Widget 简单例程(mp3播放)
- 云计算简单知识整理
- 合并两个有序单链表
- POJ 3252 Round Numbers
- OEM,ODM,OBM是什么??
- web中的checkbox以及strtus2的CheckboxInterceptor
- 值得参考的 10 个 LESS CSS 实例
- 原创伤感qq日志-早已习惯两指之间夹着烟的孤单
- 媒体MediaPlayer错误详细解析
- CoUninitialize 退出时 m_pInterface->Release()崩溃 解决
- IOS-沙盒机制和用户文件操作
- 数字雨程序源码
- Qt核心剖析: moc
- 大数(高精度)加减乘除取模