Spring 2.5的阿贾克斯1.7,更新收到错误的反应反应、错误、Spring、阿贾克斯

2023-09-11 22:34:24 作者:仅剩那点余温

我使用Spring 2.5,并且使用的Ajax.Updater(1.7),以获得从春季的requestmapping的响应。

I am using Spring 2.5, and use Ajax.updater (1.7) to get an response from an requestmapping in Spring.

在请求映射我返回一个ModelAndView对象是指用一些文字的JSP。但问题是,当AJAX接收到响应,并更新容器,整个页面被包装在容器中,而不是仅仅jsp的输出,这是为什么

In the request mapping I am returning an ModelAndView which refers to an jsp with some text. But the problem is that when ajax received the response and update the container, the entire page is wrapped in the container instead just the jsp output, why is this

推荐答案

我好了,这是怎么使用AJAX和Spring 2.5

Ok, this is how I used ajax with Spring 2.5

首先,你需要控制对AJAX调用响应

First of all you need controller to respond on ajax calls

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数据响应

Then you need ajaxView to write ajax data to response

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调用

View resolver to resolve ajax calls

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

URL映射,映射AJAX网址Ajax控制器

Url mapping, map ajax url to ajax controller

<bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
         <props>
            <prop key="/getAjaxData.htm">ajaxController</prop>
            <prop key="/anotherMethod.htm">ajaxController</prop>
        </props>
    </property>
</bean>

控制器有豆ajaxResolver为MethodNameResolver的,控制器有一些服务或DAOS例如:

Controller has bean ajaxResolver as methodNameResolver, controller has some services or daos for example

  <bean name="ajaxController" class="my.test.web.ajax.AjaxController">
        <property name="service" ref="service"/>
        <property name="dao" ref="dao"/>
        <property name="methodNameResolver" ref="ajaxResolver"/>
    </bean>

方法名称解析

Method name resolver

<bean id="ajaxResolver"
          class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
        <property name="mappings">
            <props>
                <prop key="/getAjaxData.htm">getAjaxData</prop>
                <prop key="/anotherMethod.htm">anotherMethod</prop>
            </props>
        </property>
    </bean>

阿贾克斯查看解析器打电话给你的AJAX视图当您从AjaxController返回的ModelAndView

Ajax View resolver to call your ajax view when you return modelAndView from AjaxController

 <bean id="ajaxViewResolver" class="my.test.web.ajax.AjaxViewResolver">
        <property name="ajaxView">
            <bean class="my.test.web.ajax.AjaxView"/>
        </property>
        <property name="ajaxPrefix" value="ajax_"/>
    </bean>