File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Can Watch "Can New topic

Can't Return action error from action class

Anil Mundra

Joined: Nov 14, 2008
Posts: 21
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:-


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 {
} 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 ( {
user = rs.getString("login_name");
} catch (SQLException e) {

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 {
else if(table.equalsIgnoreCase("faculty"))

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

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

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

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

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.
I agree. Here's the link:
subject: Can't Return action error from action class
Similar Threads
form bean doesn't come into picture
confusing compile error
problem in Developing Struts small Apllication