How do you create a Spring MVC that just prints some text to the screen after finishing a task?
I want to create a simple handler that performs a single task and then prints开发者_StackOverflow社区 the word "Done" to the screen.
Do I need to create a View template or is there an easy way to print text to the screen without writing a template?
@RequestMapping(value = "/simple_handler", method = RequestMethod.GET)
public void simpleHandler(HttpServletRequest request, ModelMap model){
this.carryOutSomeTask();
// Print "Done" on the screen
}
See http://static.springsource.org/spring/docs/3.0.x/reference/mvc.html#mvc-ann-responsebody
You just have to use
@RequestMapping(value = "/simple_handler", method = RequestMethod.GET)
@ResponseBody
public void simpleHandler(HttpServletRequest request, ModelMap model){
this.carryOutSomeTask();
return "Done";
}
In controller:
@RequestMapping(value = "/simple_handler", method = RequestMethod.GET)
public void simpleHandler(HttpServletRequest request, ModelMap model){
model.addAttribute("msg","Hello World");
}
In JSP:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<p>This is my message: ${msg}</p>
</body>
</html>
@RequestMapping(value = "/simple_handler", method = RequestMethod.GET)
public @ResponseBody String simpleHandler(){
this.carryOutSomeTask();
return "Done";
}
I have used this with a lot of Ajax related projects, to return a button text for example:
@RequestMapping("/startMonitor")
public @ResponseBody String startMonitor() {
printService.getMonitor().start();
return MONITOR_STARTED;
}
精彩评论