扩展Spring MVC 用以支持Controller对Ajax的处理

pring Mvc中没有特别针对于Ajax请求的处理Controller.但是AbstractController和    

SimpleFormController也是可以针对Ajax的请求给与相应的.但显得有一点点的不自然. 

@Override     
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,      
        BindException ex) {      
    String userId=request.getParamater("userId");      
                 Map userInfoMap=userDao.getUserBaseInfo(userId);      
                 JSONObject object = new JSONObject();      
                 object.put("userInfoMap",userInfoMap);      
    response.setContentType("text/Xml;charset=gbk");      
    PrintWriter out = null;      
    try {      
        out = response.getWriter();      
             out.println(object.toString());      
                   
    }      
    catch (IOException ex1) {      
        ex1.printStackTrace();      
    }finally{      
                           out.close();      
                  }      
    return null;      
} 

 

 @Override  
        protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,   
                        BindException ex) {   
                String userId=request.getParamater("userId");   
                  Map userInfoMap=userDao.getUserBaseInfo(userId);   
                  JSONObject object = new JSONObject();   
                  object.put("userInfoMap",userInfoMap);   
                response.setContentType("text/Xml;charset=gbk");   
                PrintWriter out = null;   
                try {   
                        out = response.getWriter();   
                         out.println(object.toString());   
                            
                }   
                catch (IOException ex1) {   
                        ex1.printStackTrace();   
                }finally{   
                            out.close();   
                   }   
                return null;   
        }   

 

说明一下我向客户端页面response的是一个JSon对象,选择JSon这种数据格式的两个原因是:    

1.体积小可降低流量.    

 2.Js端解码十分方便,Js对JSon格式数据有原生的支持.     

 

使用Spring Mvc原有的SimpleFormController,从上边的例子上看是可以实现的,但是每次都要写"将JSonObject写入流,关流的代码.很累,很麻烦."   

所以,扩展了两个新的Controller,以简化对Ajax请求的处理.    

JSONObjectAbstractController 用于简单的从request.getParamater参数的情况.    

JSONObjectAbstractController 的原代码:    

 

package com.javaest.ext.spring.mvc.controller;      
     
import java.io.IOException;      
import java.io.PrintWriter;      
     
import javax.servlet.http.HttpServletRequest;      
import javax.servlet.http.HttpServletResponse;      
     
import net.sf.json.JSONObject;      
     
import org.springframework.web.servlet.ModelAndView;      
import org.springframework.web.servlet.mvc.AbstractController;      
     
public abstract class JSONObjectAbstractController extends AbstractController {      
     
    @Override     
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)      
            throws Exception {      
        JSONObject jsonObject = handleAjaxRequestInternal(request, response);      
        response.setContentType("text/Xml;charset=gbk");      
        PrintWriter out = null;      
        try {      
            out = response.getWriter();      
            out.println(jsonObject.toString());      
        }      
        catch (IOException ex1) {      
            ex1.printStackTrace();      
        }      
        finally {      
            out.close();      
        }      
     
        return null;      
    }      
     
    protected abstract JSONObject handleAjaxRequestInternal(HttpServletRequest httpServletRequest,      
            HttpServletResponse httpServletResponse) throws Exception;      
     
}    

 

 

package com.javaest.ext.spring.mvc.controller;   
  
import java.io.IOException;   
import java.io.PrintWriter;   
  
import javax.servlet.http.HttpServletRequest;   
import javax.servlet.http.HttpServletResponse;   
  
import net.sf.json.JSONObject;   
  
import org.springframework.web.servlet.ModelAndView;   
import org.springframework.web.servlet.mvc.AbstractController;   
  
public abstract class JSONObjectAbstractController extends AbstractController {   
  
        @Override  
        protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)   
                        throws Exception {   
                JSONObject jsonObject = handleAjaxRequestInternal(request, response);   
                response.setContentType("text/Xml;charset=gbk");   
                PrintWriter out = null;   
                try {   
                        out = response.getWriter();   
                        out.println(jsonObject.toString());   
                }   
                catch (IOException ex1) {   
                        ex1.printStackTrace();   
                }   
                finally {   
                        out.close();   
                }   
  
                return null;   
        }   
  
        protected abstract JSONObject handleAjaxRequestInternal(HttpServletRequest httpServletRequest,   
                        HttpServletResponse httpServletResponse) throws Exception;   
  
} 

 

JSONObjectSimpleFormController 用于自动将请求参数绑定到Command对象的情况.    

JSONObjectSimpleFormController 的代码:    

package com.javaest.ext.spring.mvc.controller;      
     
import java.io.IOException;      
import java.io.PrintWriter;      
     
import javax.servlet.http.HttpServletRequest;      
import javax.servlet.http.HttpServletResponse;      
     
import net.sf.json.JSONObject;      
     
import org.springframework.validation.BindException;      
import org.springframework.web.servlet.ModelAndView;      
import org.springframework.web.servlet.mvc.SimpleFormController;      
     
public abstract class JSONObjectSimpleFormController extends SimpleFormController {      
     
    @Override     
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,      
            BindException ex) {      
        JSONObject object = onAjaxSubmit(request, response, cmd, ex);      
     
        response.setContentType("text/Xml;charset=gbk");      
        PrintWriter out = null;      
        try {      
            out = response.getWriter();      
            out.println(object.toString());      
        }      
        catch (IOException ex1) {      
            ex1.printStackTrace();      
        }      
        finally {      
            out.close();      
        }      
     
        return null;      
    }      
     
    public abstract JSONObject onAjaxSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,      
            BindException ex);      
     
}

 

package com.javaest.ext.spring.mvc.controller;   
  
import java.io.IOException;   
import java.io.PrintWriter;   
  
import javax.servlet.http.HttpServletRequest;   
import javax.servlet.http.HttpServletResponse;   
  
import net.sf.json.JSONObject;   
  
import org.springframework.validation.BindException;   
import org.springframework.web.servlet.ModelAndView;   
import org.springframework.web.servlet.mvc.SimpleFormController;   
  
public abstract class JSONObjectSimpleFormController extends SimpleFormController {   
  
        @Override  
        protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,   
                        BindException ex) {   
                JSONObject object = onAjaxSubmit(request, response, cmd, ex);   
  
                response.setContentType("text/Xml;charset=gbk");   
                PrintWriter out = null;   
                try {   
                        out = response.getWriter();   
                        out.println(object.toString());   
                }   
                catch (IOException ex1) {   
                        ex1.printStackTrace();   
                }   
                finally {   
                        out.close();   
                }   
  
                return null;   
        }   
  
        public abstract JSONObject onAjaxSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,   
                        BindException ex);   
  
}  

 

 

你可能感兴趣的:(spring,json,Ajax,mvc,servlet)