I have an object lets say Class, Now Class has Student, each student has a status selected. I represent this status on my jsp page with a checkbox. This class object is available in session. Now the UI will have each Class under which a list of students I can select and deselect a student using that checkbox, But this isn't working Not sure why? Any help??
<%@taglib uri="http://ift.tt/18NLDGG" prefix="html"%>
<%@taglib uri="http://ift.tt/18NLDGF" prefix="bean"%>
<%@taglib uri="http://ift.tt/1c9Z5PU" prefix="logic"%>
<html>
<head>
</head>
<body>
<h1>Struts html:checkbox example</h1>
<html:form action="/CheckBox">
<html:messages id="err_name" property="common.checkbox.err">
<div style="color:red">
<bean:write name="err_name" />
</div>
</html:messages>
<div style="padding:16px">
<bean:message key="label.common.html.checkbox.name" /> :
<html:checkbox property="checkboxValue" />
</div>
<div style="padding:16px">
<bean:message key="label.common.html.checkbox.name" /> :
<html:checkbox property="customObject.checkboxValue" />
</div>
Checkbox within list
<div style="padding:16px">
<logic:iterate name="htmlCheckBoxForm" id="co" property="customObjects">
<html:text property="customObjects" value="id"/>
<html:multibox property="customObjects" value="checkboxValue"/>
</logic:iterate>
</div>
<div style="padding:16px">
<div style="float:left;padding-right:8px;">
<html:submit><bean:message key="label.common.html.checkbox.button.submit" /></html:submit>
</div>
<html:reset><bean:message key="label.common.html.checkbox.button.reset" /></html:reset>
</div>
</html:form>
</body>
</html>
This is my POC code.
<%@taglib uri="http://ift.tt/18NLDGF" prefix="bean"%>
<%@taglib uri="http://ift.tt/1c9Z5PU" prefix="logic"%>
<html>
<head>
</head>
<body>
<h1>
CheckBox value :
<bean:write name="htmlCheckBoxForm" property="checkboxValue" />
</h1>
<h1>
CheckBox value :
<bean:write name="htmlCheckBoxForm"
property="customObject.checkboxValue" />
</h1>
List
<logic:iterate name="htmlCheckBoxForm" id="co" property="customObjects">
<bean:write property="id" name="customObjects" />
<bean:write property="customObjects" name="customObjects"/>
</logic:iterate>
</body>
</html>
This is my form object.
package com.mkyong.common.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import com.mykong.common.model.CustomObject;
public class HtmlCheckBoxForm extends ActionForm {
private static final long serialVersionUID = 122524010494197585L;
String checkboxValue;
CustomObject customObject;
CustomObject[] customObjects;
public String getCheckboxValue() {
return checkboxValue;
}
public void setCheckboxValue(String checkboxValue) {
this.checkboxValue = checkboxValue;
}
public CustomObject getCustomObject() {
if (customObject == null) {
customObject = new CustomObject("1");
}
return customObject;
}
public void setCustomObject(CustomObject customObject) {
this.customObject = customObject;
}
public CustomObject[] getCustomObjects() {
if (customObjects == null) {
customObjects = new CustomObject[3];
customObjects[0] = new CustomObject(System.currentTimeMillis() + "");
customObjects[1] = new CustomObject(
(System.currentTimeMillis() + 100) + "");
customObjects[2] = new CustomObject(
(System.currentTimeMillis() + 200) + "");
}
return customObjects;
}
public void setCustomObjects(CustomObject[] customObjects) {
this.customObjects = customObjects;
}
@Override
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
/*
* if( getCheckboxValue() == null || ("".equals(getCheckboxValue()))) {
* errors.add("common.checkbox.err", new
* ActionMessage("error.common.html.checkbox.required")); }
*/
return errors;
}
@Override
public void reset(ActionMapping mapping, HttpServletRequest request) {
// reset properties
checkboxValue = "";
getCustomObject().setCheckboxValue(false);
for (CustomObject co : getCustomObjects()) {
co.setCheckboxValue(false);
}
}
}
Custom Object pojo
package com.mykong.common.model;
public class CustomObject {
boolean checkboxValue;
private String id;
public CustomObject(String id) {
this.id = id;
}
public boolean isCheckboxValue() {
return checkboxValue;
}
public void setCheckboxValue(boolean checkboxValue) {
this.checkboxValue = checkboxValue;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
What am I doing wrong?? How can I achieve this with struts?
Aucun commentaire:
Enregistrer un commentaire