I'm investigating using
Struts. I can hydrate a ActionForm and display the information to the screen. But when I try to save the info and validate it the values in the ActionForm are null. It's my undersatnding that Struts will populate the ActionForm with the new values but my original values aren't even there. Why?
Here's a listing of my code:
Struts Config:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.0//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">
<struts-config>
<!-- ========== Form Bean Definitions ============ -->
<form-beans>
<form-bean name="login" type="test.struts.LoginForm" />
<form-bean name="addressform" type="struts.formbean.AddressForm" />
</form-beans>
<!-- ========== Global Forward Definitions ========= -->
<global-forwards>
</global-forwards>
<!-- ========== Action Mapping Definitions ======== -->
<action-mappings>
<action
path="/login"
type="test.struts.LoginAction"
name="login"
input="/jsp/LoginView.jsp"
validate="true">
<forward name="valid" path="/editaddress.do" />
<forward name="invalid" path="/jsp/LoginView.jsp" />
</action>
<action
path="/editaddress" scope="request"
name="addressform"
type="struts.action.AddressEditAction" unknown="false"
validate="false">
<forward name="valid" path="/jsp/EditAddress.jsp"/>
</action>
<action
path="/saveaddress" scope="request"
name="addressform"
type="struts.action.AddressSaveAction" unknown="false"
input="/jsp/EditAddress.jsp"
validate="true">
<forward name="valid" path="/jsp/LoginView.jsp"/>
</action>
</action-mappings>
</struts-config>
JSP:
<!-- EditAddress.jsp -->
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts.tld" prefix="struts" %>
<HTML>
<HEAD><TITLE><struts:message key="title.editaddress" /></TITLE></HEAD>
<BODY>
<struts:message key="heading.editaddress" />
<html:errors />
<html:form action="/editaddress">
<p>
<struts:message key="label.address1" />:
<html:text property="address1" size="10" />
</html:form>
<html:form action="/saveaddress">
<html:submit>
<bean:message key="editaddressbutton.submit" />
</html:submit>
</html:form>
</BODY>
</HTML>
ActionForm:
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionMapping;
public class AddressForm extends ActionForm {
private
String address1;
private int id;
}
public AddressForm() {
super();
}
public int getId() {
return this.id;
}
public void setId(int aId) {
this.id = aId;
}
public String getAddress1() {
if (this.address1 == null) {
this.address1 = new String();
}
return this.address1;
}
public void setAddress1(String aAddress1) {
this.address1 = aAddress1;
}
public void load(int key) {
this.setId(key);
this.setAddress1("address1");
}
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors ae = new ActionErrors();
if (this.getAddress1().trim().length() == 0) {
ae.add("address1", new ActionError("error.no.address1"));
}
return ae;
}
}
Edit Action:
import j2ee.test.AddressVO;
import org.apache.struts.action.*;
import struts.formbean.AddressForm;
public class AddressEditAction extends org.apache.struts.action.Action {
public AddressEditAction() {
super();
}
public ActionForward perform(ActionMapping mapping, ActionForm form, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
AddressForm f = (AddressForm)form;
f.load(1);
return mapping.findForward("valid");
}
}
Save Action:
import j2ee.test.AddressVO;
import org.apache.struts.action.*;
import struts.formbean.AddressForm;
public class AddressSaveAction extends Action {
public AddressSaveAction() {
super();
}
public ActionForward perform(ActionMapping mapping, ActionForm form, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
System.out.println("Saved");
return mapping.findForward("valid");
}
}
Thanks,
Scott