解决struts2和jquery AjaxForm上传图片不能返回Json的问题
?情况一:能上传,但是会报如下错误,而且返回不了JSON值:
Js:
$("#filesubmit").click(function () {
????????????????? if ($("#inputfile").val() == "") {
????????????????????? alert("请选择一个图片文件");
???????????????????? return;
???????????????? }
???????????????? alert("ok");
???????????????? $("#fileUpload").ajaxSubmit({
???????????????? ?//url:"fileUpload",?
?????????? ????? ??? ?//type:"post",?
?????????? ????? ??? ?//enctype:'multipart/form-data',?
????????????? ??? ??? ?//iframe: true,?
???????????????? ?dataType:"json",
???????????????????? success: function (data, status) {
???????????????????? //if()
??? ?????????????????????alert(data);
???????????????????????? alert(data.myFilefileName);
???????????????????????? //var imgpath="UploadImages/"+data.myFilefileName;
???????????????????????? var imgpath="../UploadImages/"+data.myFilefileName;
???????????????????????? alert(imgpath);
???????????????????????? $(".jcrop-preview").attr('src', imgpath);
???????????????????????? //$("#target").removeAttr('src');
??????????????????????? $("#target").attr('src', imgpath);
??????????????????????? // alert($("#target").attr('src'));
??????????????????????? $("#srcimg").val( data.myFilefileName);
??????????????????????? $(".jcrop-holder img").attr('src', imgpath);
???????????????????? }
???????????????? });
???????????? });
Action片段:
publicclass ImgUploadAction extends MySuperAction{??
??? privatestaticfinallongserialVersionUID = 572146812454l;?
??? privatestaticfinalintBUFFER_SIZE = 16 * 1024;?
??? public OperateImage imgoper=new OperateImage();
??? private File myFile;
??? private String myFilecontentType;?
??? private String myFilefileName;??
??? private String imageFileName;?
??? private String caption;
Struts.xml:
<package name="ajaxDemo" extends="json-default">
??? <action name="fileUpload" class="action.ImgUploadAction" method="uploadPic">???
?????? <interceptor-ref name="fileUploadStack" />
?????? <interceptor-ref name="fileUpload">
??????????????? <param name="allowedTypes">
??????????????????? image/bmp,image/png,image/gif,image/jpeg,image/jpg
??????????????? </param>
??????????????? <param name="maximumSize">4194304</param>
???????? </interceptor-ref>????
?????? <result type="json"></result>??
??? </action>?
??? <action name="cutPic" class="action.ImgUploadAction" method="cutPic">?
??? <result? type="redirect">/page/altpho.jsp</result>?
??? </action>
??? <action name="zhuce" class="action.UserAction"? method="zhuceCheck">
??? <result type="json"></result>
??? </action>
??? <action name="setRelationship" class="action.RelationAjaxAction">
??? <result type="json"></result>
??? </action>
</package> ??
十二月 05, 2013 10:41:37 下午 org.apache.struts2.dispatcher.Dispatcher warn
WARNING: Could not find action or result
There is no Action mapped for action name undefined. - [unknown location]
??? at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)
??? at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
??? at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
??? at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
??? at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:553)
??? at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
??? at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
??? at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
??? at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
??? at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
??? at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
??? at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
??? at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
??? at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
??? at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
??? at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
??? at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
??? at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
??? at java.lang.Thread.run(Thread.java:722)
?
十二月 05, 2013 10:41:37 下午 org.apache.struts2.dispatcher.Dispatcher error
SEVERE: Exception occurred during processing request: There is no Action mapped for action name undefined.
There is no Action mapped for action name undefined. - [unknown location]
??? at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)
??? at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
??? at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
??? at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
??? at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:553)
??? at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
??? at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
??? at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
??? at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
??? at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
??? at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
??? at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
??? at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
??? at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
??? at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
??? at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
??? at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
??? at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
??? at java.lang.Thread.run(Thread.java:722)
?
情况二:注释掉dataType:”json”,后不报异常,但是返回的看似Json,但不可用
?
还有其他各种状况,多得都忘了,下面是解决方法(花了我好几天的时间,走了不少弯路,最终还是行了啊? >_<? )
Struts.xml:
<struts>?
<constant name="struts.multipart.saveDir" value="/temp"></constant>
<constant name="struts.multipart.maxSize" value="1024102400"/>
<constant name="struts.multipart.maxSize" value="9000000"/>
<package name="example" namespace="" extends="struts-default">
<action name="fileUpload" class="action.ImgUploadAction" method="uploadPic">???
?????? <interceptor-ref name="fileUploadStack" />
?????? <interceptor-ref name="fileUpload">
??????????????? <param name="allowedTypes">
???????????? ???????image/bmp,image/png,image/gif,image/jpeg,image/jpg
??????????????? </param>
??????????????? <param name="maximumSize">4194304</param>
???????? </interceptor-ref>????
?????? <result name="success">/page/test.jsp </result>??
??? </action>?
???
??? <action name="cutPic" class="action.ImgUploadAction" method="cutPic">?
??? <result? type="redirect">/page/altpho.jsp</result>?
??? </action>
</package>
</struts>
Test.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
???? <%
???? //本jsp用于解决struts2和jquery AjaxForm上传图片不能返回Json的问题,
???? //从altpho.jsp异步提交到ImgUploadAction中,再return success/error到此页面中
???? //在此页面获取Action中的值,制作成Json,就能在altpho.jsp中获取
?? ???? //System.out.println("有执行2"+request.getParameter("myFilefileName"));
?? ???? response.setContentType("text/html;charaset=utf-8");
??????? //System.out.println("有执行3");? response.getWriter().write("{\"myFilefileName\":\""+session.getAttribute("imageFileName")+"\"}");
?????? System.out.println(session.getAttribute("imageFileName"));
?????? //System.out.println("有执行4");
?????? //response.sendRedirect("altpho.jsp");
??? %>
Js:
?
Action:
Alert(data);
Alert(data.myFilefileName);
不知你们是怎么解决的,愿意的话贴贴你们解决的方法