如何在没有模型或模型的情况下将值从控制器传递到 JSP?
@RequestMapping(value = "/selectUser", method = RequestMethod.GET )
public String getTravelByUserId(@RequestParam("userId") Long theId) {
System.out.println("AdminController.getTravelByUserId");
// get travels from the service
List<Travel> travelList=travelService.getTravelByUserId(theId);
//add the user which authenticatedAdmin to the model
return "Admin/travel-list";
}
您可以使用请求 参数如下:
@RequestMapping(value = "/selectUser", method = RequestMethod.GET )
public String getTravelByUserId(@RequestParam("userId") Long theId, HttpServletRequest request){
....
request.setAttribute("userName", varToSend);
}
如何在没有模型或模型的情况下将值从控制器传递到 JSP 春天的阿特里布特?
您可以返回包含 JSON 的 HTTP 响应。让我们aasume,你想将一个User对象作为JSON返回,那么你将需要这样的东西(假设你的控制器是@RestController
(:
@RequestMapping(value = "/selectUser", method = RequestMethod.GET, consumes = "application/json", produces="application/json")
public ResponseEntity<User> getTravelByUserId(@RequestParam("userId") Long theId) {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json");
User userToReturn = new User();
return new ResponseEntity<User>(userToReturn, headers, HttpStatus.OK);
}
在客户端,您需要使用 Javascript 处理该 HTTP 消息。