<action name=”loginForm”
path=”/Logon”
scope=”request”
type=”jeni3.struts.user.action.LoginAction”
input=”/Login.do”
validate=”true”>
<forward name=”success” path=”/WEB-INF/view_pages/Success.jsp”/>
<forward name=”input” path=”/WEB-INF/view_pages/LoginForm.jsp”/>
</action>
• Pada class LoginForm ganti superclass nya dengan ValidatorForm, sehingga kodenya menjadi seperti berikut:
package jeni3.struts.user.actionform;
import org.apache.struts.validator.ValidatorForm;
/**
* @author mee_andto@yahoo.com
* @version 0.5
*/
public class LoginForm extends ValidatorForm{
private String username;
private String password;
public String getUsername(){
return username;
}
public void setUsername(String username){
this.username = username;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password = password;
}
}
• Definisikan elemen-elemen form yang akan divalidasi dalam file
validation.xml seperti berikut:
<form-validation>
<formset>
<form name=”loginForm”>
<field
property=”username”
depends=”required,minlength”>
<arg position=”0″ key=”loginForm.username”/>
<arg position=”1″ name=”minlength”
key=”${var:minlength}” resource=”false”/>
<var>
<var-name>minlength</var-name>
<var-value>4</var-value>
</var>
</field>
<field
property=”password”
depends=”required,mask”>
<arg position=”0″ key=”loginForm.password”/>
<var>
<var-name>mask</var-name>
<var-value>^[0-9a-zA-Z]*$</var-value>
</var>
</field>
</form>
</formset>
</form-validation>
• Buka file ApplicationResource.properties dalam package jeni3.struts dan tambahkan key dan value untuk username dan password, seperti berikut: loginForm.username=Username loginForm.password=Password
• Buka kembali file LoginForm.jsp dan tambahkan tag html:errors untuk memunculkan pesan kesalahan validasi, sehingga kodenya menjadi seperti berikut:
<%@taglib uri=”http://jakarta.apache.org/struts/tags-html”
prefix=”html”%>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-
8″>
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<font color=”red”><html:errors /></font>
<html:form action=”Logon.do” method=”POST”>
Username : <html:text property=”username”/> <br/>
Password : <html:password property=”password”/> <br/>
<html:reset/>
<html:submit />
</html:form>
</body>
</html>
• Run Run Project
path=”/Logon”
scope=”request”
type=”jeni3.struts.user.action.LoginAction”
input=”/Login.do”
validate=”true”>
<forward name=”success” path=”/WEB-INF/view_pages/Success.jsp”/>
<forward name=”input” path=”/WEB-INF/view_pages/LoginForm.jsp”/>
</action>
• Pada class LoginForm ganti superclass nya dengan ValidatorForm, sehingga kodenya menjadi seperti berikut:
package jeni3.struts.user.actionform;
import org.apache.struts.validator.ValidatorForm;
/**
* @author mee_andto@yahoo.com
* @version 0.5
*/
public class LoginForm extends ValidatorForm{
private String username;
private String password;
public String getUsername(){
return username;
}
public void setUsername(String username){
this.username = username;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password = password;
}
}
• Definisikan elemen-elemen form yang akan divalidasi dalam file
validation.xml seperti berikut:
<form-validation>
<formset>
<form name=”loginForm”>
<field
property=”username”
depends=”required,minlength”>
<arg position=”0″ key=”loginForm.username”/>
<arg position=”1″ name=”minlength”
key=”${var:minlength}” resource=”false”/>
<var>
<var-name>minlength</var-name>
<var-value>4</var-value>
</var>
</field>
<field
property=”password”
depends=”required,mask”>
<arg position=”0″ key=”loginForm.password”/>
<var>
<var-name>mask</var-name>
<var-value>^[0-9a-zA-Z]*$</var-value>
</var>
</field>
</form>
</formset>
</form-validation>
• Buka file ApplicationResource.properties dalam package jeni3.struts dan tambahkan key dan value untuk username dan password, seperti berikut: loginForm.username=Username loginForm.password=Password
• Buka kembali file LoginForm.jsp dan tambahkan tag html:errors untuk memunculkan pesan kesalahan validasi, sehingga kodenya menjadi seperti berikut:
<%@taglib uri=”http://jakarta.apache.org/struts/tags-html”
prefix=”html”%>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-
8″>
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<font color=”red”><html:errors /></font>
<html:form action=”Logon.do” method=”POST”>
Username : <html:text property=”username”/> <br/>
Password : <html:password property=”password”/> <br/>
<html:reset/>
<html:submit />
</html:form>
</body>
</html>
• Run Run Project
Tidak ada komentar:
Posting Komentar