Saturday, December 6, 2014

Can NOT display Image and Text together


Hi All

Can someone please help, I'm trying to display Images with text together but finding it difficult. I can display Image or Text but not both together. I have two seperate controllers one for text and one for image.


DAO:-



Java Code:



public List<Product> listProductsById(Integer productId) {
List<Product> prod = em.createNamedQuery("Product.findByProductId")
.setParameter("productId", productId).getResultList();
return prod;
}
public byte[] loadImage(Integer productId){
return em.find(Product.class, productId).getimage();
}

Controller:-


Java Code:



@RequestMapping(value = "details/{id}", method = RequestMethod.GET)
public String showProductDetails(@PathVariable("id") Integer id, Model model)
throws IOException {

List<Product> product = olss.listProductById(id);
model.addAttribute("prodList", product);
return "prodDetails";
}


Java Code:



@RequestMapping( method = RequestMethod.GET)
public String showImage(@PathVariable("id") Integer id,
Model model, HttpServletResponse response) throws IOException {

byte[] image = olss.loadImage(id);
response.setContentType("image/jpeg, image/jpg, image/png, image/gif");
response.getOutputStream().write(image);
response.getOutputStream().flush();
response.getOutputStream().close();
model.addAttribute(image);
return null;
}

JSP


Java Code:



<img src="onlineshop/images/details?id=${prod.productId}"/>

Thank you

No comments:

Post a Comment