今天遇到这样一个问题我需要获得一个action执行方法后的一个返回值。
先讲下大概思路,其实很简单,就是用Jquery的$.post来执行ajax
jQuery.post(url,[data],[callback],[type])
url:执行的action的url
[data]:传递过去的参数,假如在表单中,可以使用$("form").serialize()把form中的参数全部提交
[callback]:回调函数,这个用来获取返回值,并把返回值当做参数。例如function(responseText){}
type:可以规定返回的类型,可以返回html,text,xml,json等
我讲一下具体的解决办法,
Action:
public ActionForward doGetEditConsumeCount(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String orgIdStr = request.getParameter("orgId");
int orgId = Integer.parseInt(orgIdStr);
MasOrgInfo organ = organService.getOrganById(orgId);
String consumeType = request.getParameter("consumeType"); //消费类型
String consumeDateStart = request.getParameter("consumeDateStart"); //消费日期起始
String consumeDateEnd = request.getParameter("consumeDateEnd"); //消费日期截止
String prStart = request.getParameter("priceStart"); //实际价格原始价格
Float priceStart = null;
if (!prStart.equals("")) {
priceStart = Float.parseFloat(prStart);
}
String prEnd = request.getParameter("priceEnd"); //实际价格新价格
Float priceEnd = null;
if (!prEnd.equals("")) {
priceEnd = Float.parseFloat(prEnd);
}
String type = request.getParameter("type"); //专业类型
String packageName = request.getParameter("packageName"); //专业或包
String combo = request.getParameter("combo"); //套餐
Integer comsumeNum=organService.getUpdateConsumeNum(orgId, type, packageName, combo, consumeType, consumeDateStart, consumeDateEnd, priceStart);
Long peopleNum=organService.getUpdateConsumePeopleNum(orgId, type, packageName, combo, consumeType, consumeDateStart, consumeDateEnd, priceStart);
String strNum="\n 即将修改"+comsumeNum+"条记录,共计"+peopleNum+"个账号";
response.setContentType("text/xml;charset=utf-8"); //更改字符编码
response.getWriter().println(strNum); //这句话是把结果返回(这个是关键)
return mapping.findForward(""); //这里返回空就好
}
jsp页面的js代码如下:
function update() {
if($('#type').val()!=null&&$('#type').val){
$.post("${basepath}/organ.do?o=doGetEditConsumeCount&orgId=${organ.id}&pageNum=1", $("form").serialize(),
//这里的resonseText就是action中的strNum了,这个名字可以随便起的
function(responseText){
var msg = responseText+"\n您真的确定要批量修改吗?\n\n请确认!";
if (confirm(msg)==true){
theform.action = "${basepath}/organ.do?o=doEditLotConsumes&orgId=${organ.id}";
theform.submit();
return true;
}else{
return false;
}
}
, "text");
}else{
alert("提示:请将原始价格填入“实际单价起始价格框”,新价格填入“实际单价截止价格框”");
}
}
上面的代码我其实是在回调函数里面又写了一些东西,其实可以简化为这个样子
$.post("${basepath}/organ.do?o=doGetEditConsumeCount&orgId=${organ.id}&pageNum=1", $("form").serialize(),function(responseText){alert(responseText)},"text");
分享到:
相关推荐
主要介绍了JavaScript获取网页表单action属性的方法,涉及javascript操作表单属性的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了JavaScript获取表单内所有元素值的方法。分享给大家供大家参考。具体如下: 下面的JS代码可以遍历指定表单中的所有元素,并输出元素的值 <!DOCTYPE html> <html> <body> <form id=...
原理就是:一般使用遍历的方法,判断每个Radio是否被选中,如果是,再取其值. <form id=userlist method=post action=option.php> <input type=radio name=userid value=1>...function usubmit(action){ v
在Asp.net Core之前所有的Action返回值都是ActionResult,Json(),File()等方法返回的都是ActionResult的子类。并且Core把MVC跟WebApi合并之后Action的返回值体系也有了很大的变化。 ActionResult类 ...
JS当中不能接收ModelAndView的返回值吗?一定要在JSP页面中才能接收吗? 1 方法一 【有效】 可以的,跟el表达式访问方式一样。 示例代码,一个数据展示请求的Action中存入一个userId: @RequestMapping(value="/...
mvc通过反射获取action方法,适用于权限控制。
Struts-JSON-Jquery完成根据条件返回对象Demo,仅用于教学,帮助那些刚入门的朋友,导入工程即可看效果,高手绕行,谢谢!... 2),如果想查看没有返回值时的效果,在action中屏蔽user=getUserInfo()即可
Node.js in Action, Second Edition 第一章
D3.js in Action Second Edition D3.js实战 2018年最新版 原书第二版 pdf格式 386页
D3.js In Action D3实战 英文版 D3.js In Action D3实战 英文版 D3.js In Action D3实战 英文版 D3JS参考 学习D3JS
Ember.js in Action
Node.js in action第二版(英文版) Node.js is anasynchronous, event-driven JavasScript runtime that offers a powerful but consise standard library.
javascript in action : javascript 实战源码。比较经典。
D3.js in Action Data visualization with JavaScript(2nd) 英文无水印原版pdf 第2版 pdf所有页面使用FoxitReader、PDF-XChangeViewer、SumatraPDF和Firefox测试都可以打开 本资源转载自网络,如有侵权,请联系...
博文链接:https://clarancepeng.iteye.com/blog/112040
用js函数让网页重定向,window.location.href=‘actionName.action?paraName=arg0’; 如果需要带参数的话可以在action中定义那个参数名paraName,并生成setter和getter方法 方法二: <script type= "text/...
Node.js in Action, 2nd Edition 教程。 Title: Node.js in Action, 2nd Edition, Publication Date: 2017-09-17, pdf Node.js Nodejs
Vue.js in Action Before you dive into learning how to make Vue.js applications, let’s talk about a few things you should know first. In this book we’ll look at everything you need to know to become...
Angular JS in Action 英文原版