登录界面及其jq的编写:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'MyJsp.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="jquery/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".asnyButton").click(function(){
$.ajax({
type:"POST",
url:"ajax.action",
data:"name=Join&location=Button",
async:true,//使用异步交互数据
success:function(msg){//成功后返回的界面
alert(msg);
},
error:function(XMLHttpRequest,textStatus,errorThrown){
alert(errorThrown);//若失败则打印相印的信息
}
});
});
});
</script>
<style type="text/css">
button {
width: 300px;
height: 200px;
position: absolute;
left:35%;/*FF IE7*/
top:50%;/*FF IE7*/
}
</style>
</head>
<body>
<button id="button" class="asnyButton">获取同步</button>
</body>
</html>
对应action的编写
package struct2;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import com.opensymphony.xwork2.Action;
public class Login implements Action {
private String name;
private String location;
private InputStream inputStream;//返回给ajax的输入流
public InputStream getResult() {
return inputStream;
}
public void setResult(InputStream inputStream) {
this.inputStream = inputStream;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
@Override
public String execute() throws Exception {
inputStream = new ByteArrayInputStream("Hello world".getBytes("UTF-8"));
return SUCCESS;
}
}
action的配置:
<action name="ajax" class="struct2.Login">
<result name="success" type="stream">
<param name="contentType">text/html</param>//类型
<param name="inputName">result</param>//参数
</result>
</action>