Reputation: 3367
I am using Spring 2.5 (XML based configuration) .In my Spring controller how can i return a string back to my AJAX request? My ajax code is as follows:
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 requires a ModelAndView object return value.However I only require to return a string.How could I do that.Please suggest.
Upvotes: 0
Views: 1488
Reputation: 537
As in the question, Spring 2.5 is used; @ResponseBody
was introduced in Spring 3.0
@see ResponseBody JavaDocs in Latest Spring
Back to the question.
In my Spring controller how can i return a string ...
The simple answer as per Spring Blog Post you need to add OutputStream
as a parameter to your controller method.
You XML based configuration should look like this
<?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>
And your controller should look like this
package your.controllers;
@Controller
public class YourController {
@RequestMapping(value = "/yourController")
protected void handleRequest(OutputStream outputStream) throws IOException {
outputStream.write(
"YOUR DESIRED STRING VALUE".getBytes()
);
}
}
Finally if you want to do it without any kind of annotations at all, then;
Your XML based configuration should look like this
<?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>
And your controller should look like this
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;
}
}
Upvotes: 0
Reputation: 5868
You need to use @ResponseBody
in your mapping method. Using @ResponseBody
would write the value you return to HttpResponseBody
and will not be evaluated as view. Here's quick demo:
@Controller
public class DemoController {
@RequestMapping(value="/getString",method = RequestMethod.POST)
@ResponseBody
public String getStringData() {
return "data";
}
}
Upvotes: 0