Results 1 to 11 of 11
  1. #1
    zed420 is offline Member
    Join Date
    Dec 2009
    Location
    Leeds, UK
    Posts
    12
    Rep Power
    0

    Default 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
    Last edited by zed420; 12-06-2014 at 09:45 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,012
    Rep Power
    35

    Default Re: Can NOT display Image and Text together

    Where is the code where you are trying to show the image with some text?
    What kind of component are you trying to show them in?
    Is the text to be written over some part of the image? where?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    zed420 is offline Member
    Join Date
    Dec 2009
    Location
    Leeds, UK
    Posts
    12
    Rep Power
    0

    Default Re: Can NOT display Image and Text together

    Thank for your reply Norm here's my Product object, no text is not to be written over an image. it's a description of a product with product's image (eg. iphone, laptop etc)
    Java Code:
     @Id
        @Basic(optional = false)
    	@GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "PRODUCT_ID")
        private Integer productId;
        
        @Column(name = "PRODUCT_CODE")
        private String productCode;
        
        @Column(name = "PRODUCT_NAME")
        private String productName;
       
        @Column(name = "BUY_COST")
        private String buyCost;
        
        @Column(name = "DESCRIPTION")
        private String description;
       
        @Lob
        @Basic(fetch = FetchType.LAZY)
        @Column(name = "IMAGE")
        private byte[] image;
    
    	@Column(name = "MANUFACTURER")
        private String manufacturer;
        
        @Column(name = "QUANTITY")
        private Integer quantity;
        
        @Column(name = "REAL_COST")
        private String realCost;
        
        @JoinColumn(name = "CAT_FK_ID")
        @ManyToOne(optional = true)
        @ForeignKey(name = "catId")
        private Category catId;
    I have the getters and Setters
    Last edited by zed420; 12-06-2014 at 10:50 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,012
    Rep Power
    35

    Default Re: Can NOT display Image and Text together

    That code doesn't look like normal java. It must have a preprocessor that creates a java source file.

    What environment is this code for?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    zed420 is offline Member
    Join Date
    Dec 2009
    Location
    Leeds, UK
    Posts
    12
    Rep Power
    0

    Default Re: Can NOT display Image and Text together

    It's in Spring MVC, Hibernate and JSP

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,012
    Rep Power
    35

    Default Re: Can NOT display Image and Text together

    Move to Spring section
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    14

    Default Re: Can NOT display Image and Text together

    I still don't see what the actual problem is.

    I can display Image or Text but not both together.
    Why not? What happens when you try?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    28

    Default Re: Can NOT display Image and Text together

    You've shown us an <img> tag, and the GET call that handles it (showImage).
    But there's nothing there that shows us what you are trying to do with the showDetails method.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    zed420 is offline Member
    Join Date
    Dec 2009
    Location
    Leeds, UK
    Posts
    12
    Rep Power
    0

    Default Re: Can NOT display Image and Text together

    Thanks for your replies, there is a list of Products by clicking on there names it showsProductDetails in that detail you have image, productCode, productName, description, buyCost etc.
    By clicking on this link it shows everything expect the IMAGE image space is empty
    Java Code:
    <a href="details/${prod.productId}">	${prod.productName}</a>
    @RequestMapping(value = "details/{id}", method = RequestMethod.GET)
        public String showProductDetails(@PathVariable("id") Integer id, Model model)
                throws IOException { ...
    If I change the link to
    Java Code:
    <a href="image/${prod.productId}">	${prod.productName}</a>
    @RequestMapping(value = "image/{id}", method = RequestMethod.GET)
    	public String showImage(@PathVariable("id") Integer id,
    			Model model, HttpServletResponse response) throws IOException {...
    this will show only the image and nothing else. How can I show both (image and the rest of the info) together.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    28

    Default Re: Can NOT display Image and Text together

    You cannot stream an image and html at the same time.
    The HTML returned by your showProductDetails needs to include an <img> tag that will fetch the image from showImage.

    If your are not returning HTML, and the <a> is attached to some form of Ajax call, then you need to show us exactly what that call is doing.

    Finally if you still think you are doing what I suggest above then you need to post the full response HTML from showProductDetails.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    14

    Default Re: Can NOT display Image and Text together

    Basically the main problem here is not understanding the HTTP protocol and basic HTML and thus making wrong assumptions. I would spend some time with that before you continue learning something as complicated as Spring and JPA. In that same line - did you study using plain JDBC and SQL?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Button To Display Image And Text
    By muhammad naim in forum AWT / Swing
    Replies: 6
    Last Post: 07-30-2013, 07:21 PM
  2. Replies: 0
    Last Post: 12-09-2010, 05:56 PM
  3. Replies: 1
    Last Post: 12-01-2010, 06:35 PM
  4. Replies: 1
    Last Post: 05-31-2010, 08:46 AM
  5. How to display scrolling text and image on a JFrame
    By Abhi_vk in forum AWT / Swing
    Replies: 1
    Last Post: 06-20-2008, 10:19 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •