Display list of jpg images in JSP using Spring MVC and Hibernate

I have searched for similar questions but misunderstood some things. My code also works, but some icons are shown instead of jpg images. It would be great if you can help figure this out.

Total, controller:

@Controller
public class MainController {

@Autowired
private MasterpieceService masterpieceService;

@RequestMapping(value={ "/", "/home" }, method = RequestMethod.GET)
public ModelAndView firstPage(){
    ModelAndView model = new ModelAndView();
    model.addObject("masterpiece", new Masterpiece());
    model.addObject("masterpieceList", masterpieceService.getMasterpieces());
    model.setViewName("home");
    return model;
}

(...other methods...)


    @RequestMapping(value = {"/uploadMasterpiece"}, method = RequestMethod.POST)
public ModelAndView uploadMasterpiece(@RequestParam("name") String name,
                                      @RequestParam("file") MultipartFile file,
                                      @RequestParam("comment") String comment) {
    ModelAndView model = new ModelAndView();

    if(file.isEmpty()){
        // oh no.
        model.setViewName("home");
    }else {
        Masterpiece masterpiece = new Masterpiece();
        try {
            masterpiece.setName(name);
            masterpiece.setImage(file.getBytes());
            masterpiece.setComment(comment);
            masterpieceService.addMasterpiece(masterpiece);

            model.setViewName("admin");
        }catch (Exception e){
            e.printStackTrace();
            model.setViewName("home");
        }
    }
    return model;
}}

      

and my JSP:

<c:if test="${!empty masterpieceList}">
<table>

    <c:forEach items="${masterpieceList}" var="masterpiece">
        <tr>
            <td>${masterpiece.name},</td>
            <td><img src="${pageContext.request.contextPath}/masterpiece/${masterpiece.image}" /> </td>
            <td>${masterpiece.comment} </td>
        </tr>
    </c:forEach>
</table>

      

At the end of the day, it should look like an artist's gallery presenting a table of images on the first page. Right now I don't have any css and I haven't been looking for how to make a good table from the output, but the question is about images. It looks like this:

enter image description here

with icons instead of images, and I'm wondering why ...

Decision

After comments and answers, I will eventually find out the solution. I used to try to pass images directly and not as URL values, which seems like bad practice. Here is the code in the controller for my case:

@RequestMapping(value = "/something/getImg{masterpieceId}", method = RequestMethod.GET,
        produces = MediaType.IMAGE_JPEG_VALUE)
public ResponseEntity<byte[]> ListImage(@PathVariable long masterpieceId) throws IOException{
    Masterpiece m = masterpieceService.getMasterpieceById(masterpieceId);
    byte [] image = m.getImage();
    final HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.IMAGE_JPEG);
    return new ResponseEntity<byte[]>(image, headers, HttpStatus.CREATED);
}

      

And my updated form:

 <c:if test="${!empty masterpieceList}">
<table>

    <c:forEach items="${masterpieceList}" var="masterpiece">
        <tr>
            <td>${masterpiece.name},</td>
            <td><img src="/something/getImg${masterpiece.masterpieceId}" /> </td>
            <td>${masterpiece.comment} </td>
        </tr>
    </c:forEach>
</table>

      

+3


source to share


2 answers


Images cannot (should not) be inserted as data in HTML. The image must be loaded with a URL.

  • Make HTML like this: img src = "/ something / getImg? Id = 123"
  • Create a controller that accepts the url "something / getImg".
  • In controller, return an image using Servlet.Outputstream response


For each image you want to display, create an HTML IMG element. Each "src" IMG attribute will point to a specific URL for that image: .getImage? Id = 111, getImage? Id = 112, etc. The controller method will look for the "id" query parameter and return an image for that id.

+3


source


Broken image means wrong image url. Can you tell us what the href value for this image is on your page.



+2


source







All Articles