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

Spring 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;
}

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

[list=1]
[*]体积小可降低流量.
[*]Js端解码十分方便,Js对JSon格式数据有原生的支持.
[/list]
[b]使用Spring Mvc原有的SimpleFormController,从上边的例子上看是可以实现的,但是每次都要写"将JSonObject写入流,关流的代码.很累,很麻纺."[/b]
[b]所以,扩展了两个新的Controller,以简化对Ajax请求的处理.[/b]
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;

}

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);

}


以下就是我为了支持Ajax请求的处理所扩展的SpringMvc Controller.
作者来自长春,一直在基于J2EE架构开发税务方面的应用.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值