使用struts的Ajax支持

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 InputStream inputStream;

   private String nameId;

 

   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


<?xml version="1.0"encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC

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

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

<struts>

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

   <package name="ztx" extends="struts-default">

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

        <result type="stream">

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

           <param name="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 + "/";

%>

 

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

<html>

<head>

<base href="<%=basePath%>">

 

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

<meta http-equiv="pragma"content="no-cache">

<meta http-equiv="cache-control"content="no-cache">

<meta http-equiv="expires"content="0">

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

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

<!--

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

   -->

<script type="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>

   <form action="" method="post">

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

      <div id="info"></div>

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

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

   </form>

</body>

</html>

 



  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值