Struts1 动态FormBean 类型问题

Silence_Smile 2010-07-16 01:32:34
Struts1中 如果页面中使用的是普通的html标签,则标签<input type="checkbox" name="f1"/> 在配置动态FormBean的时候属性f1应该配置成什么类型?怎么我配成String和Boolean都不行啊?checkbox选没选中都输出“on”
...全文
169 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
sun_s 2010-07-16
  • 打赏
  • 举报
回复
动态的formbean 应该是Sting类型的 因为他的值是String 类型的 只是他的属性 有selected 的属性 或者方法的 配置是String 类型的 看看你的程序处理的逻辑是不是有错的
closewbq 2010-07-16
  • 打赏
  • 举报
回复
配置文件中:
<form-property name= "checkbox" type= "java.lang.String[]"> </form-property>

Action中:
String[] items = (String[])form.get("checkbox");
Silence_Smile 2010-07-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 epiphone 的回复:]

FORMBEAN 中定义
private String[] checkBox = {""};//这样可以避免一个都没有选中时的空引用
public String[] getCheckBox(){return this.checkBox ;}
public void setCheckBox( String[] checkBox){ this.checkBox = checkBox ;} ……
[/Quote]
如果是动态FormBean呢?
wj63558595 2010-07-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 epiphone 的回复:]

FORMBEAN 中定义
private String[] checkBox = {""};//这样可以避免一个都没有选中时的空引用
public String[] getCheckBox(){return this.checkBox ;}
public void setCheckBox( String[] checkBox){ this.checkBox = checkBox ;} ……
[/Quote]

正解!checkbox的值是数组型的
Epiphone 2010-07-16
  • 打赏
  • 举报
回复
FORMBEAN 中定义
private String[] checkBox = {""};//这样可以避免一个都没有选中时的空引用
public String[] getCheckBox(){return this.checkBox ;}
public void setCheckBox( String[] checkBox){ this.checkBox = checkBox ;}

ACTION

for (int i=0;i <yourformbean.getCheckBox().length;i++) {
System.out.println(yourformbean.getCheckboxd()[i]);
}
Silence_Smile 2010-07-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jiaping108 的回复:]

用String 就行
HTML code

<html>
<head>
<script language="javascript">
function setValue(){
if(document.all.f2.checked){
document.all.f1.value="1";
}
else{
document.all.f1.value="0";
}
}
</script>
</he……
[/Quote]
晕,这么搞我不如直接request.getParameter()算了
jiaping108 2010-07-16
  • 打赏
  • 举报
回复
用String 就行

<html>
<head>
<script language="javascript">
function setValue(){
if(document.all.f2.checked){
document.all.f1.value="1";
}
else{
document.all.f1.value="0";
}
}
</script>
</head>
<body>
<input type="checkbox" name="f2" onclick="setValue();"/>
<input type="hidden" name="f1" value="0"/>
</body>
</html>
Action的区别 对于有着丰富的Struts1.x开发经验的朋友来说,都十分的清楚Action是整个Struts框架的核心内容,当然Struts2也不例外。不过,Struts1.x与Struts2的Action模型很大的区别。 Struts2和Struts1.x的差别,最明显的就是Struts2是一个pull-MVC架构。这是什么意思呢?从开发者角度看,就是说需要显示给用户的数据可以直接从Action中获取,而不像Struts1.x那样,必须把相应的Bean存到Page、Request或者Session中才能获取。Struts1.x 必须继承org.apache.struts.action.Action或者其子类,表单数据封装在FormBean中。Struts 2无须继承任何类型或实现任何接口,表单数据包含在Action中,通过Getter和Setter获取。 虽然,在理论上Struts2的Action无须实现任何接口或者是继承任何的类,但是,在实际编程过程中,为了更加方便的实现Action,大多数情况下都会继承com.opensymphony.xwork2.ActionSupport类,并且重载(Override)此类里的String execute()方法。如下所示: package ActionDiffer; import java.text.DateFormat; import java.util.Date; import com.opensymphony.xwork2.ActionSupport; public class ActionForStruts2 extends ActionSupport ...{ private String message; public String getMessage() ...{ return message; } @Override public String execute() ...{ message = " This is hello from strtuts2. Now is: " + DateFormat.getInstance().format( new Date()); return SUCCESS; } } 首先,从ActionForStruts2可以看出,返回的对象不是ActionForward,而是String。如果你不喜欢以字符串的形式出现在你的代码中,有个Helper接口Action可以以常量方式提供常见结果,如“success”、“none”、“error”、“input”和“login”。 另外,按照惯例,在Struts1.x中只有“execute”方法能调用Action, 但在Struts2中并非必要,任何声明为public String methodName() 方法,都能通过配置来调用Action。 最后,和Struts1.x最大的革命性的不同是,Struts2处理Action过程中调用的方法(“execute”方法)是不带参数的。那如何获取所需要的对象呢?答案是使用IoC(反转控制,Inversion of Control),也叫“依赖注入(Dependency Injection)”的模式(想更多地了解这方面信息请看Martin Fowler的文章http://www.martinfowler.com/articles/injection.html)。Spring框架使得这个模式流行起来,然而Struts2的前身(WebWork)也同时应用上了这个模式。

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