This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Struts and the fly likes problem getting data from form bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "problem getting data from form bean" Watch "problem getting data from form bean" New topic
Author

problem getting data from form bean

kamesh aru
Ranch Hand

Joined: Mar 16, 2002
Posts: 150
my action class:

-----------------------------------------------
package actions;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorForm;
import forms.sheduleForm;
/**
* @version 1.0
* @author
*/
public class shedule1Action extends Action {

public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {

ActionErrors errors = new ActionErrors();
ActionForward forward = new ActionForward(); // return value
// DynaValidatorForm dynaValidatorForm = (DynaValidatorForm) form;

try {

// do something here
System.out.println("step2");
sheduleForm project = new sheduleForm();
project.setProjectName("sheduling testssss");


} catch (Exception e) {

// Report the error using the appropriate name and ID.
errors.add("name", new ActionError("id"));

}

// If a message is required, save the specified key(s)
// into the request for use by the <struts:errors> tag.

if (!errors.isEmpty()) {
saveErrors(request, errors);

// Forward control to the appropriate 'failure' URI (change name as desired)
//forward = mapping.findForward("failure");

} else {

// Forward control to the appropriate 'success' URI (change name as desired)
// forward = mapping.findForward("success");

}

// Finish with
return (mapping.findForward("success"));

}
}

-----------------------------------------------------------
My form bean
-----------------------------------------------------------
package forms;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

/**
* Form bean for a Struts application.
* Users may access 1 field on this form:
* <ul>
* <li>projectName - [your comment here]
* </ul>
* @version 1.0
* @author
*/
public class sheduleForm extends ActionForm {

private String projectName = null;

/**
* Get projectName
* @return String
*/
public String getProjectName() {


return projectName;
}

/**
* Set projectName
* @param <code>String</code>
*/
public void setProjectName(String p) {


this.projectName = p;
}

public void reset(ActionMapping mapping, HttpServletRequest request) {

// Reset values are provided as samples only. Change as appropriate.

projectName = null;

}

public ActionErrors validate(
ActionMapping mapping,
HttpServletRequest request) {

ActionErrors errors = new ActionErrors();
// Validate the fields in your form, adding
// adding each error to this.errors as found, e.g.

// if ((field == null) || (field.length() == 0)) {
// errors.add("field", new org.apache.struts.action.ActionError("error.field.required"));
// }
return errors;

}
}

---------------------------------------------------------

my jsp
---------------------------------------------------------


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ page import="forms.sheduleForm" %>
<%
sheduleForm project = new sheduleForm();
String proj = project.getProjectName();

%>
<html:html>
<HEAD>
<%@ page
language="java"
contentType="text/html; charset=ISO-8859-1"
%>

<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM WebSphere Studio">
<TITLE></TITLE>
</HEAD>

<BODY>
<P>Test Shedule.</P>
<%
System.out.println("inside jsp"+proj);

%>
<P><%= project.getProjectName() %></P>
</BODY>

</html:html>
------------------------------------------------------
my strutsconfig
-------------------------------------------------------
<action path="/shedule1" type="actions.shedule1Action">
<forward name="success" path="/sheduled.jsp">
</forward>
</action>


i am getting projectname as null in JSP ?


thanks in advance
sreenath reddy
Ranch Hand

Joined: Sep 21, 2003
Posts: 415
Hi

The big mistake u r doing is once again instantiating the form bean in ur jsp saying ScheduleForm project = new ScheduleForm() which will create a new instance rather u should access the form bean instance created by the struts .

U can display that project name using <bean:write name="ScheduleForm" property="projectName"/> This will give u ur project name.

Avoid using scriplets then u will understand the beauty of struts .
kamesh aru
Ranch Hand

Joined: Mar 16, 2002
Posts: 150
thanks for the reply
i changed the struts config
<action path="/shedule1" type="actions.shedule1Action" name="sheduleForm" scope="request">
<forward name="success" path="/sheduled.jsp">
</forward>
</action>
and jsp used


<bean:write name="scheduleForm" property="projectName"/>



giving error


SRVE0026E: [Servlet Error]-[Cannot find bean ScheduleForm in any scope]: javax.servlet.jsp.JspException: Cannot find bean ScheduleForm in any scope
at org.apache.struts.util.RequestUtils.lookup(RequestUtils.java:938)
at org.apache.struts.taglib.bean.WriteTag.doStartTag(WriteTag.java:286)

?
sreenath reddy
Ranch Hand

Joined: Sep 21, 2003
Posts: 415
<action path="/shedule1" type="actions.shedule1Action" name="sheduleForm" scope="request">

check out properly here name is sheduleForm while u r using scheduleForm ............even after doing this also if the problem occurs then there might be something wrong with ur code as its not able to find the form bean at all
kamesh aru
Ranch Hand

Joined: Mar 16, 2002
Posts: 150
thanks for the reply
yes i changed the name to sheduleForm its not giving error now but display is not showing the value of projectName what may be the cause i code i am using is what i mentioned above ?
kamesh aru
Ranch Hand

Joined: Mar 16, 2002
Posts: 150
changed the declaration in the action class its working fine
sheduleForm project = (sheduleForm) form;
thanks for the reply
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem getting data from form bean
 
Similar Threads
How to get upload an image on server and get it on next jsp?
how would I check if there are errors in Action execute method
<html:errors /> not viewing
form bean doesn't come into picture
Struts 1.1.3 validator framework not working