【问题标题】:Returning String or HTML to ajax request with spring 2.5使用spring 2.5将字符串或HTML返回到ajax请求
【发布时间】:2015-02-23 13:07:20
【问题描述】:

我正在使用 Spring 2.5(基于 XML 的配置)。在我的 Spring 控制器中,如何将字符串返回给我的 AJAX 请求? 我的ajax代码如下:

 jq("#editAdTextSave").click(function() {
        alert( "Handler for .click() called." );
        jq.ajax({
              url: 'bookingDetail.html',
              type: 'POST',
              data: 'action=updateAdText',
              success: function(data) {
                //called when successful
                alert(model);
                //$('#ajaxphp-results').html(data);
              },
              error: function(e) {
                //called when there is an error
                //console.log(e.message);
  }
});
    });

CONtroller onSubmit 需要一个 ModelAndView 对象返回值。但是我只需要返回一个字符串。我该怎么做。请建议。

【问题讨论】:

    标签: spring spring-mvc spring2.x spring-2.5


    【解决方案1】:

    您需要在映射方法中使用@ResponseBody。使用@ResponseBody 会将您返回的值写入HttpResponseBody,并且不会被评估为视图。这是快速演示:

    @Controller
    public class DemoController {
    
        @RequestMapping(value="/getString",method = RequestMethod.POST)
        @ResponseBody
        public String getStringData() {
            return "data";
        }    
    }
    

    【讨论】:

    • 我没有使用注释我实现 SimpleFormController 并使用受保护的 ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { method
    • @ResponseBody 是在 spring 3.0 中引入的,这里的问题是关于 Spring 2.5。 Spring javadoc
    【解决方案2】:

    如题,使用Spring 2.5@ResponseBody 是在 Spring 3.0 中引入的

    @见ResponseBody JavaDocs in Latest Spring


    回到问题。

    在我的 Spring 控制器中,我如何返回一个字符串...

    根据Spring Blog Post 的简单答案,您需要将OutputStream 作为参数添加到您的控制器方法中。

    基于 XML 的配置应该如下所示

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:component-scan base-package="your.controllers"/>
    
    </beans>
    

    你的控制器应该是这样的

    package your.controllers;
    
    @Controller
    public class YourController {
    
        @RequestMapping(value = "/yourController")
        protected void handleRequest(OutputStream outputStream) throws IOException {
            outputStream.write(
                "YOUR DESIRED STRING VALUE".getBytes()
            );
        }
    }
    

    最后,如果你想在没有任何注释的情况下这样做,那么;

    您的基于 XML 的配置应如下所示

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    
        <bean name="/yourController" class="your.controllers.YourController"/>
    
    </beans>
    

    你的控制器应该是这样的

    package your.controllers;
    
    public class YourController extends AbstractController {
    
        @Override
        protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
            response.getOutputStream().write(("YOUR DESIRED STRING VALUE").getBytes());
            return null;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-08
      • 1970-01-01
      • 1970-01-01
      • 2014-01-06
      • 1970-01-01
      • 1970-01-01
      • 2017-07-23
      • 1970-01-01
      相关资源
      最近更新 更多