aspose file tools*
The moose likes Struts and the fly likes Can't Return action error from action class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Can Watch "Can New topic
Author

Can't Return action error from action class

Anil Mundra
Greenhorn

Joined: Nov 14, 2008
Posts: 21
hi,
i want to return the object of action errors from mu action class then how can i return it because i am already returning the
"return mapping.findForward(target);"

my code is shown below:-


package classes.com.virtualclass.struts;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

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

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;


public class LoginAction extends Action {

public static String getUserName(String login_name, String password, String table) throws Exception {

try {
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
} catch (Exception e1) {

}
String user = "This is only due to some tech problems";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

try {
conn = DriverManager.getConnection("jdbc b2:first", "", "");
stmt = conn.createStatement();

rs = stmt.executeQuery("SELECT * FROM ANIL."+table+" WHERE "
+ "ANIL."+table+".LOGIN_NAME = '"+login_name+"'"
+ " AND "+"ANIL."+table+".PASSWORD = '"+password+"'");


if (rs.next()) {
user = rs.getString("login_name");
}
} catch (SQLException e) {

}
conn.close();
return user;
}

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

String target = null;

String user = null;

// Extract attributes and parameters we will need

LoginForm loginForm = (LoginForm)form;

String login_name = loginForm.getLogin_name();
String password = loginForm.getPassword();
String table = loginForm.getLoginas();

user = getUserName(login_name,password,table);

if ((user.equals("This is only due to some tech problems" )) ) {
target = "failure";
ActionErrors errors = new ActionErrors();
errors.add("User Name",
new ActionError("errors.unknown.login_name"));
}
else {
if(table.equalsIgnoreCase("students"))
target="students";
else if(table.equalsIgnoreCase("faculty"))
target="faculty";
else
target="failure";

HttpSession session = request.getSession();
String no = "no";
session.setAttribute("USER", user);
request.setAttribute("no",no );
}


request.setAttribute("user",user);
return mapping.findForward(target);
}
}



Thanks in advance.
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
If you're attempting to return the errors so they can be used by html:errors then take a look at using the saveErrors method in org.apache.struts.action.Action. This will put the errors in the request where the errors tag expects to find them. If you are doing this for some other reason it will probably be an bigger problem since the struts framework requires a forward to continue is processing. If you give more details as to why you want to return the error messages I might be able to help more. However, I'll probably just recomend storing them in the request object.
Andy Lileston
Greenhorn

Joined: Nov 03, 2008
Posts: 10
Assuming you want to send them back to where they came from and display the errors:

Use saveErrors and make sure you have an "input" attribute set in your struts-config.xml for that action so that Struts knows where to send them back to.
Anil Mundra
Greenhorn

Joined: Nov 14, 2008
Posts: 21
thank you for comments.
yes i want to use that errors to display in my form by html:errors tag.
but if the error comes at the bean class then it is displaying the error by that tag but in action class it is not returning to the my form.
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
I'm a little confused. Are you saying that you used the saveErrors method in your action class and the errors still didn't display? If thats the case, please post the relevant parts of your action class.
Anil Mundra
Greenhorn

Joined: Nov 14, 2008
Posts: 21
Thank you very much.
I got it. Actually there was some coding mistake by me. Now it is working.
Thank you again.
 
 
subject: Can't Return action error from action class