3个回答
展开全部
例子:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script src="jquery.js"></script>
<script>
function sub(){
var value1 = $("#id1").val();
var value2 = $("#id2").val();
if(value1!="" && value2!=""){
$.ajax({
url:'a.action',
type:'post',
data:'value1='+value1+'&value2='+value2
//data:{value1:value1,value2:value2},
dataType:'text',
success:function(text){
alert(text);
}
})
}
}
</script>
</head>
<body>
<form id="form1" name="form1">
<input type="text" id="id1"/><br />
<input type="text" id="id2"/><br />
<input type="button" value="提交" onclick="sub();"/>
</form>
</body>
</html>
struts.xml:
<action name="a" class="com.test.a.action" method="test">
</action>
a.action:
private String value1;
private String value2;
private String text;
get()...set()...
public void test()throws Exception{
//do somethings
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/json;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
try{
PrintWriter pw = response.getWriter();
boolean flag = XXXService(value1,value2);
String text = "";
if(flag)
text = "执行成功!";
else
text = "执行失败!";
pw.write(text);
pw.flush();
}catch(Exception e){
e.printStackTrace();
throw new Exception("执行异常!");
}
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script src="jquery.js"></script>
<script>
function sub(){
var value1 = $("#id1").val();
var value2 = $("#id2").val();
if(value1!="" && value2!=""){
$.ajax({
url:'a.action',
type:'post',
data:'value1='+value1+'&value2='+value2
//data:{value1:value1,value2:value2},
dataType:'text',
success:function(text){
alert(text);
}
})
}
}
</script>
</head>
<body>
<form id="form1" name="form1">
<input type="text" id="id1"/><br />
<input type="text" id="id2"/><br />
<input type="button" value="提交" onclick="sub();"/>
</form>
</body>
</html>
struts.xml:
<action name="a" class="com.test.a.action" method="test">
</action>
a.action:
private String value1;
private String value2;
private String text;
get()...set()...
public void test()throws Exception{
//do somethings
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/json;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
try{
PrintWriter pw = response.getWriter();
boolean flag = XXXService(value1,value2);
String text = "";
if(flag)
text = "执行成功!";
else
text = "执行失败!";
pw.write(text);
pw.flush();
}catch(Exception e){
e.printStackTrace();
throw new Exception("执行异常!");
}
}
展开全部
jQuery的ajax请求很简单。一个方法就搞定了,
$.get(url,{"propertyName":propertValue},function(data){});
Struts2 + JQuery实现ajax方式提交 ,几分钟就能搞定
$.get(url,{"propertyName":propertValue},function(data){});
Struts2 + JQuery实现ajax方式提交 ,几分钟就能搞定
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
需要的话我发一份给你。留下地址。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询