CustomLoginAction.java
package com.login;
import org.apache.commons.lang.StringUtils;
import com.opensymphony.xwork.ActionSupport;
public class CustomLoginAction extends ActionSupport {
private User user;
public void setUser(User user) {
this.user = user;
}
public User getUser() {
return user;
}
public String testexecute() {
if (StringUtils.equals(user.getUserName(),"test") &&
StringUtils.equals(user.getPwd(),"test")) {
return SUCCESS;
}
else
return "failure";
}}
LoginAction.java
package com.login;
import org.apache.commons.lang.StringUtils;
import com.opensymphony.xwork.ActionSupport;
import com.opensymphony.xwork.Validateable;
public class LoginAction extends ActionSupport implements Validateable{
private User user;
public void setUser(User user) {
this.user = user;
}
public User getUser() {
return user;
}
public String testexecute() {
if (StringUtils.equals(user.getUserName(),"akbar") &&
StringUtils.equals(user.getPwd(),"santhi")) {
return SUCCESS;
}
else
return "failure";
}
public void validate() {
User user = getUser();
if (StringUtils.isBlank(user.getUserName())) {
addActionError(getText("user.name.empty"));
}
if (StringUtils.isBlank(user.getPwd())) {
addActionError(getText("user.pwd.empty"));
}
}
}
User.java
package com.login;
public class User {
String userName="";
String pwd="";
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
Validation. xml file name must be with the name of action.(i.e if action name is test then validation.xml name is test-validation.xml)
CustomLoginAction-validation.xml
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="user.userName" >
<field-validator type="requiredstring">
<message key="user.name.empty">resource not found</message>
</field-validator>
</field>
<field name="user.pwd">
<field-validator type="requiredstring">
<message key="user.pwd.empty"/>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">5</param>
<message key="user.pwd.length"/>
</field-validator>
</field>
</validators>
Validation properties file name must be with the name of action.(i.e if action name is test then properties file name is test.properties)
CustomLoginAction.properties
user.name.empty=Enter your Name
user.pwd.empty= Enter your Password
user.pwd.length=Password must be atleast ${minLength} characters long
LoginAction.properties
user.name.empty=Please enter your Name
user.pwd.empty=Please enter your Password
validators.xml
Here you can specify your own validator.
<validators>
<validator name="required"
class="com.opensymphony.xwork.validator.validators.RequiredFieldValidator"/>
<validator name="requiredstring"
class="com.opensymphony.xwork.validator.validators.RequiredStringValidator"/>
<validator name="int"
class="com.opensymphony.xwork.validator.validators.IntRangeFieldValidator"/>
<validator name="date"
class="com.opensymphony.xwork.validator.validators.DateRangeFieldValidator"/>
<validator name="expression"
class="com.opensymphony.xwork.validator.validators.ExpressionValidator"/>
<validator name="fieldexpression"
class="com.opensymphony.xwork.validator.validators.FieldExpressionValidator"/>
<validator name="email"
class="com.opensymphony.xwork.validator.validators.EmailValidator"/>
<validator name="url" class="com.opensymphony.xwork.validator.validators.URLValidator"/>
<validator name="visitor"
class="com.opensymphony.xwork.validator.validators.VisitorFieldValidator"/>
<validator name="conversion"
class="com.opensymphony.xwork.validator.validators.ConversionErrorFieldValidator"/>
<validator name="stringlength"
class="com.opensymphony.xwork.validator.validators.StringLengthFieldValidator"/>
<validator name="customvalidator"
class="com.login.CustomLoginAction"/>
</validators>
xwork.xml
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN"
"http://www.opensymphony.com/xwork/xwork-1.1.dtd">
<xwork>
<include file="webwork-default.xml"/>
<package name="default" extends="webwork-default">
<default-interceptor-ref name="validationWorkflowStack"/>
<action name="login" class="LoginAction1" method="testexecute">
<result name="success">/success.jsp</result>
<result name="failure">/failure.jsp</result>
<result name="input">/index.jsp</result>
</action>
<action name="CustomLoginAction1" class="CustomLoginAction" method="testexecute">
<result name="success">/success.jsp</result>
<result name="failure">/failure.jsp</result>
<result name="input">/index1.jsp</result>
</action>
</package>
</xwork>
You can use struts tags here in all jsps like this .
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:form action="CustomLoginAction1">
index.jsp
<%@ taglib uri="webwork" prefix="ww" %>
<h1>
Login Form
</h1>
<hr/>
<%@ include file="/header.jsp" %>
<ww:form action="login" method="POST">
<ww:textfield name="user.userName" label="User"/>
<ww:password name="user.pwd" label="Password"/>
<ww:submit value="Submit"/>
</ww:form>
header.jsp
<!--
http://wiki.opensymphony.com/display/WW/WebWork+2+UI+Tag+Guide?decorator=printable
-->
<ww:if test="hasErrors()">
<p style="color: red;">
<b>Errors:</b>
<ul>
<ww:if test="hasActionErrors()">
<ww:iterator value="actionErrors">
<li style="color: red;"><ww:property/></li>
</ww:iterator>
</ww:if>
<ww:if test="hasFieldErrors()">
<ww:iterator value="fieldErrors">
<ww:iterator value="value">
<li style="color: red;"><ww:property/></li>
</ww:iterator>
</ww:iterator>
</ww:if>
</ul>
</p>
</ww:if>
index1.jsp
<%@ taglib uri="webwork" prefix="ww" %>
<h1>
Login Form
</h1>
<hr/>
<%@ include file="/header.jsp" %>
<ww:form action="CustomLoginAction1" method="POST">
<table>
<tr>
<td>Name</td>
<td><ww:textfield theme="simple" name="user.userName"/></td>
</tr>
<tr>
<td>Password</td>
<td><ww:password theme="simple" name="user.pwd"/></td>
</tr>
<tr>
<td></td>
<td><ww:submit theme="simple" value="Submit"/></td>
</tr>
</table>
</ww:form>
success.jsp
<%@ taglib uri="webwork" prefix="ww" %>
<p><b>
Hi <ww:property value="user.userName"/> <br/>
You Successfully Logged
</b></p>
failure.jsp
<h2>
Login Failure Please verify your UserName and Password
</h2>
1 comment:
how to use custom validation using annotation in struts ?
Post a Comment