waysun一路阳光

不轻易服输,不轻言放弃.--心是梦的舞台,心有多大,舞台有多大。踏踏实实做事,认认真真做人。

  BlogJava :: 首页 :: 新随笔 :: 联系 ::  :: 管理 ::
  167 随笔 :: 1 文章 :: 64 评论 :: 0 Trackbacks
1.请求页面的JS,客户端实现
<script type="text/javascript">
 var flagvalue=0;
 var rest="";
 var resta="";
 //实现AJAX验证
 var http_request = false;
 var infoForm_flag=false;
 var lotId="";
function Save()
{
   
      var infoForm=document.projectSetForm;
      infoForm_flag=infoForm;
      lotId=infoForm.lotId.value;
      //表名称
      var dc = infoForm.tablename.value;
      if(dc==""||dc==null){
       alert("表名称不能为空!");
       infoForm.tablename.focus();
       infoForm.tablename.select();
       return false;
 }
function send_request(url)
  {//初始化、指定处理函数、发送请求的函数
    http_request = false;
    //开始初始化XMLHttpRequest对象
    if(window.XMLHttpRequest)
    { //Mozilla 浏览器
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType)
      {//设置MiME类别
           http_request.overrideMimeType('text/xml');
      }
    }
    else if (window.ActiveXObject)
    { // IE浏览器
      try
      {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
      }
       catch (e)
      {
        try
        {
             http_request = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e)
        {
        }
      }
    }
    if (!http_request) { // 异常,创建对象实例失败
     window.alert("不能创建XMLHttpRequest对象实例.");
     return false;
    }
    http_request.onreadystatechange = processRequest;
    // 确定发送请求的方式和URL以及是否同步执行下段代码
    http_request.open("GET", url, true);
    http_request.send(null);
  }
     //处理返回信息的函数
     function processRequest()
     {
         if (http_request.readyState == 4)
         {
             //判断对象状态
             if (http_request.status == 200)
             {
                 //信息已经成功返回,开始处理信息
                 var str=http_request.responseText;//获得从服务器返回的文本信息
                 if(str=="no")
                 {
                     alert("您选择了模型分属不同的模板,请选择同一模板对应是模型!");
                     //return false;
                     infoForm_flag.action="projectSet.do?do=add&lotId="+lotId;
                     infoForm_flag.submit();
                 }
                 else
                 {
                  infoForm_flag.action="projectSet.do?do=creattable&save=-1";
               infoForm_flag.submit();
              }
             } else
             {
                 //页面不正常
                 alert("您所请求的页面有异常。");
             }
         }
     }
  function userCheck()
  {
    send_request('projectSet.do?do=check&rest='+rest);
  }
</script>
2.ProjectSetAction.java

package com.gpdi.softevaluate.action;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ProjectSetAction extends DispatchAction
{
         

/**
  * <p>模板验证</p>
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return
  * @throws Exception
  */
 public ActionForward check(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception
 {
   String rest="";
   String lotId="";
   String retn="";
   String flag="";
   CommDaoFactory mgObj = new CommDaoFactory();
   try
   {
       rest=request.getParameter("rest");
       lotId=request.getParameter("lotId");
   }
   catch(Exception e)
   {
   
   }
if(!rest.equals("0")||!rest.equals("")||rest!=null)
 {
     flag="no";
}
else
{
      flag="ok";

}
       PrintWriter out=response.getWriter();
//服务器返回信息
       out.write(flag);
       out.close();
     //读取本Action所有的请求参数,将path重新构造,加上请求参数

     //ActionForward forward= new ActionForward("project/projectSet.do?do=add&lotId=6");
     //forward.setRedirect(true);
      //传参数后返回
     //return forward;    
    return mapping.findForward(null);
 
 }
public ActionForward creattable(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception
{
     //实现功能代码
return mapping.findForward("create");
}

}
3.struts.config.xml
 <action attribute="projectSetForm" name="projectSetForm"
    parameter="do" path="/project/projectSet" scope="request"
       type="com.gpdi.softevaluate.action.ProjectSetAction" validate="false">
   <forward name="list" path="/project/viewProject.jsp" />
   <forward name="add" path="/project/addProject.jsp" />
   <forward name="create" path="/project/createTable.jsp" />
  </action>

posted on 2009-02-22 19:34 weesun一米阳光 阅读(577) 评论(1)  编辑  收藏 所属分类: StrutsAJAXcnweblog/nm1504

评论

# re: AJAX和Struts的action交互使用[原创总结] 2009-10-27 16:57 s
f  回复  更多评论
  


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


网站导航: