Tải bản đầy đủ (.doc) (3 trang)

Thực hành Strust JSF Lab 4

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (79.1 KB, 3 trang )

Architecting Applications for the Web

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: 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)
public void validate() {
if (this.userName.length() == 0) {
addFieldError("userName", "UserName is required");
} else if (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");
} else if (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");
} else if (this.confirmpassword != this.password){
addFieldError("confirmpassword", "Password and ConfirmPassword not match");
}
if (this.firstName.length() == 0) {


addFieldError("firstName", "FirstName is required");
} else if (this.firstName.length() > 50 ) {
addFieldError("firstName", "Length's firstName is required less 50 characters");
}
if (this.lastName.length() == 0) {
addFieldError("lastName", "LastName is required");
} else if (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>

IT Research Department

@BKAP 2015

Trang 1 / 3


Architecting Applications for the Web
<html>
<head>
<title><s:property value="getText('login.here')"/></title>
<s:head/>
<sx:head/>

</head>
<body>

<s:property value="getText('login.here')"/>


<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>

<s:property value="getText('login.link1')"/> <s:a href="register.jsp">value="getText('login.link2')"/></s:a> <s:property value="getText('login.link3')"/>


</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
….

<constant name="struts.custom.i18n.resources" value="global" />
….
<action name="locale" class="Session1.Locale">
<result name="success">/employee.jsp</result>
</action>
……

Struts.xml

<struts>
<constant name="struts.devMode" value="false" />
<constant name="struts.custom.i18n.resources" value="global" />

IT Research Department

@BKAP 2015


Trang 2 / 3


Architecting Applications for the Web
<!-- Configuration for the default package. -->

<interceptors>
<interceptor name="mylogging" class="Interceptor.LoginInterceptor"></interceptor>
<interceptor-stack name="loggingStack">
<interceptor-ref name="mylogging"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<action name="login" class="Session1.Login">
<interceptor-ref name="loggingStack"></interceptor-ref>
<result name="success">useriformation.jsp</result>
<result name="error">loginfailure.jsp</result>
</action>
<action name="locale" class="Session1.Locale">
<result name="success">/employee.jsp</result>
</action>>
<action name="register" class="Session2.Session2Action" method="register">
<result name="input">register.jsp</result>
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
</action>
<action name="SearchUser" class="Session2.Session2Action" method="searchUser">
<result name="success">searchuser.jsp</result>
</action>

<action name="deleteUser" class="Session2.Session2Action" method="deleteUser">
<interceptor-ref name="defaultStack"/>
<result name="success" type="chain">SearchUser</result>
</action>
<action name="updateUser" class="Session2.Session2Action" method="updateUser">
<interceptor-ref name="defaultStack"/>
<result name="success" type="chain">SearchUser</result>
</action>
</package>
</struts>

Step 4: Build and Run

IT Research Department

@BKAP 2015

Trang 3 / 3



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×