随笔-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 怡众科技 阅读(974) 评论(0)  编辑  收藏 所属分类: 转载区

只有注册用户登录后才能发表评论。


网站导航: