• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem getting data from form bean

 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 415
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 415
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<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
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
changed the declaration in the action class its working fine
sheduleForm project = (sheduleForm) form;
thanks for the reply
 
reply
    Bookmark Topic Watch Topic
  • New Topic