请教用jquery的ajax方法和struts2整合进行一对多查询的源码例子
1个回答
展开全部
<!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("执行异常!"); }}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询