struts2 action 运行完不跳转

本文介绍如何通过jQuery.ajax方法实现文件导入功能,避免页面跳转,通过在URL中加入随机数解决服务器端的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我的目的是点"导入"按钮,执行名为"test.do"的action,但就算action里return null;他还是会跳转,其实要执行action只要把action的地址传给服务器就OK了。

用jQuery的jQuery.ajax 方法,能完全不跳转,

不加Math.random()没反应,在action拿filePath的地方再去掉那个随即数就可以了,

没参数也加上随机   $.ajax({url:"temp.do?rnd="+Math.random()});

jsp

<script>

function do_import() {
var rnd=Math.random().toString().substring(3,13);
 var filePath = document.getElementById("filePath").value;
 if (filePath == "") {
  alert("请选择文件");
 } else {
  $.ajax({url:"test.do?filePath="+rnd+filePath});
 }}

</script>

<input type="file" name="filePath" id="filePath" />

<input type="button" value="导入" onclick="do_import()" />

 

action

 @Override
 public String doAction(HttpServletRequest request,
   HttpServletResponse response) throws Exception {

   importRecInfoLogic.saveAttRecInfo(filePath.substring(10));
  return null;

 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值