Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Help required ( Struts Validation ) Urgently

  Asked By: Bonni    Date: Jan 10    Category: Java    Views: 756
  

I am trying my hand in struts validation thru form bean. I
have been following simple steps but getting same error
ClassCastException in org.apache.struts.ActionMessage im getting an
error on the line numbner where i have written tag <html:errors
\>... I am stuck at this problem since long .. please help me out.
im attaching code along with this mail. and dop explain me if like i
have validation on empty username and i leave it empty then the
error will be displayed on the same page. so do i need to forward
back this page from action class or it will forward it
automatically.whenever i leave username blank???? Please help to
clear my concepts... find code below. and do write to me as soon as
u can... and im not even getting the jsp page pre populated with the
messages which i have done thru form bean... Please help



Applicationresources.property

errors.prefix=<li>
errors.suffix=</li>
errors.footer=</ul><hr>
errors.header=<h3><font color="red">Validation Error</font></h3>You
must correct the following error(s) before proceeding:<ul>
username.instructions=Enter correct username.
password.instructions=Enter correct password.
errors.username.required=Username Required.
hello=Hi {0} How are you


InputUserAction class

package view;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import java.io.IOException;
import javax.servlet.ServletException;

public class InputuserAction extends Action
{
/**
* This is the main action called from the Struts framework.
* @param mapping The ActionMapping used to select this instance.
* @param form The optional ActionForm bean for this request.
* @param request The HTTP Request we are processing.
* @param response The HTTP Response we are processing.
* @throws javax.servlet.ServletException
* @throws java.io.IOException
* @return
*/
public ActionForward perform(ActionMapping mapping, ActionForm
form, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
return mapping.findForward("username.success");
}
}


Login page form bean ( This is second form bean which is pre
populating the form from form bean)

package view;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.util.MessageResources;

public class loginpageformbean extends ActionForm
{
private String username=null;
private String password=null;
/**
* Reset all properties to their default values.
* @param mapping The ActionMapping used to select this instance.
* @param request The HTTP Request we are processing.
*/
public void reset(ActionMapping mapping, HttpServletRequest
request)
{
ActionServlet servlet=this.getServlet();
MessageResources messageResources=servlet.getResources();
username=messageResources.getMessage("username.instructions");
password=messageResources.getMessage("password.instructions");

}

/**
* Validate all properties to their default values.
* @param mapping The ActionMapping used to select this instance.
* @param request The HTTP Request we are processing.
* @return ActionErrors A list of all errors found.
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request)
{
return super.validate(mapping, request);
}
}



valid action class ( This is second action class )

package view;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import java.io.IOException;
import javax.servlet.ServletException;

public class ValidAction extends Action
{
/**
* This is the main action called from the Struts framework.
* @param mapping The ActionMapping used to select this instance.
* @param form The optional ActionForm bean for this request.
* @param request The HTTP Request we are processing.
* @param response The HTTP Response we are processing.
* @throws javax.servlet.ServletException
* @throws java.io.IOException
* @return
*/
public ActionForward execute(ActionMapping mapping, ActionForm
form, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
System.out.println("success");
return mapping.findForward("success");
}
}


validformbean ( this is the form bean which save errors )

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

public class ValidFormBean extends ActionForm
{
private String username=null;
private String password=null;
/**
* Reset all properties to their default values.
* @param mapping The ActionMapping used to select this instance.
* @param request The HTTP Request we are processing.
*/
public void reset(ActionMapping mapping, HttpServletRequest
request)
{
super.reset(mapping, request);
}

public void setUsername(String username)
{
this.username=username;
}
public String getUsername()
{
return username;
}
public void setPassword(String password)
{
this.password=password;
}
public String password()
{
return password;
}
/**
* Validate all properties to their default values.
* @param mapping The ActionMapping used to select this instance.
* @param request The HTTP Request we are processing.
* @return ActionErrors A list of all errors found.
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request)
{
ActionErrors errors=new ActionErrors();

if(username.trim().length()==0)
{
errors.add("username",new ActionMessage
("errors.username.required"));
}
return errors;
}
}




Struts config.xml

<?xml version = '1.0' encoding = 'windows-1252'?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD
Struts Configuration
1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-
config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="form-bean1" type="view.loginpageformbean"/>
<form-bean name="form-bean2" type="view.ValidFormBean"/>
</form-beans>
<action-mappings>
<action path="/inputuser" type="view.InputuserAction" name="form-
bean1"
scope="request" input="/start.jsp" validate="false" >
<forward name="username.success" path="/loginpage.jsp"/>
</action>
<action path="/valid" type="view.ValidAction" name="form-bean2"
scope="request"
input="/loginpage.jsp" validate="true" >
<forward name="success" path="/success.jsp"/>
</action>
</action-mappings>
<message-resources parameter="view.ApplicationResources"/>
</struts-config>




/**Second form to be diplayed which contains username and password*/


<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ page contentType="text/html;charset=windows-1252"%>
<html:html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-
1252">
<title>untitled</title>
</head>
<body>
<html:form action="valid.do">
<html:errors />
<bean:message key="hello" arg0="Bhuwan" />
<P>UserName
<input type="text" name="username"/>
</P>
<P>PassWord
<input type="password" name="password"/>
</P>
<P>
<input type="submit" value="Submit"/>
</P>
</html:form>
</body>
</html:html>



/**First form to be displayed which simply contains one submit
buttons */

<%@ page contentType="text/html;charset=windows-1252"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-
1252">
<title>untitled</title>
</head>
<body>
<form name="Actionform" action="inputuser.do">
<input type="submit" value="Submit"/>
</form>
</body>
</html>

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on Help required ( Struts Validation ) Urgently Or get search suggestion and latest updates.




Tagged: