如何创建一个Spring MVC,在完成任务后将一些文本打印到屏幕上



我想创建一个简单的处理程序来执行单个任务,然后将单词"Done"打印到屏幕上。

我需要创建一个视图模板吗?或者有一种简单的方法可以在不写模板的情况下将文本打印到屏幕上吗?

@RequestMapping(value = "/simple_handler", method = RequestMethod.GET)
public void simpleHandler(HttpServletRequest request, ModelMap model){
 this.carryOutSomeTask();
 // Print "Done" on the screen
}

请参阅http://static.springsource.org/spring/docs/3.0.x/reference/mvc.html#mvc-ann响应体

你只需要使用

 @RequestMapping(value = "/simple_handler", method = RequestMethod.GET)
 @ResponseBody
 public void simpleHandler(HttpServletRequest request, ModelMap model){
     this.carryOutSomeTask();
     return "Done";
 }

在控制器中:

@RequestMapping(value = "/simple_handler", method = RequestMethod.GET)
public void simpleHandler(HttpServletRequest request, ModelMap model){
  model.addAttribute("msg","Hello World");
}

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

我在很多Ajax相关的项目中都使用过这种方法,以返回按钮文本,例如:

@RequestMapping("/startMonitor")
public @ResponseBody String startMonitor() {
   printService.getMonitor().start();
   return MONITOR_STARTED;
}

最新更新