日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
WebWork用戶登陸簡單實現(xiàn)

WebWork用戶登陸主要實現(xiàn)如下功能:

1、login.jsp中就name,password字段

2、用戶名與密碼不能為空!如果空給予提示(使用Webwork驗證,國際化)

2、當用戶名為:xiaomaha密碼為123跳轉(zhuǎn)到seccess.jsp頁面否則跳轉(zhuǎn)到defeat.jsp頁面

WebWork用戶登陸***步:導包

版本為:webwork-2.2.5

http://www.opensymphony.com/webwork

可以下載所需要的包

1、webwork-2.2.5.jar

2、lib目錄下default中的所有包

WebWork用戶登陸第二步:配置web.xml

com.opensymphony.webwork.dispatcher.FilterDispatcher  為WebWork中的Servlet控制器

 
 
 
  1. 〈?xmlversion="1.0"encoding="UTF-8"?〉  
  2. 〈web-appversion="2.4" 
  3. xmlns="http://java.sun.com/xml/ns/j2ee" 
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee  
  6. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"〉  
  7. 〈filter〉  
  8. 〈filter-name〉webwork〈/filter-name〉  
  9. 〈filter-class〉  
  10. com.opensymphony.webwork.dispatcher.FilterDispatcher  
  11. 〈/filter-class〉  
  12. 〈/filter〉  
  13. 〈filter-mapping〉  
  14. 〈filter-name〉webwork〈/filter-name〉  
  15. 〈url-pattern〉*.action〈/url-pattern〉  
  16. 〈/filter-mapping〉  
  17. 〈/web-app〉  

Filter是一個過濾器,頁面以*.action的請求都交給WebWork的servlet控制器

WebWork用戶登陸第三步:創(chuàng)建Action

首先創(chuàng)建一個Action,WebWork中Action其實就是一個普通的JAVA類。

只要寫一個execute()方法 返回

