十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章主要为大家展示了“jsp+ajax如何实现无刷新上传文件”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“jsp+ajax如何实现无刷新上传文件”这篇文章吧。
创新互联公司专注于企业成都营销网站建设、网站重做改版、枣庄网站定制设计、自适应品牌网站建设、html5、成都做商城网站、集团公司官网建设、外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为枣庄等各大城市提供网站开发制作服务。
具体如下:
列表页:selectaddress.jsp
js页:ajax_edit.js
jsp处理页:editaddress.jsp
上传工具类:UploadUtil.java
思想:由于安全问题,javascript操纵不了文件, 导致ajax不能动态上传文件,所以选择了iframe,
列表页把form表单提交到一个隐式的iframe里面,设置表单的属性
复制代码 代码如下:
enctype='multipart/form-data' target='hidden_frame'
然后,处理页处理后返回
复制代码 代码如下:
out.println('');
列表页执行回调函数!
selectaddress.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="com.wap3.navigater.service.*" %> <%@ page import="com.wap3.navigater.dao.*" %> <%@ page import="com.wap3.navigater.pojo.*" %> <%@ page import="com.wap3.navigater.util.*" %> <%@ page import="java.util.*" %> <%@ page import="java.text.*" %>Insert title here <% String action = ParameterUtil.getStringParameter(request,"action",""); FriendurlDao friendurlDao = new IbatisFriendurlDao(); if("del".equals(action)){ int[] urlids = ParameterUtil.getIntArrayParams(request,"urlid"); for(int urlid : urlids){ friendurlDao.deleteFriendurlByP(urlid); } } String navigaterPage = request.getRequestURL().toString(); if(request.getQueryString()!= null){ navigaterPage += "?"+request.getQueryString(); } int categoryId = ParameterUtil.getIntParameter(request,"categoryId",0); if(categoryId == 0){ response.sendRedirect("selectcategory.jsp"); return; } int pageSize = ParameterUtil.getIntParameter(request,"pageSize",10); int pageNo = ParameterUtil.getIntParameter(request,"pageNo",1); String orderBy = ParameterUtil.getStringParameter(request,"orderBy","sequence"); int ascOrDesc = ParameterUtil.getIntParameter(request,"ascOrDesc",0); CategoryDao categoryDao = new IbatisCategoryDao(); Category category = categoryDao.selectCategoryByP(categoryId); String categoryName = category.getCategoryName(); Friendurl friendurl = new Friendurl(); friendurl.setCategoryId(categoryId); friendurl.addOrderBy(orderBy,ascOrDesc); ListfriendurlList = friendurlDao.selectFriendurlByE(friendurl); //分页 int totallpage = (int) Math.ceil(((double) friendurlList .size() * 1.0D)/ (double) pageSize); pageNo =pageNo <= 0 ? 1 : pageNo; pageNo =pageNo > totallpage ? totallpage : pageNo; DataPageUtil datePage = new DataPageUtil(friendurlList ,friendurlList .size(),pageSize, pageNo); boolean hasPrerPage = datePage.hasPrevPage(); boolean hasNextPage= datePage.hasNextPage(); int curpageNo = datePage.getPageNo(); int pageCount = datePage.getPageCount(); friendurlList = DataPageUtil.subList(friendurlList, pageSize, pageNo); if(friendurlList == null && friendurlList.size()<=0) { out.println("没有您需要的数据"); }else{ %> <%@include file="inc/header.jsp" %> <% } %><%@ include file="inc/pagination.jsp"%>
条目列表 <% for(Friendurl friendurlTemp:friendurlList) { int urlId = friendurlTemp.getUrlid(); String siteName = friendurlTemp.getSitename(); String alias = friendurlTemp.getAlias(); String address = friendurlTemp.getAddress(); String texturl = friendurlTemp.getTexturl(); String imageurl = friendurlTemp.getImageurl(); String description = friendurlTemp.getDescription(); Date validbegintime = friendurlTemp.getValidbegintime(); Date validendtime = friendurlTemp.getValidendtime(); String mobile = friendurlTemp.getMobile(); int sequence = friendurlTemp.getSequence(); int filter = friendurlTemp.getFilter(); %> 站点名称 站点别名 归类 公司地址 文字链接 图片链接 描述 有效开始时间 有效结束时间 电话 位置排序 推荐序号 <% } %> /> <%=siteName %> <%=alias %> <%=categoryName%> <%=address %> <%=texturl%> ' class='logo' title='<%=imageurl%>'/> <%=description%> <%=validbegintime == null || "".equals(validbegintime)? "--:--" : TimeUtil.date2Str(validbegintime)%> <%=validendtime == null || "".equals(validendtime)? "--:--" : TimeUtil.date2Str(validendtime)%> <%=mobile%> <%=sequence%> <%=filter%> 提示:双击选框修改属性值
editaddress.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="com.wap3.navigater.util.*" %> <%@ page import="com.wap3.navigater.service.*" %> <%@ page import="com.wap3.navigater.pojo.*" %> <%@ page import="com.wap3.navigater.dao.*" %> <%@ page import="java.util.*" %> <%@ page import="java.text.*" %> <% String target = ParameterUtil.getStringParameter(request,"target",""); String action = ParameterUtil.getStringParameter(request,"action",""); if(!"".equals(target) && target != null && "edit".equals(action)){ int categoryId = 0; CategoryDao categoryDao = null; int urlid=0,status=0,sequence=0,filter=0; String sitename=null,alias=null,texturl=null,imageurl=null,description=null,mobile=null; Date validbegintime=null,validendtime=null; if("imageurl".equals(target)){ System.out.println("图片上传"); UploadUtil.upload(request, response); String imageurlpath = (String) request.getAttribute("imageurlpath"); urlid = Integer.parseInt((String)request.getAttribute("urlid")); imageurl = (String)request.getAttribute("imageurlpath"); }else{ if("categoryName".equals(target)){ String categoryName = ParameterUtil.getStringParameter(request,"categoryName",""); Category category = new Category(); category.setCategoryName(categoryName); categoryDao = new IbatisCategoryDao(); Category category2 = categoryDao.selectCategoryByE(category).get(0); categoryId = category2.getCategoryId(); }else{ categoryId = ParameterUtil.getIntParameter(request,"categoryId",0); } urlid = ParameterUtil.getIntParameter(request,"urlid",0); sitename = ParameterUtil.getStringParameter(request,"sitename",""); alias = ParameterUtil.getStringParameter(request,"alias",""); texturl = ParameterUtil.getStringParameter(request,"texturl",""); imageurl = ParameterUtil.getStringParameter(request,"imageurl",""); description = ParameterUtil.getStringParameter(request,"description",""); status = ParameterUtil.getIntParameter(request,"status",0); sequence = ParameterUtil.getIntParameter(request,"sequence",0); filter = ParameterUtil.getIntParameter(request,"filter",0); mobile = ParameterUtil.getStringParameter(request,"mobile",""); validbegintime = ParameterUtil.getDateParameter(request,"validbegintime"); validendtime = ParameterUtil.getDateParameter(request,"validendtime"); } Calendar beginDay=Calendar.getInstance(); Friendurl friendurl = new Friendurl(); friendurl.setUrlid(urlid); friendurl.setSitename(sitename); friendurl.setDescription(description); friendurl.setTexturl(texturl); friendurl.setImageurl(imageurl); friendurl.setAlias(alias); friendurl.setSequence(sequence); friendurl.setMobile(mobile); friendurl.setCategoryId(categoryId); friendurl.setStatus(status); friendurl.setValidbegintime(validbegintime); friendurl.setValidendtime(validendtime); friendurl.setFilter(filter); FriendurlDao friendurlDao = new IbatisFriendurlDao(); friendurlDao.updateFriendurlByP(friendurl); Friendurl friendurl2 = friendurlDao.selectFriendurlByE(friendurl).get(0); if(friendurl2 != null){ String methodName = "get" + target.substring(0,1).toUpperCase()+target.substring(1); if(target.endsWith("time")){ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); out.println(TimeUtil.date2Str(((Date)(friendurl2.getClass().getMethod(methodName,new Class[]{}).invoke(friendurl2,new Object[]{}))))); }else if("categoryName".equals(target)){ out.println(categoryDao.selectCategoryByP(friendurl2.getCategoryId()).getCategoryName()); }else if("imageurl".equals(target)){ // 无刷新关键的步骤,让iframe的父辈body执行callback这个函数 out.println(""); }else{ out.println(friendurl2.getClass().getMethod(methodName,new Class[]{}).invoke(friendurl2,new Object[]{})); } }else{ out.println("更新失败"); } } %>
ajax_edit.js
(function(){ $.fn.extend({ ajax_edit:function(str,oldValue){ return this.each(function(){ $this = $(this); var id; var nameId = $this.attr("class").split(" ")[1]; var idName = $this.parents("tr.package_list").find("td:first-child :input[type=checkbox]").attr("class").split(" ")[1]; id = $this.siblings("."+idName+"_td").find("input").val(); if(nameId == "categoryName" && str == "editaddress.jsp"){ $this.load("categorylist.jsp",{'oldValue':oldValue,'urlid':id}); }else{ $this.html(""); $this.find(":input:last").focus(); } }) }, ajax_edit_img:function(str,oldValue){ //处理图片上传AJAX 关键的一步form表格里的 target='hidden_frame'提交到ID为hidden_frame的iframe里面 return this.each(function(){ var $this = $(this); var nameId = $this.attr("class").split(" ")[1]; var idName = $this.parents("tr.package_list").find("td:first-child :input[type=checkbox]").attr("class").split(" ")[1]; var id = $this.siblings("."+idName+"_td").find("input").val(); var $imgForm = $(""); $this.html($imgForm); $this.find(":input:last").focus(); $imgForm.find(":input[type=file]").bind("change",function(){ $(this).fileTypeJudge("photo"); }) $imgForm.find(":input.ok").bind("click",function(){ var $button = $(this); if($imgForm.find(":input[type=file]").val()==''){ alert("请上传图片!"); return false; } $button.attr("disabled",true); $("#selectaddressForm_img").submit(); }) $imgForm.find(":input.cancel").bind("click",function(){ $(this).parents(".edit_img").html(oldValue); }); }) }, ajax_handle:function(str,oldValue){ return this.each(function(){ var $this = $(this); setTimeout(function(){ if(!$("div").hasClass("datepick-control") || !$("div .datepick-control").is(":visible")){ if(oldValue != $this.val() && $this.val() !="" ){ $.post( str,$("#selectaddressForm").serialize(), function(data){ $this.parents(".edit").text(data.trim()).css("color","red"); } ) }else{ $this.parents(".edit").text(oldValue); } } },1000); }) } }) })(jQuery)
以上是“jsp+ajax如何实现无刷新上传文件”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!