Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Spring2.5将字符串或HTML返回到ajax请求_Spring_Spring Mvc_Spring2.x_Spring 2.5 - Fatal编程技术网

使用Spring2.5将字符串或HTML返回到ajax请求

使用Spring2.5将字符串或HTML返回到ajax请求,spring,spring-mvc,spring2.x,spring-2.5,Spring,Spring Mvc,Spring2.x,Spring 2.5,我正在使用Spring2.5(基于XML的配置)。在我的Spring控制器中,如何将字符串返回到AJAX请求? 我的ajax代码如下所示: jq("#editAdTextSave").click(function() { alert( "Handler for .click() called." ); jq.ajax({ url: 'bookingDetail.html', type: 'POST',

我正在使用Spring2.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);
  }
});
    });

Submit上的控制器需要ModelAndView对象返回值。但是我只需要返回字符串。我如何才能这样做。请建议。

您需要在映射方法中使用
@ResponseBody
。使用
@ResponseBody
会将返回的值写入
HttpResponseBody
,并且不会作为视图进行计算。下面是快速演示:

@Controller
public class DemoController {

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

如问题中所述,使用了Spring 2.5
@ResponseBody
是在Spring3.0中引入的

@看


回到问题上来

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

您需要将
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 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;
    }
}

最后,如果你不想做任何注释,那么

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

我没有使用注释我实现SimpleFormController并使用受保护的ModelAndView onSubmit(HttpServletRequest请求、HttpServletResponse响应、Object命令、BindException错误)抛出异常{method
@ResponseBody
是在spring 3.0中引入的,这里的问题是关于spring 2.5的。