struts中html:checkbox和html:multibox标签竟然这样!!!

pcdll 2003-09-12 09:57:09
如果这样写:
<html:multibox property="type" value="2"/>
则是正常的
但如果写成这样
<html:multibox property="type" value="<%=subType.getString("name")%>"/>
即将value写成动态形式,就不能显示出来了,浏览时点右键查看html源码,居然显示如下:
<html:multibox property="type" value="39"/>

html:checkbox也是这样,究竟是怎么回事啊?如果checkbox不能动态附值,那还有什么意义啊?
...全文
176 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
msgboxmail 2004-01-08
  • 打赏
  • 举报
回复
这样的用法是不符合Struts模式的,真正的Struts模式,数据是在action中取得的,同时在action中对actionform的属性赋值,这样从action跳转到jsp页面后表单中的值就会显示了,
你的方法是在jsp页面中调用ResultSet对象取数据库中的数据,这和原来的jsp开发没有什么两样,只是形式上用了Struts标签,实际上根本就不是什么mvc模式,真正的mvc模式jsp页面上只用于显示数据结果.你应该先调用一个action,在action中调用DAO(数据访问)对象,读取数据库中的数据,给要跳转的jsp中的表单actionform中的各项属性赋值,然后再跳转到jsp页面,这才是真正的mvc模式,你用到的value属性是没有用的,只用property属性就可以动态赋值了.
kui 2004-01-08
  • 打赏
  • 举报
回复
实际上,Struts的许多标签都是不能以<%= %>方式动态赋值的,例如<html:form>等,这可能不是出于Struts模式或mvc模式的考虑,因为有时也非常需要这样的功能,我认为更主要的是在Struts开发者们编写Struts标签程序的问题,源程序中是直接把value的值输出,并没有先判断是否有<%= %>以及做动态处理.如果要处理动态的,那程序就复杂多了,也许到下个版本中会加上这些功能吧!
下面大家自己去分析一下html:checkbox和html:multibox标签的源代码,相信可解决上述问题.
package org.apache.struts.taglib.html;


import javax.servlet.jsp.JspException;
import org.apache.struts.util.MessageResources;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;


/**
* Tag for input fields of type "checkbox".
*
* @author Craig R. McClanahan
* @version $Revision: 1.17 $ $Date: 2002/11/16 06:05:21 $
*/

