struct2 ajax

来源:互联网 发布:数据库设置主键的语句 编辑:程序博客网 时间:2024/06/05 17:08

可能我的code 不是很好,只是作为一个参考,我也是初学者


之前用ajax 都有配合用struct   应为strcut  拦截功能   和返回物理视图,

但是,ajax 读取一个放回数据时候,他便直接放回  所以我的代码  值返回out  的数据  再返回SUCCESS  因为根本访问不到  所以  都无所谓



package org.com.ssh.email_tool;

import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletResponseAware;
import org.com.ssh.hibernate.service.DabaseOperate;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.opensymphony.xwork2.Action;

public class FindEmail implements Action,ServletResponseAware{
    
    
    private String email;
    private HttpServletResponse response;
    
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
            
            setEmail("960603374@qq.com");
            AbstractApplicationContext app =new ClassPathXmlApplicationContext("beans.xml");
            DabaseOperate da=app.getBean("dabaseoperate",DabaseOperate.class);
            app.registerShutdownHook();
            PrintWriter out =response.getWriter();
            String info=da.FindEmail(this.email)+"";
            out.println(info);
            System.out.println(info);
            out.flush();
            out.close();
            return SUCCESS;
            
        
        }




@Override
public void setServletResponse(HttpServletResponse arg0) {
    // TODO Auto-generated method stub
    this.response=arg0;
    
    
    
}

}

------



<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>

<script type="text/javascript">
function showCustomer(str)
{
    
    alert("1")
  var xmlhttp;    
    
  if (str=="")
  {
    document.getElementById("txtHint").innerHTML="";
    return;
  }
  if (window.XMLHttpRequest)
  {
    // IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
    xmlhttp=new XMLHttpRequest();
  }
  else
  {
    // IE6, IE5 浏览器执行代码
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 
  xmlhttp.onreadystatechange=
      function()
      {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            
            <!--  document.getElementById("txtHint").innerHTML=xmlhttp.responseText;-->
            alert(xmlhttp.responseText)
        
        }
      }
  xmlhttp.open("GET","/SSH/findemail?email="+str,true);
  xmlhttp.send();
}


</script>

<body>


<input type="text" id="txtHint" value="960603374@qq.com">
<input type="button" onclick='showCustomer("ok")'>




</body>
</html>

原创粉丝点击