我想在我的html页面中获得一个背景图像,其中图像来自rest。下面是我的html代码
<img class="background-img" id="container" ng-src="{{::backgroundImgUrl}}">从我的controller.js上我会打电话给你,
var idx = Math.floor(Math.random() * (4 - 1)) + 1;
$scope.backgroundImgUrl = "http://myrestsvc.mydomain.com/api/images/v3/" + idx;我的输出应该如下,
<img class="background-img" id="container" ng-src="http://myrestsvc.mydomain.com/api/images/v3/5" src="http://myrestsvc.mydomain.com/api/images/v3/5">这是预期的行为,我不是java开发人员,我如何编写java控制器来实现这一点?
我使用Springv1.3.3和angularJS。提前谢谢。
发布于 2016-12-09 17:24:31
提供答案:将image_id添加到@RequestMapping中
和@PathVariable到处理程序方法参数
@ResponseBody
@RequestMapping(value = "/api/images/v3/{id}",
method = RequestMethod.GET,
produces = MediaType.IMAGE_JPEG_VALUE)
public byte[] handler(@PathVariable("id") int imageId){
InputStream in = servletContext.getResourceAsStream("/image"+ imageId +".jpg");
return IOUtils.toByteArray(in);
}https://stackoverflow.com/questions/41064773
复制相似问题