I am using thyme leaf for one my project, I have problem in generating a QRCode and display the same in browser and I am using spring mvc framework.
Please help on the same.
Regards Mohan
Just send a HTTP request to your controller.
In your Thymeleaf template, set the source of your image to the url of your Spring MVC controller:
<img th:src="@{/controller/qr/${id}}" />
Provide a method in your controller that returns the image as ResponseEntity
:
@RequestMapping (value="/qr/{id}", method = RequestMethod.GET)
public ResponseEntity<byte[]> getQRImage(@PathVariable final String id) {
byte[] bytes = ...; // Generate the image based on the id
// Set headers
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_PNG);
return new ResponseEntity<byte[]> (bytes, headers, HttpStatus.CREATED);
}
More answers can be found in this question: Spring MVC: How to return image in @ResponseBody?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With