少年阿宾

那些青春的岁月

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

//ReceiveAction.java
package com.abin.ajax.action;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.abin.ajax.bean.UserBean;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class ReceiveAction extends ActionSupport implements ModelDriven{
 private String result;
 private UserBean user;
 public Object getModel(){
  if(null==user){
   user=new UserBean();
  }
  return user;
 }
 public String receive(){
  HttpServletResponse response=ServletActionContext.getResponse();
  String username=user.getUsername();
  String password=user.getPassword();
  String company=user.getCompany();
  String work=user.getWork();
  String address=user.getAddress();
  
  try {
   if(username!=null){
    System.out.println("username="+username);
    result="1";
    response.getWriter().write("1");
    response.getWriter().flush();
    response.getWriter().close();
    return "json";
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return null;
 }
 
 
 public UserBean getUser() {
  return user;
 }
 public void setUser(UserBean user) {
  this.user = user;
 }
 public String getResult() {
  return result;
 }
 public void setResult(String result) {
  this.result = result;
 }
 
}



//struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
   
<struts>
 <package name="userAjax" extends="json-default" namespace="/" >
  <action name="receive" method="receive" class="com.abin.ajax.action.ReceiveAction">
   <result name="json" type="json"></result>
  </action>
 </package>


</struts>






//web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>website</display-name>
 
  <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>
    </filter>
 
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
 
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>





//index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
 pageEncoding="utf-8"%>
<!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=utf-8">
<title>清风少年</title>
<link href="css/page/html.css" type="text/css" rel="stylesheet"></link>
</head>
<style type="text/css">
#nav li {
 display: inline;
 list-style-type: none;
 padding: 5px;
}
</style>
<script type="text/javascript" src="js/first/public.js"></script>


<body>
 <ul id="nav">
  <li><a href="http://www.itpuji.net.cn">首页</a></li>
  <li><a href="#">博客</a></li>
  <li><a href="#">相册</a></li>
  <li><a href="#">圈子</a></li>
  <li><a href="#">设置</a></li>
 </ul>
 <br/>
 <span id="txt" ></span>
 <br/>
 <div id="practise">
  用户名:<br/>
  <input type="text" name="username" id="username"/><br/>
  密码:<br/>
  <input type="text" name="password" id="password"/><br/>
  <hr/>
  公司:&nbsp;
  <select id="company" name="company">
   <option value="abin">abin</option>
   <option value="bing">bing</option>
  </select>
  <br/>
  <hr/>
  职业:<br/>
  农民:
  <input type="checkbox" name="work" id="work" checked  value="1"/>
  男人:
  <input type="checkbox" name="work" id="work"/><br/>
  诗人:
  <input type="checkbox" name="work" id="work"/>
  歌手:
  <input type="checkbox" name="work" id="work"/><br/>
  <hr/>
  家庭住址:<br/>
  中国:
  <input type="radio" name="address" id="address" checked  value="1"/>
  凉城:
  <input type="radio" name="address" id="address"/><br/>
  内蒙古:
  <input type="radio" name="address" id="address"/><br/>
  
  <input type="button"  value="提交" onclick="SendAndReturn()"/>
 </div>


</body>
</html>

 





//public.js
//创建Ajax请求
function CreateXmlRequest(){
  var xmlHttp=false;
  if(window.XMLHttpRequest){
   xmlHttp=new XMLHttpRequest();
  }else if(window.ActiveXObject){
   try{
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
   }catch(e1){
    try {
     xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    } catch (failed) {
     xmlHttp=false;
    }
    
   }
  }
  return xmlHttp;
 }
 
function SendAndReturn(){
 var username=document.getElementById("username").value;
 var password=document.getElementById("password").value;
 var company=document.getElementById("company").value;
 var work=document.getElementById("work").value;
 var address=document.getElementById("address").value;
 
 var request=CreateXmlRequest();
 if(!request){
  document.getElementById("txt").innerHTML="当前浏览器不支持";
 }else{
  document.getElementById("txt").innerHTML="创建xmlHttp成功";
  request.open("post","receive?username="+username+"&password="+password+"&company="+company+"&work="+work+"&address="+address,true);
  request.send();
  request.onreadystatechange=function(){
   if(request.readyState==4){
    if(request.status==200){
     var data=request.responseText;
     
     alert("data="+data);
     if(data=="1"){
      alert("username!=null");
     }
     document.getElementById("txt").innerHTML=request.responseText;
    }
   }
  }
 }
 
 
 
 
}
posted on 2011-12-22 00:14 abin 阅读(906) 评论(0)  编辑  收藏

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


网站导航: