wood burning stoves 2.0*
The moose likes Struts and the fly likes Struts, ActionForm and JSP Tag Libs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts, ActionForm and JSP Tag Libs" Watch "Struts, ActionForm and JSP Tag Libs" New topic
Author

Struts, ActionForm and JSP Tag Libs

scott gaidos
Greenhorn

Joined: Jan 30, 2002
Posts: 6
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
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
I'm having a little trouble following this based on your description. It would be easier to follow the code if you could post it using the UBB Code Code tags.
Could you give us a little insight as to what the flow is and what results you expect. My first reaction is to say to specify a scope of session for the form, but without some furhter clarification I wouldn't want to say for sure.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15964
    
  19

Normally you'd set the Form Bean's default values in its reset() method. Then, if you're loading in persistent data, you can overlay the defaults in the Action processor.
Here's a working demo:
http://www.mousetech.com/strutsdemo/index.jsp
The page that will help most is the one you get when you click on "Create new Category".


Customer surveys are for companies who didn't pay proper attention to begin with.
 
jQuery in Action, 2nd edition
 
subject: Struts, ActionForm and JSP Tag Libs
 
Similar Threads
How to get the field details from one jsp page in struts dispatchaction action class
No getter method for property name1 of bean pagesArrayList
HTTP Status 500 -
problem with LookupDispatchAction
what does .do file extension indicate