I used this link to learn the struts-1 checkbox Struts checkbox example Now I did some modifications to it. But I am unable to bind the value of checkbox to my custom object variable.
Here are my jsps,
<%@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">
<% int i = 0; %>
<logic:iterate name="htmlCheckBoxForm" id="co" property="customObjects">
<html:text property="id" name="co"/>
<html:checkbox property="checkboxValue" name="co"/>
<label>html checkbox :> </label>
<input type="checkbox" name="co" />
</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>
2nd JSP showing selected value.
<%@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="co" />
<bean:write property="checkboxValue" name="co" />
</logic:iterate>
</body>
</html>
Action form:
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);
}
}
}
Here is my custom checkbox object :
package com.mykong.common.model;
public class CustomObject {
boolean checkboxValue;
private String id;
public CustomObject(String id) {
this.id = id;
}
public boolean getCheckboxValue() {
return checkboxValue;
}
public void setCheckboxValue(boolean checkboxValue) {
this.checkboxValue = checkboxValue;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
Struts config xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://ift.tt/1sIlwtT">
<struts-config>
<form-beans>
<form-bean name="htmlCheckBoxForm" type="com.mkyong.common.form.HtmlCheckBoxForm" />
</form-beans>
<action-mappings>
<action path="/CheckBoxPage" type="org.apache.struts.actions.ForwardAction"
parameter="/pages/checkbox.jsp" scope="session" />
<action path="/CheckBox" type="com.mkyong.common.action.HtmlCheckBoxAction"
name="htmlCheckBoxForm" validate="true" input="/pages/checkbox.jsp" scope="session">
<forward name="success" path="/pages/display.jsp" />
</action>
</action-mappings>
<message-resources parameter="com.mkyong.common.properties.Common" />
</struts-config>
Any help would be nice. thank you for reading and spending your time. I am using struts 1.3.
Aucun commentaire:
Enregistrer un commentaire