springmvc velocity 文件上傳 json返回 主要代碼

摘要:主要實現步驟:1.jsp發送請求使用jquery.form.js  2.controller下載並返回json數據 3.配置spingmvc.xml  四、頁面腳本接受並處理數據html

注意:這只是主要代碼jquery

springmvc.xmlweb

<context:component-scan base-package="。。。。" />
	
	<mvc:annotation-driven />
	
	<mvc:default-servlet-handler/>
<!--     <mvc:resources location="/upload/" mapping="/upload/**"/> -->
<!--     <mvc:resources location="/error/" mapping="/error/**"/> -->
    <!-- 配置velocity引擎 -->
    <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
	        <property name="resourceLoaderPath" value="/web" />
	        <property name="configLocation" value="/WEB-INF/velocity.properties" />
    </bean>

    <!-- 配置視圖的顯示 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">  
	       <property name="prefix" value="/" />
	       <property name="suffix" value=".html" />
	       <property name="contentType" value="text/html;charset=UTF-8" />
	        <property name="toolboxConfigLocation" value="/WEB-INF/toolbox.xml" />
	       <property name="dateToolAttribute" value="dateTool" />
	       <property name="numberToolAttribute" value="numberTool" />
	       <property name="exposeRequestAttributes" value="true" />
	       <property name="exposeSessionAttributes" value="true" />
   </bean>
   
   <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
     <!-- 1024*1即1k  byte-->
     <property name="maxUploadSize" value="104857600" />
     <property name="maxInMemorySize" value="4096" />
     <property name="defaultEncoding" value="UTF-8"></property>
  </bean>
    
    
    <!-- 總錯誤處理-->  
	<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  
	    <property name="defaultErrorView">
	        <value>/error/error</value>
	    </property>
	    <property name="defaultStatusCode"> 
	        <value>500</value>
	    </property>
		<property name="warnLogCategory"> 
	        <value>org.springframework.web.servlet.handler.SimpleMappingExceptionResolver</value>
	    </property> 
	</bean>
這裏輸入代碼

js代碼:ajax

$(document).on('ready', function() {
	
	 $("#btnSubmit").on('click', function() {
		 $('.modle').show ();
		 $('.modles').show ();
		 $("input[data-url]").val();
		 $("#select_form").ajaxSubmit({
				type: "POST",
				url:"full!update.do",
				dataType: "json",
				error : function(data) {
					alert("處理失敗!")
					$('.modle').hide ();
					 $('.modles').hide ();
				},
			    success: function(data){
			     	if(data.msg=='ok'){
			     		location.href=data.url;
			   		}
			    	else{
			    		if(data.msg != null || data.msg != ""){
			    			alert(data.msg);
			    		}else{
			    			alert("保存失敗");
			    			
			    		}
			    	}
			     	$('.modle').hide ();
					$('.modles').hide ();
				}
			});
	 });
	
});

html代碼spring

<form  id="select_form" 
			 		method="post" 
			 		enctype="multipart/form-data">
					<input type="file" name="logourl" />
				</form>

後臺代碼json

public class FileUploadVO {
	
	private CommonsMultipartFile logourl;
	public CommonsMultipartFile getLogourl() {
		return logourl;
	}
	public void setLogourl(CommonsMultipartFile logourl) {
		this.logourl = logourl;
	}
}

@RequestMapping(value = "/full!update.do")
	public void upload(FileUploadVO fileUpload,HttpServletRequest request,//
						HttpServletResponse response) {//
		String projectUrl = request.getContextPath();
		JSONObject ob = new JSONObject();
		PrintWriter out = null;
		Map<String,String> errorMap = new HashMap<String,String>();
		try {
			out = response.getWriter();
			appInfoEditBiz.editAppInfo(staffId,fileUpload,uploadUrl,pasCopyDir,uploadHomeDir,errorMap);// 
	
			ob.put("url", projectUrl + "/appOutputFull/listDetails.do?prodId="//
							+fileUpload.getProdid()+"&searchConditions="//
							+fileUpload.getSearchConditions()+"&isEdited=isEdited");//
			ob.put("msg","ok");
			out.print(ob);
		} catch (Exception e) {
			
			if(errorMap.size() > 0 && errorMap.get("result") != null){
				ob.put("msg",errorMap.get("result"));
			}else{
				ob.put("msg","保存失敗");
			}
			out.print(ob);
		}finally {
			if(out != null){
				out.flush();
				out.close();
			}
		}
	}
相關文章
相關標籤/搜索