栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Spring 2.5 Ajax 1.7,更新程序收到错误响应

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring 2.5 Ajax 1.7,更新程序收到错误响应

好的,这就是我在Spring 2.5中使用ajax的方式

首先,您需要控制器来响应ajax调用

public class AjaxController extends MultiActionController {    // injected services, daos here    public ModelAndView getAjaxData(HttpServletRequest request, HttpServletResponse response) throws Exception {        String data = // construct your ajax response here - string, json, xml etc        model.put("data", data);        return new ModelAndView("ajax_foo", model);    }     public ModelAndView anotherMethod(HttpServletRequest request, HttpServletResponse response) throws Exception {        String data = // construct your ajax response here - string, json, xml etc        model.put("data", data);        return new ModelAndView("ajax_foo", model);    }}

然后您需要ajaxView来编写ajax数据以进行响应

public class AjaxView extends AbstractView {    @Override    protected void renderMergedOutputModel(Map map, HttpServletRequest request, HttpServletResponse response) throws Exception {        String ajaxResponse = map.get("data");  response.setContentType("text/plain; charset=UTF-8");        response.getOutputStream().write(ajaxResponse.getBytes());}

查看解析器以解决Ajax调用

public class AjaxViewResolver extends AbstractCachingViewResolver {    private String ajaxPrefix;    private View ajaxView;    @Override    protected View loadView(String viewName, Locale locale) throws Exception {        View view = null;        if (viewName.startsWith(this.ajaxPrefix)) { view = ajaxView;        } else {        }        return view;    }    public void setAjaxPrefix(String ajaxPrefix) {        this.ajaxPrefix = ajaxPrefix;    }    public void setAjaxView(View ajaxView) {        this.ajaxView = ajaxView;    }}

网址映射,将ajax网址映射到ajax控制器

<bean id="simpleUrlMapping" >    <property name="mappings">         <props> <prop key="/getAjaxData.htm">ajaxController</prop> <prop key="/anotherMethod.htm">ajaxController</prop>        </props>    </property></bean>

控制器具有bean ajaxResolver作为methodNameResolver,例如,控制器具有一些服务或dao

  <bean name="ajaxController" >        <property name="service" ref="service"/>        <property name="dao" ref="dao"/>        <property name="methodNameResolver" ref="ajaxResolver"/>    </bean>

方法名称解析器

<bean id="ajaxResolver"          >        <property name="mappings"> <props>     <prop key="/getAjaxData.htm">getAjaxData</prop>     <prop key="/anotherMethod.htm">anotherMethod</prop> </props>        </property>    </bean>

从AjaxController返回modelAndView时,Ajax View解析器调用您的Ajax视图

 <bean id="ajaxViewResolver" >        <property name="ajaxView"> <bean />        </property>        <property name="ajaxPrefix" value="ajax_"/>    </bean>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/615839.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号