String類型,在配置文件定義它.那么它就是一個Action。相當與Struts1中的Action!WebWork實現(xiàn)了與Servlet解偶合,方便測試`本人特別喜歡?。?/p>

但是如果需要方便的開發(fā)那么繼承ActionSupport,覆寫它的execute()注意它的返回值是一個String,不是一個ActionForword

WebWork中可以使用兩中方法實現(xiàn)驗證

1、在Action中繼承ActionSupport,覆寫validate()方法(本人給予注釋了!)

2、配置文件實現(xiàn),本例使用配置文件,注意必須將驗證的配置文件寫到與自定義Action同包下!

 
 
 
  1. package com.xiaomaha.action;  
  2. import com.opensymphony.xwork.ActionSupport;  
  3. public class LoginAction extends   
  4. ActionSupport ...  
  5. {  
  6. private String userName;  
  7. private String passWord;  
  8. public String getPassWord() ...  
  9. {  
  10. return passWord;  
  11. }  
  12. public void setPassWord(String passWord) ...  
  13. {  
  14. this.passWord = passWord;  
  15. }  
  16. public String getUserName() ...{  
  17. return userName;  
  18. }  
  19. public void setUserName(String userName) ...  
  20. {  
  21. this.userName = userName;  
  22. }  
  23. public String execute() throws Exception ...  
  24. {  
  25. String path = null ;   
  26. if("xiaomaha".equalsIgnoreCase(this.userName)&&"123".  
  27. equals(this.passWord))...{  
  28. path =this.SUCCESS;  
  29. }else...{  
  30. path =this.ERROR;  
  31. }  
  32. return path;  
  33. }  
  34. //public void validate() {  
  35. //if("".equals(this.userName)||this.userName==null){  
  36. ///*addFieldError()相當與Struts1中的ActionErrors*  
  37. // * 如果要實現(xiàn)國際化那么需要getText()它用于讀取資源文件  
  38. // * getText("name.null")對應資源文件中的key,它會找到value  
  39. // */  
  40. //this.addFieldError("userName", getText("name.null"));  
  41. //}  
  42. //  
  43. //if("".equals(this.passWord)||this.passWord==null){  
  44. //this.addFieldError("passWord", getText("pass.null"));  
  45. //}  
  46. //}  
  47. }  

WebWork用戶登陸第四步:創(chuàng)建xwork.xml

xwork.xml有點像Struts1中的struts-config.xml

xwork.xml文件必須放在類路徑下   WEB-INF/classes/    之下

WebWork的Servlet控制器會去找到它并解析它,路徑一定不要放錯了,名字一定不要錯!

還要提示注意,在WebWork中所有的Action標簽必須在package標簽下,package是action的父標簽

 
 
 
  1. 〈 include file="webwork-default.xml"〉   
  2. 〈 /include〉   
  3. 會去找到WebWork中JAR文件中的webwork-default.xml,  
  4. 必寫 〈 !DOCTYPE xwork PUBLIC   
  5. "-//OpenSymphony Group//XWork 1.1.1//EN"   
  6. "http://www.opensymphony.com/xwork/xwork-1.1.1.dtd"〉   
  7. 〈 xwork〉   
  8. 〈 !-- include加載webwork中的webwork-default.xml  
  9. 是必須的不然會拋出  
  10. servlet jsp threw exception  
  11.  --〉   
  12. 〈 include file="webwork-default.xml"〉 〈 /include〉   
  13. 〈 package name="webworkdemo" extends="webwork-default"〉   
  14. 〈 action name="login"   
  15. class="com.xiaomaha.action.LoginAction"〉   
  16. 〈 result name="success"〉 /success.jsp〈 /result〉   
  17. 〈 result name="error"〉 /error.jsp〈 /result〉   
  18. 〈 result name="input"〉 /login.jsp〈 /result〉   
  19. 〈 /action〉   
  20. 〈 /package〉   
  21. 〈 /xwork〉   
 
 
 
  1. 〈 result name="success"〉 /success.jsp〈 /result〉 

如果Action中的execute()方法返回success字符串那么,跳轉(zhuǎn)到success.jsp頁面中

 
 
 
  1. 〈 result name="input"〉 /login.jsp〈 /result〉 

和Struts1中的input一樣都是顯示錯誤消息,但在WebWork中不需要定義返回錯誤的標簽。

因為WebWork中定義的標簽中〈 w:form〉 已經(jīng)實現(xiàn)了錯誤驗證

WebWork用戶登陸第五步:驗證配置文件

在自定義的Action同包下建立一個與Action一樣名字+validation.xml文件

例如:

Action:LoginAction.java 

配置文件:LoginAction-validation.xml

 
 
 
  1. 〈 ?xml version="1.0" encoding="UTF-8"?〉  
  2. 〈 !DOCTYPE validators PUBLIC   
  3. "-//OpenSymphony Group//XWork Validator 1.0//EN"   
  4. "http://www.opensymphony.com/xwork/  
  5. xwork-validator-1.0.dtd" 〉  
  6. 〈 validators〉  
  7. 〈 field name="userName"〉  
  8. 〈 field-validator type="requiredstring" 〉  
  9. 〈 message key="name.null"〉〈 /message〉  
  10. 〈 /field-validator〉  
  11. 〈 /field〉  
  12. 〈 field name="passWord"〉  
  13. 〈 field-validator type="requiredstring"〉  
  14. 〈 message key="pass.null"〉〈 /message〉  
  15. 〈 /field-validator〉  
  16. 〈 /field〉  
  17. 〈 /validators〉  

name.null 、pass.null  對應資源文件的key

requiredstring  代表:不能為空,是必須的

WebWork用戶登陸第六步:創(chuàng)建國際化資源文件

創(chuàng)建一個Message.properties  ,里面不能寫中文。

需要在dos窗口中進行轉(zhuǎn)碼

命令為native2ascii Message.properties  Message.properties_zh_cn.properties  就生成了一個新的文件,刪除Message.properties

瀏覽器如果為中文,那么它會找到 Message.properties_zh_cn.properties ,為了方便我就不定義其它語言的配置文件咯!

提示:Message  可以任意修改!

例:比如需要定義支持英語的可以這樣寫   Message_en_US.properties

Message.properties_zh_cn.properties如下:

 
 
 
  1. name.null =   
  2. \u7528\u6237\u540d\u4e0d\u80fd\u4e3a\u7a7a\uff01  
  3. pass.null =  
  4.  \u5bc6\u7801\u4e0d\u80fd\u4e3a\u7a7a\uff01  

WebWork用戶登陸***一步:加載上面的資源文件

在類路徑下創(chuàng)建 webwork.properties ,WebWork會在類路徑找到它并且解析它!

里面添加如下:

webwork.custom.i18n.resources=Message

Ok一切搞定咯!


當前名稱:WebWork用戶登陸簡單實現(xiàn)
網(wǎng)站URL:http://www.dlmjj.cn/article/dhgdhcs.html