Here I am explaining simple Login Form validation in struts .
LoginForm
package com.simplevalidation;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.ValidatorForm;
/**
*
* @author Prasanthi
*
*/
public class LoginForm extends ValidatorForm {
private String name;
private String pwd;
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ActionErrors validate(ActionMapping map,HttpServletRequest req)
{
ActionErrors errors=new ActionErrors();
if(name == null || name.length()== 0)
errors.add("name", new ActionError("error.name"));
if(pwd == null || pwd.length()== 0)
errors.add("pwd", new ActionError("error.pwd"));
return errors;
}
}
Login Action
package com.simplevalidation;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
*
* @author Prasanthi
*
*/
public class LoginAction extends Action{
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws IOException
{
return mapping.findForward("success");
}
}
ApplicationResources.properties
error.name = Name Is Required
error.pwd = Password Is Required
struts-config.xml
<struts-config>
<form-beans>
<form-bean name="loginForm"
type="com.simplevalidation.LoginForm" />
</form-beans>
<action-mappings>
<action path="/Login"
type="com.simplevalidation.LoginAction"
name="loginForm"
validate="true"
scope="request"
input="/LoginForm.jsp">
<forward name ="success" path="/LoginSuccess.jsp"/>
</action>
</action-mappings>
<message-resources parameter="ApplicationResources" />
</struts-config>
web.xml
<web-app>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<welcome-file-list>
<welcome-file>LoginForm.jsp</welcome-file>
</welcome-file-list>
</web-app>
LoginForm.jsp
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<html>
<body>
<br><br>
<center><h2><u>Login Form</u></h2><br>
<html:form action="/Login" >
Name<html:text property="name"/><br>
<font color="red"> <html:errors property="name" /></font>
<br>
Password <html:password property="pwd"/><br>
<font color="red"> <html:errors property="pwd" /></font>
<br>
<html:submit property="submit" value="Login"/>
</html:form>
</body>
</html>
LoginSuccess.jsp
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<html:html>
<body>
<BR>
<BR>
<center><h2>
Successfully Logged.
</body>
</html:html>
No comments:
Post a Comment