这个例子是简单的jQuery提交的例子
首先是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>
<scripttype="text/javascript"src="js/jquery-1.4.4.min.js"></script>
<metahttp-equiv="Content-Type"content="text/html; charset=utf-8">
<title>JQuery测试页面</title>
<scripttype="text/javascript">
function verify(){
var jqueryObj = $("#username");
//获取节点的值
var userName = jqueryObj.val();
alert(userName);
$.get("TestAction!test.action?name=" + userName,null,callback);
}
function callback(data){
//alert("服务器返回值:"+data);
var resultObj = $("#result");
resultObj.html(data);
}
function postsubmit(){
$.post("TestAction!test.action?name=" + $("#username").val(),null,callback);
}
function clean(){
var resultObj = $("#result");
resultObj.html("");
}
</script>
</head>
<body>
<h1>JQuery练习</h1>
<inputid="username"name="username"value="请输入..."></input>
<inputtype="button"onclick="verify()"value="确定"/>
<inputtype="button"onclick="postsubmit()"value="post提交"/>
<inputtype="button"onclick="clean()"value="清除"/>
<divid="result"/>
</body>
</html>
接着是action的代码
package com.xiaoqiang;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport {
private String name;
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return "test";
}
public void test() throws Exception {
// TODO Auto-generated method stub
System.out.println("name:"+new String(name.getBytes("iso-8859-1"),"utf-8"));
HttpServletResponse httpServletResponse = ServletActionContext.getResponse();
httpServletResponse.setContentType("text/html;charset=utf-8");
PrintWriter pw = httpServletResponse.getWriter();
pw.print("可以注册");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}