使用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>
<%@ 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>
- 使用struts的Ajax支持
- Struts 对 Ajax的支持
- struts 2的Ajax支持
- 在Struts中使用JSON Ajax支持
- 在Struts 2中怎样使用使用JSON Ajax支持
- 在struts 2中使用json ajax支持
- 在struts 2中使用json ajax支持
- 在struts 2中使用json ajax支持
- 在Struts 2中使用JSON Ajax支持
- 在Struts 2中使用JSon ajax支持
- 在Struts 2中使用JSON Ajax支持
- 在Struts 2中使用JSON Ajax支持
- 在Struts 2中使用JSON Ajax支持
- 在Struts 2中使用JSON Ajax支持
- 在Struts 2中使用JSON Ajax支持
- 在Struts 2中使用JSON Ajax支持
- Jsp在Struts 2中使用JSON Ajax支持
- Jsp在Struts 2中使用JSON Ajax支持
- 整数因子分解问题
- Calendar(示例,出错代码)
- [转]python socket编程
- java中volatile关键字的含义
- 【Java并发编程】之十四:图文讲述同步的另一个重要功能:内存可见性
- 使用struts的Ajax支持
- 想要一个开源的深度学习构架吗? 挑选你的
- Linux上相对路径问题
- vmvare上hadoop2.6的伪分布环境搭建
- Cocos2D:塔防游戏制作之旅(十六)
- O06 - proc
- yii2 save update 数组赋值操作
- 数据结构、算法与应用 (C++描述) 第二版 1.23
- NSURLSession简介