aspose file tools*
The moose likes Struts and the fly likes Actionforms and Actions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Actionforms and Actions" Watch "Actionforms and Actions" New topic
Author

Actionforms and Actions

Nicola Guy
Ranch Hand

Joined: Jun 23, 2004
Posts: 91
Hi i'm having problem connecting my Actions and ActionForms together, which is causing alot of problems when I try to read in user details. The ActionForm reads in the details from the jsp and the action (which is supposed to write the details to a file ) does what it supposed to when it doesn't have to get details from the Actionform. However when they are linked together it returns a NullPointerException meaning that the details aren't being sent or read or whatever from the ActionForm.

Any ideas what could be causing this??

Thanx
Pankaj Narang
Ranch Hand

Joined: Jun 07, 2004
Posts: 81
hi guy

Can you elaborate the problem you are getting.

If i am not wrong you are not getting data through your formbeans is it the situation, if i am correct in understanding , then i can help you only when you send me the snippets of your code, so that i can see where the problem exists.

But first check that , are using the taglibs in your jsp, then first check the properties with their getters and setters . also check the way you are trying to get the values.

if you can send me snippets of your jsp code and action classs, i may sove yout problems

Best Regards
Pankaj
Nicola Guy
Ranch Hand

Joined: Jun 23, 2004
Posts: 91
hi

heres the code you asked for. First it in parts of the struts-config that use the action and action form:

in the form bean defs:

<form-bean name="userForm"type="app.UserForm"/>

in the global forwards:

<forward name="confirmDetails"path="/confirmDetails.jsp" />

in the action-mappings

<actionpath="/adduser"
type="app.UserAction"
name="userForm"
scope="request"
input="/addDetails.jsp" >
</action>


THE FORM IS AS FOLLOWS:

package app;

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

public class UserForm extends ActionForm {

private String title = null;
private String firstName = null;
private String lastName = null;
private String email = null;
private String DOB = null;

public String getBirth() {
return DOB;
}

public String getEmail() {
return email;
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public String getTitle() {
return title;
}

public String getDOB() {
return DOB;
}

public void setEmail(String email) {
this.email = email;
}

public void setDOB(String DOB) {
this.DOB = DOB;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public void setTitle(String title) {
this.title = title;
}

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if(title == null || title.equals("")) {
errors.add("title", new ActionError("error.title"));
}

if(firstName == null || firstName.equals("")) {

errors.add("firstName", new ActionError("errors.firstName"));

}

if(lastName == null || lastName.equals("")) {

errors.add("lastName", new ActionError("errors.lastName"));

}

return errors;

}

}//End UserForm

AND THE ACTION IS:

package app;

import java.io.*;
import java.io.Writer.*;
import java.io.IOException;
import java.lang.StringBuffer.*;
import java.util.Vector;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;

public class UserAction extends Action {

private String title = null;
private String firstName = null;
private String lastName = null;
private String email = null;
private String DOB = null;
private File file = new File("/home/nguy/blah.txt");
StringBuffer contents = new StringBuffer();
String line;

public String readFile(){
BufferedReader input = null;

try {
input = new BufferedReader( new FileReader(file) );
String line = null;

while (( line = input.readLine()) != null) {
contents.append(line);
contents.append(";");

}//end of while
}//end try

catch (FileNotFoundException ex) {
ex.printStackTrace();
}//end catch

catch (IOException ex) {
ex.printStackTrace();
}//end catch

return contents.toString();
}// end of readFile

public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String newfile;
newfile = readFile();
String doc;
doc = getDetails();

BufferedWriter output = null;
output = new BufferedWriter( new FileWriter(file) );
output.write(doc);
output.close();

return (mapping.findForward("confirmDetails"));

}

}//End of UserAction

i know there are a few bits of the above code that may not be needed but are included to let me test the read/write functionality so i knew the problem wasn't there.

any help you can give would be great


nicola
Pankaj Narang
Ranch Hand

Joined: Jun 07, 2004
Posts: 81
hi guy

i got the code but didnt get wher is your getdetail()method written, where inyour code you are getting the values using formbean.

Can you tell me where you getdetails method written

i.e doc = getdeatails();

Best regards
Pankaj
Nicola Guy
Ranch Hand

Joined: Jun 23, 2004
Posts: 91
Sorry i have that changed but didn't save it its meant to be


doc = getFirstName();
Nicola Guy
Ranch Hand

Joined: Jun 23, 2004
Posts: 91
when i run it I get alot of ; in my file blah.txt
Pankaj Narang
Ranch Hand

Joined: Jun 07, 2004
Posts: 81
hi guy

Ther exists the problem try this one

UserForm frm = (UserForm)form;

doc = frm.getFirstname();

Try this i think this will solve the problem

Best Regards
Pankaj
Nicola Guy
Ranch Hand

Joined: Jun 23, 2004
Posts: 91
it won't compile because it cannot resolve symbol UserForm

UserForm frm = (UserForm)form;
^
Pankaj Narang
Ranch Hand

Joined: Jun 07, 2004
Posts: 81
hi guy

have you imported the class UserForm

import app.UserForm;

Regards
Pankaj
Nicola Guy
Ranch Hand

Joined: Jun 23, 2004
Posts: 91
I thought you didn't need that if they were in the same package anyway i added it and got the following errors ....

UserAction.java:3: cannot resolve symbol
symbol : class UserForm
location: package app
import app.UserForm;
^
UserAction.java:23: cannot resolve symbol
symbol : class UserForm
location: class app.UserAction
UserForm frm = (UserForm)form;
^
UserAction.java:23: cannot resolve symbol
symbol : class UserForm
location: class app.UserAction
UserForm frm = (UserForm)form;
^
UserAction.java:23: cannot resolve symbol
symbol : variable form
location: class app.UserAction
UserForm frm = (UserForm)form;
Nicola Guy
Ranch Hand

Joined: Jun 23, 2004
Posts: 91
I thought you didn't need that if they were in the same package anyway i added it and got the following errors ....

UserAction.java:3: cannot resolve symbol
symbol : class UserForm
location: package app
import app.UserForm;
^
UserAction.java:23: cannot resolve symbol
symbol : class UserForm
location: class app.UserAction
UserForm frm = (UserForm)form;
^
UserAction.java:23: cannot resolve symbol
symbol : class UserForm
location: class app.UserAction
UserForm frm = (UserForm)form;
^
UserAction.java:23: cannot resolve symbol
symbol : variable form
location: class app.UserAction
UserForm frm = (UserForm)form;
Pankaj Narang
Ranch Hand

Joined: Jun 07, 2004
Posts: 81
hi guy

Are using any IDE like eclipse for development, if not, try first compiling the UserForm.jave and then try compiling your action classes, becuase there is not error in syntax, and the form is declared in the signature of teh perform method.

Try compiling the UserForm first and then this class

I am not able to see any errors intye code now
Best Regards
Pankaj
Nicola Guy
Ranch Hand

Joined: Jun 23, 2004
Posts: 91
I'm not using eclipse or anything like that i'm just creating files and saving them, could it be a problem that i'm not using a development environment??
Pankaj Narang
Ranch Hand

Joined: Jun 07, 2004
Posts: 81
That can not be problem , but check whether the class file for the userform is present in the package you have imported , if it is not there the class file should be there in that package.

Regards
Pankaj
Nicola Guy
Ranch Hand

Joined: Jun 23, 2004
Posts: 91
It is there!! my stupid project at least I know the codes right so if I get my computer to work then I'll be able to use struts

thanks for you help

Nicola
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Actionforms and Actions