使用struts的Ajax支持

来源:互联网 发布:抓取ajax动态网页java 编辑:程序博客网 时间:2024/06/05 16:39

ActionTest.java

package com.ztx.action;

 

import java.io.ByteArrayInputStream;

import java.io.InputStream;

import java.io.UnsupportedEncodingException;

 

import com.opensymphony.xwork2.ActionSupport;

 

public classActionTest extends ActionSupport {

 

   private InputStreaminputStream;

   private StringnameId;

 

   public String getNameId() {

      return nameId;

   }

 

   public void setNameId(StringnameId) {

      try {

        //%df%a4转换为字符串,即使用指定的编码机制对 application/x-www-form-urlencoded 字符串解码

        this.nameId = java.net.URLDecoder.decode(nameId,"UTF-8");

      }catch(UnsupportedEncodingException e) {

        e.printStackTrace();

      }

   }

 

   public InputStreamgetInputStream() {

      return inputStream;

   }

 

   public String execute()throws Exception {

      if (nameId.equals("zhangsan")){

        inputStream = new ByteArrayInputStream("用户名正确".getBytes("utf-8"));

      }else{

        inputStream = new ByteArrayInputStream("用户名不存在".getBytes("utf-8"));

      }

      return SUCCESS;

   }

}

 


struts.xml


<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEstruts PUBLIC

    "-//Apache Software Foundation//DTD StrutsConfiguration 2.3//EN"

    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

   <constantname="struts.configuration.xml.reload"value="true"/>

   <packagename="ztx"extends="struts-default">

      <actionname="testAjax"class="com.ztx.action.ActionTest">

        <resulttype="stream">

           <paramname="contentType">application/octet-stream</param>

           <paramname="inputName">inputStream</param>

        </result>

      </action>

   </package>

</struts>



login.jsp


<%@ page language="java"import="java.util.*"pageEncoding="utf-8"%>

<%

   Stringpath = request.getContextPath();

   StringbasePath = request.getScheme() + "://"

        +request.getServerName() + ":" + request.getServerPort()

        +path + "/";

%>

 

<!DOCTYPEHTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">

<html>

<head>

<basehref="<%=basePath%>">

 

<title>My JSP'index.jsp' starting page</title>

<metahttp-equiv="pragma"content="no-cache">

<metahttp-equiv="cache-control"content="no-cache">

<metahttp-equiv="expires"content="0">

<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

<metahttp-equiv="description"content="This is my page">

<!--

   <linkrel="stylesheet" type="text/css"href="styles.css">

   -->

<scripttype="text/javascript">

   var xmlHttp;

   function validate() {

      var idField = document.getElementById("userName");



//    varxhrUrl = "DoAjaxServlet.do?nameId="+escape(idField.value);

//这样写的后果就是:

//如果传递中文,就会抛出异常

//警告: Parameters: Character decoding failed. Parameter skipped.
//java.io.CharConversionException: isHexDigit.



      var xhrUrl ="testAjax.action?nameId=" + idField.value;

      xhrUrl = encodeURI(xhrUrl); //写一个不行。如果写一个就是????号。

      xhrUrl = encodeURI(xhrUrl); //写2个,则输出%df%a4这中.application/x-www-form-urlencoded字符串

      if (window.XMLHttpRequest) {

        xmlHttp = new XMLHttpRequest();

      } else {

        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

      }

      xmlHttp.open("get", xhrUrl,true);

      xmlHttp.onreadystatechange =callback;

      xmlHttp.send();

   }

  

   function callback() {

      if (xmlHttp.readyState == 4 &&xmlHttp.status == 200) {

        document.getElementById("info").innerHTML =xmlHttp.responseText;

      }

   }

</script>

</head>

 

<body>

   <formaction=""method="post">

      <inputtype="text"id="userName"name="userName"onblur="validate()"/>

      <divid="info"></div>

      <br/> <inputtype="text"id="password"name="password"/><br/> <input

        type="submit"value="提交">

   </form>

</body>

</html>

 



2 0
原创粉丝点击