Lab 04 Struts 2 – OGNL,Validation and Internationalization Mục tiêu - Sử dụng OGNL, Validation và Internationalization xây dựng ứng dụng Web Phần I Bài tập step by step Bài tập: Phần 1:
Trang 1Lab 04 Struts 2 – OGNL,Validation and Internationalization
Mục tiêu
- Sử dụng OGNL, Validation và Internationalization xây dựng ứng dụng Web
Phần I Bài tập step by step
Bài tập:
Phần 1: OGNL: Sử dụng ValueStack, OGNL expression, converter đã được sử dụng để xây
dựng ứng dụng trong Lap
Phần 2: Xây dựng validation cho trang đăng ký User(register)
publicvoid validate() {
if this.userName.length() == 0) {
addFieldError("userName", "UserName is required");
} elseif this.userName.length() < 6 || this.userName.length() > 50) {
addFieldError("userName", "Length's username is required from 6 to 50 characters");
}
if this.password.length() == 0) {
addFieldError("password", "Password is required");
} elseif this.password.length() < 6 || this.password.length() > 30) {
addFieldError("password", "Length's password is required from 6 to 30 characters");
}
if this.confirmpassword.length() == 0) {
addFieldError("confirmpassword", "ConfirmPassword is required");
} elseif this.confirmpassword != this.password){
addFieldError("confirmpassword", "Password and ConfirmPassword not match");
}
if this.firstName.length() == 0) {
addFieldError("firstName", "FirstName is required");
} elseif this.firstName.length() > 50 ) {
addFieldError("firstName", "Length's firstName is required less 50 characters");
}
if this.lastName.length() == 0) {
addFieldError("lastName", "LastName is required");
} elseif this.lastName.length() > 50 ) {
addFieldError("lastName", "Length's lastName is required less 50 characters");
}
}
Phần 3: Sử dụng Internationalization i18n để xây dựng trang login đa ngôn ngữ
Step 1: Xây dựng trang login_lang.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
Trang 2<head>
<title><s:property value="getText('login.here')"/></title>
<s:head/>
<sx:head/>
</head>
<body>
<h1><s:property value="getText('login.here')"/></h1>
<s:url id="localeEN" namespace="/" action="locale" >
<s:param name="request_locale" >en</s:param>
</s:url>
<s:url id="localeFR" namespace="/" action="locale" >
<s:param name="request_locale" >fr</s:param>
</s:url>
<s:a href="%{localeEN}">English</s:a>
<s:a href="%{localeFR}">France</s:a>
<s:form action="login" theme="simple">
<s:textfield name="userName" key="login.username" size="30"/>
<s:password name="password" key="login.password" size="30"/>
<s:submit value="Login" key="login.submit"></s:submit>
</s:form>
<h4><s:property value="getText('login.link1')"/> <s:a href="register.jsp"><s:property
value="getText('login.link2')"/></s:a> <s:property value="getText('login.link3')"/></h4>
</body>
</html>
Step 2: Xây dựng global Resource Bundle
global_en.properties
login.here = Login
login.username = UserName
login.password = Password
login.submit = Login
login.link1 = Are you new users? Click
login.link2 = here
login.link3 = to create new user
global_fr.properties
login.here = Connectez-vous
login.user = Nom d'utilisateur
login.password = Mot de passe
login.submit = Connexion
login.link1 = Êtes-vous de nouveaux utilisateurs? Cliquez
login.link2 = ici
login.link3 = pour créer un nouvel utilisateur
Step 3: Cấu hình struts.xml
….
<constantname="struts.custom.i18n.resources"value="global"/>
…
<actionname="locale"class="Session1.Locale">
<resultname="success">/employee.jsp</result>
</action>
……
Struts.xml
<struts>
<constantname="struts.devMode"value="false"/>
<constantname="struts.custom.i18n.resources"value="global"/>
Trang 3<! Configuration for the default package >
<packagename="default"extends="struts-default">
<interceptors>
<interceptorname="mylogging"class="Interceptor.LoginInterceptor"></interceptor>
<interceptor-stackname="loggingStack">
<interceptor-refname="mylogging"/>
<interceptor-refname="defaultStack"/>
</interceptor-stack>
</interceptors>
<actionname="login"class="Session1.Login">
<interceptor-refname="loggingStack"></interceptor-ref>
<resultname="success">useriformation.jsp</result>
<resultname="error">loginfailure.jsp</result>
</action>
<actionname="locale"class="Session1.Locale">
<resultname="success">/employee.jsp</result>
</action>>
<actionname="register"class="Session2.Session2Action"method="register">
<resultname="input">register.jsp</result>
<resultname="success">success.jsp</result>
<resultname="error">error.jsp</result>
</action>
<actionname="SearchUser"class="Session2.Session2Action"method="searchUser"> <resultname="success">searchuser.jsp</result>
</action>
<actionname="deleteUser"class="Session2.Session2Action"method="deleteUser">
<interceptor-refname="defaultStack"/>
<resultname="success"type="chain">SearchUser</result>
</action>
<actionname="updateUser"class="Session2.Session2Action"method="updateUser">
<interceptor-refname="defaultStack"/>
<resultname="success"type="chain">SearchUser</result>
</action>
</package>
</struts>
Step 4: Build and Run