隨筆-8  評論-4  文章-0  trackbacks-0

          Struts 2內嵌了Dojo工具包,實現對Ajax的支持。下面是一個用戶名和密碼都是Admin的Login應用。

           1、在struts.xml中加入一個Action mapping

          xml 代碼

           

          1. <action name="showAjaxLoginForm">  
          2.        <result>/pages/ajaxlogin.jspresult>  
          3. action>  
          4.   
          5. <action name="ajaxLogin" class="net.roseindia.Login">  
          6.        <result name="input">/pages/ajaxlogin.jspresult>  
          7.        <result name="error">/pages/ajaxlogin.jspresult>  
          8.        <result>/pages/ajaxloginsuccess.jspresult>  
          9. > 

           

           2、用Ajax編寫一個Login頁面ajaxlogin.jsp

           這個頁面使用了 標簽, 這個標簽能通過Ajax tags載入頁面內容。jsp頁面還使用了標簽,這個標簽可以利用Ajax來更形頁面元素和提交一個form。當出現錯誤是,標簽執行并顯示錯誤信息。

          xml 代碼 
          1. <%@ taglib prefix="s" uri="/struts-tags"%>  
          2. <html>  
          3.   <head>  
          4.     <s:head theme="ajax" debug="true"/>  
          5.   head>  
          6.   <body>  
          7.     <s:div id="loginDiv" theme="ajax">  
          8.     <div style="width: 300px;border-style: solid">  
          9.       <s:form action="ajaxLogin"  validate="true">  
          10.         <tr>  
          11.           <td colspan="2">  
          12.             Login   
          13.           td>  
          14.         tr>  
          15.         <tr>  
          16.           <td colspan="2">  
          17.             <s:actionerror />  
          18.             <s:fielderror />  
          19.           td>  
          20.         tr>     
          21.           <s:textfield name="username" label="Login name"/>  
          22.           <s:password name="password" label="Password"/>  
          23.           <s:submit theme="ajax" targets="loginDiv" notifyTopics="/ajaxLogin"/>       
          24.       s:form>  
          25.     div>  
          26.     s:div>  
          27.   body>  
          28. html>   

           

           3、編寫一個驗證用戶名和密碼的Action類Login.java

          如果驗證成功返回SUCCESS,失敗就返回ERROR

          java 代碼 
          1. package net.roseindia;   
          2.   
          3. import com.opensymphony.xwork2.ActionSupport;   
          4. import java.util.Date;   
          5.   
          6. /**  
          7.  * Validate a user login.  
          8.  */  
          9. public class Login extends ActionSupport {   
          10.   
          11.     public String execute() throws Exception {   
          12.         System.out.println("Validating login ... ...");   
          13.         System.out.println("User = " + getUsername());   
          14.         if (!getUsername().equals("Admin") || !getPassword().equals("Admin")) {   
          15.             System.out.println("Validating error ! User = " + getUsername());   
          16.             addActionError("Invalid user name or password! Please try again!");   
          17.             return ERROR;   
          18.         } else {   
          19.             System.out.println("Validating success !");   
          20.             return SUCCESS;   
          21.         }   
          22.     }   
          23.   
          24.     // ---- Username property ----   
          25.   
          26.     /**  
          27.      * Field to store User username. 
          28.      */  
          29.     private String username = null;   
          30.   
          31.     public String getUsername() {   
          32.         return username;   
          33.     }   
          34.   
          35.     public void setUsername(String value) {   
          36.         username = value;   
          37.     }   
          38.   
          39.     // ---- Username property ----   
          40.   
          41.     /**  
          42.      * Field to store User password. 
          43.      */  
          44.     private String password = null;   
          45.   
          46.     public String getPassword() {   
          47.         return password;   
          48.     }   
          49.   
          50.     public void setPassword(String value) {   
          51.         password = value;   
          52.     }   
          53.   
          54. }   

           4、編寫一個登錄成功頁面ajaxloginsuccess.jsp

          xml 代碼
          1. <html>  
          2.   <head>  
          3.     <title>Login Successtitle>  
          4.   head>  
          5.   <body>  
          6.     <p align="center"><font color="#000080" size="5">Login Successful !font>p>  
          7.     <h1> Welcome to <%=request.getParameter("username")%>  h1>  
          8.   body>  
          9. html>   

          5、訪問下面連接 http://localhost:8080/s2ajax/showAjaxLoginForm.action

          posted on 2007-12-05 00:01 怡眾科技 閱讀(987) 評論(0)  編輯  收藏 所屬分類: 轉載區
          主站蜘蛛池模板: 扶余县| 武夷山市| 白沙| 内丘县| 赤壁市| 佳木斯市| 同德县| 屏东市| 天等县| 安图县| 汨罗市| 河北省| 磐安县| 柯坪县| 无锡市| 新民市| 杭锦旗| 福建省| 抚松县| 绥滨县| 靖远县| 芜湖县| 宜都市| 连城县| 交口县| 乌苏市| 九龙县| 盱眙县| 华亭县| 菏泽市| 通辽市| 金乡县| 邯郸县| 辽阳市| 临武县| 米脂县| 博客| 蕲春县| 宝兴县| 武安市| 新民市|