spring mvc整合ajax,spring mvc整合ajax

#13楼 得分:0回复于:2012-03-07 23:10:03

这样的问题,还不简单,还需要怎么解释呢?

直接在客户端发起ajax请求,在服务器的spring   controller里面处理。

例如:

public   class   SimpleConlleter   extends   BaseController{

private   String   charset   =   "UTF-8 ";

public   ModelAndView   handleRequest(HttpServletRequest   request,HttpServletResponse   response)

throws   Exception   {

if(request.getCharacterEncoding()   ==   null   &&   charset   !=   null){

request.setCharacterEncoding(charset);

}

if(charset   !=   null)

response.setCharacterEncoding(charset);

Integer   start   =   getIntRequestParam( "start ");

Integer   limit   =   getIntRequestParam( "limit ");

PageData   pdata   =   SingleObjectFactory.getObject(BudgetSettingDao.class).findFullProperties(start,limit);

GridDataModel   gdm   =   new   GridDataModel ();

gdm.setTotal(pdata.getTotal());

gdm.setRows(pdata.getPageData());

String   json   =   convertToJSON(gdm);

log.log(Level.INFO,json);

renderJSONResult(json);

}

/**

*   输出JSON   响应结果

*/

public   void   renderJSONResult(String   json){

try   {

PrintWriter   w   =   response.getWriter();

w.write(json);

}   catch   (IOException   e)   {

log.log(Level.SEVERE,e.getMessage(),e);

}

}

}

/**

*   转换配置

*   @return

*/

protected   JsonConfig   getJSONConfig(){

JsonConfig   jc   =   new   JsonConfig();

jc.registerDefaultValueProcessor(int.class,   DefaultValueProcessors.NULL);

jc.registerDefaultValueProcessor(Integer.class,   DefaultValueProcessors.NULL);

jc.registerDefaultValueProcessor(Long.class,   DefaultValueProcessors.NULL);

jc.registerDefaultValueProcessor(long.class,   DefaultValueProcessors.NULL);

jc.registerDefaultValueProcessor(Double.class,   DefaultValueProcessors.NULL);

jc.registerDefaultValueProcessor(double.class,   DefaultValueProcessors.NULL);

jc.registerDefaultValueProcessor(Float.class,   DefaultValueProcessors.NULL);

jc.registerDefaultValueProcessor(float.class,   DefaultValueProcessors.NULL);

jc.registerDefaultValueProcessor(Short.class,   DefaultValueProcessors.NULL);

jc.registerDefaultValueProcessor(short.class,   DefaultValueProcessors.NULL);

jc.registerJsonValueProcessor(long.class,new   LongJsonValueProcessorImpl());

jc.registerJsonValueProcessor(Long.class,new   LongJsonValueProcessorImpl());

jc.registerJsonValueProcessor(short.class,new   ShortJsonValueProcessorImpl());

jc.registerJsonValueProcessor(Short.class,new   ShortJsonValueProcessorImpl());

jc.registerJsonValueProcessor(int.class,new   IntegerJsonValueProcessorImpl());

jc.registerJsonValueProcessor(Integer.class,new   IntegerJsonValueProcessorImpl());

jc.registerJsonValueProcessor(float.class,new   FloatJsonValueProcessorImpl());

jc.registerJsonValueProcessor(Float.class,new   FloatJsonValueProcessorImpl());

jc.registerJsonValueProcessor(double.class,new   DoubleJsonValueProcessorImpl());

jc.registerJsonValueProcessor(Double.class,new   DoubleJsonValueProcessorImpl());

jc.registerJsonValueProcessor(Date.class,   new   DateJsonValueProcessorImpl( "yyyy-MM-dd "));

return   jc;

}

/**

*   将数据转成JSON格式

*   @param   data   数据

*   @return

*/

public   String   convertToJSON(Object   data){

return   JSONObject.fromObject(data,getJSONConfig()).toString();

}

}

文章转载请注明来源。新辉网主页 > 网页基础 > Javascript/Ajax >

标题:spring mvc整合ajax

地址:http://www.a55.com.cn/a/3498.html

关键词:spring,mvc整合ajax,Java,Web,开发

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值