public class CheckboxTag extends BaseHandlerTag {


// ----------------------------------------------------- Instance Variables


/**
* The message resources for this package.
*/
protected static MessageResources messages =
MessageResources.getMessageResources(Constants.Package + ".LocalStrings");


/**
* The name of the bean containing our underlying property.
*/
protected String name = Constants.BEAN_KEY;

public String getName() {
return (this.name);
}

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


/**
* The property name for this field.
*/
protected String property = null;


/**
* The body content of this tag (if any).
*/
protected String text = null;


/**
* The server value for this option.
*/
protected String value = null;


// ------------------------------------------------------------- Properties


/**
* Return the property name.
*/
public String getProperty() {

return (this.property);

}


/**
* Set the property name.
*
* @param property The new property name
*/
public void setProperty(String property) {

this.property = property;

}


/**
* Return the server value.
*/
public String getValue() {

return (this.value);

}


/**
* Set the server value.
*
* @param value The new server value
*/
public void setValue(String value) {

this.value = value;

}


// --------------------------------------------------------- Public Methods


/**
* Generate the required input tag.
* <p>
* Support for indexed property since Struts 1.1
*
* @exception JspException if a JSP exception has occurred
*/
public int doStartTag() throws JspException {

// Create an appropriate "input" element based on our parameters
StringBuffer results = new StringBuffer("<input type=\"checkbox\"");
results.append(" name=\"");
// * @since Struts 1.1
if( indexed )
prepareIndex( results, name );
results.append(this.property);
results.append("\"");
if (accesskey != null) {
results.append(" accesskey=\"");
results.append(accesskey);
results.append("\"");
}
if (tabindex != null) {
results.append(" tabindex=\"");
results.append(tabindex);
results.append("\"");
}
results.append(" value=\"");
if (value == null)
results.append("on");
else
results.append(value);
results.append("\"");
Object result = RequestUtils.lookup(pageContext, name,
property, null);
if (result == null)
result = "";
if (!(result instanceof String))
result = result.toString();
String checked = (String) result;
if (checked.equalsIgnoreCase(value)
|| checked.equalsIgnoreCase("true")
|| checked.equalsIgnoreCase("yes")
|| checked.equalsIgnoreCase("on"))
results.append(" checked=\"checked\"");
results.append(prepareEventHandlers());
results.append(prepareStyles());
results.append(getElementClose());

// Print this field to our output writer
ResponseUtils.write(pageContext, results.toString());

// Continue processing this page
this.text = null;
return (EVAL_BODY_TAG);

}



/**
* Save the associated label from the body content.
*
* @exception JspException if a JSP exception has occurred
*/
public int doAfterBody() throws JspException {

if (bodyContent != null) {
String value = bodyContent.getString().trim();
if (value.length() > 0)
text = value;
}
return (SKIP_BODY);

}


/**
* Process the remainder of this page normally.
*
* @exception JspException if a JSP exception has occurred
*/
public int doEndTag() throws JspException {

// Render any description for this checkbox
if (text != null)
ResponseUtils.write(pageContext, text);

// Evaluate the remainder of this page
return (EVAL_PAGE);

}


/**
* Release any acquired resources.
*/
public void release() {

super.release();
name = Constants.BEAN_KEY;
property = null;
text = null;
value = null;

}


}
whyxx 2004-01-08
  • 打赏
  • 举报
回复
你的写法不太标准,但如果写成这样,sturts的确会出现这种现象,他在解析<html:的时候会将后面<%=subType.getString("name")%>当作一个字符串来处理,而不会先计算出值再来处理.
Leemaasn 2004-01-08
  • 打赏
  • 举报
回复
是啊。

试试楼上的?



Up。。。

^@^
bookhome 2004-01-08
  • 打赏
  • 举报
回复
当然楼主这种做法也是可以地,呵呵。
送上sample一份
<%
String[] multiboxValues = (String[])request.getAttribute("multiboxValues");
for (int i = 0; i < multiboxValues.length; i++) {
%>
<html:multibox property="multiboxData">
<%= multiboxValues[i] %>
</html:multibox><%= multiboxValues[i] %><br>
<%
}
%>
bookhome 2004-01-08
  • 打赏
  • 举报
回复
2楼说的对
BeanUtils.setProperty(actionForm, property, 值(String));
BeanUtils.setArrayProperty(actionForm, property, 值(String[]));
adjoin 2004-01-08
  • 打赏
  • 举报
回复
学习
jokerjava 2004-01-08
  • 打赏
  • 举报
回复
我猜他的jsp里面没有

<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-template.tld" prefix="template" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>

这些东西 嘿嘿
bluesky35 2004-01-08
  • 打赏
  • 举报
回复
其实是这样的,struts中html:checkbox和html:multibox,在JSP第一次编译时,会将他们的property和value进行比较,如果相同就会自动的打上勾,也就是选中,提交时,将value中的值提交出去,具体看下面的分析:
<html:multibox property="stringMultibox" value="1" />
如果把这句写在JSP中,那么当JSP第一次被编译时运行时,form bean里的stringMultibox将和1进行比较,如果等于1,这个框就是选中的,当提交时就提交1,当然,你也可以改变他
记得在第一次运行JSP时,这个VALUE不是赋值,而是比较!!!
寒冬 2004-01-08
  • 打赏
  • 举报
回复
我感觉要在struts中一点也不用<% %>,有时会增加许多工作量,我有的地方都是二者结合的。

关注。。学习。。。
junnef 2003-09-12
  • 打赏
  • 举报
回复
不会吧,我这样用都没有问题的,你的标签库是不是配置有问题?

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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