#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,开发