谁能给个最最最最最最最最最最最最最最最最最最最最最最最最最最简单的Struts2+AJAX+JSP的验证实例吗?
最主要的是AJAX请求到Action时候的URL怎么写,action又要怎么配置。就七十分!全给一个人吧

解决方案 »

  1.   

    这是个做登录验证帐号密码是否正确,然后返回到JSP页面的Action代码
    package com.softeem.mail.action;import java.io.ByteArrayInputStream;
    import java.io.InputStream;import javax.servlet.http.Cookie;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    import com.softeem.mail.entity.Users;
    import com.softeem.mail.factory.Factory;
    import com.softeem.mail.service.UsersService;public class UserLoginAction extends ActionSupport{
    private static final long serialVersionUID = 1L;

    private String name;
    private String password;
    private String remerber_password; public InputStream getLogin() {

    UsersService usersService = Factory.getUsersService();
    Users users = usersService.checkLogin(name, password);

    ByteArrayInputStream input = null;
    if(users.getId() != 0){
    ActionContext.getContext().getSession().put("users", users);

    if("on".equals(remerber_password)){
    Cookie cookieuser = new Cookie("user",name+"-"+password); 
    cookieuser.setMaxAge(60*60*24*7);
    }

    input = new ByteArrayInputStream("ok".getBytes());

    return input;
    }else{
    input = new ByteArrayInputStream("error".getBytes());

    return input;
    }
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public String getPassword() {
    return password;
    }

    public void setPassword(String password) {
    this.password = password;
    }

    public String getRemerber_password() {
    return remerber_password;
    }

    public void setRemerber_password(String remerberPassword) {
    remerber_password = remerberPassword;
    }
    }Action的XML配置
    <action name="userLoginAction" class="com.softeem.mail.action.UserLoginAction">
    <result type="stream">
    <param name="contentType">text</param>
    <param name="inputName">login</param>
    </result>
    </action>JSP页面代码
    <script type="text/javascript">
     /*提交验证,异步传输*/ 
    $(document).ready(function(){
      $("form").submit(function (){
      if ($("#name").val() == '' || $("#password").val() == ''){ 
      alert("帐号和密码不能为空!");
      return false; 
      }else{             
       $.ajax({                  
       url: 'userLoginAction',       //处理测试页面,注意返回内容,成功返回OK                  
       dataType: 'text',                  
       data: $("form").serialize(),                  
       success: function (msg) {                      
       if (msg == "ok"){ 
       window.location.href = "fail.jsp"; 
       }else{ 
       alert(msg);                       
       alert("帐号或密码不正确,请您重新输入!");                          
       return;                      
       }                  
        }              
        });          
     }        
    return false;                         ////////////////要阻止表单提交     
    });
    });
      

  2.   

    Action返回的是一个InputStream,在XML文件的配置里面要把result类型写stream