Results 1 to 3 of 3
  1. #1
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default use string from db column as img src for thymeleaf?

    Hey people what is going on?

    What I am having trouble with is I am trying to display a list of items from a "inventory" on a view and I want to use one of the columns to save URL's for image sources. So far I have it to where It will display all items saved in the table, But I am having trouble pulling out the string from the imgUrl column. Can I get some help please?

    In the ViewController I have an ArrayList<String> that I haven't used yet. But I started with the idea of I will just add all the URL's to here but then I thought that if I start looping through nested lists in the view that its not going to produce what I am looking for.

    Any suggestions as to what I should try next?


    Java Code:
    	
    			<div th:unless="${#lists.isEmpty(items)}">
    				<dl th:each="item : ${items}">
    				<dt class="itemInfo">	
    				
    				<label>ID:</label>
    				<span th:if="${item.id}" th:text="${item.id}">ID: </span> <br/>
    				<span th:if="${item.id eq null}">No ID available</span> <br/>
    					
    				<label>Image URL:</label>
    
                                    <!-- here is where I try to add the image -->
    				<img th:src="@{item.imgUrl}"
    	         		src="${item.imgUrl}" width="350" height="200"/>
    	         		
    				<span th:if="${item.imgUrl}" th:text="${item.imgUrl}"></span> <br/>
    				<span th:if="${item.imgUrl eq null}">No image available</span> <br/>
    				
    				<label>Size:</label>
    				<span th:if="${item.size}" th:text="${item.size}">Size: </span> <br/>
    				<span th:if="${item.size eq null}">No size available</span> <br/>
    				
    				<label>Designer:</label>
    				<span th:if="${item.designer}" th:text="${item.designer}">Designer: </span> <br/>
    				<span th:if="${item.designer eq null}">No description available</span> <br/>
    				
    				<label>Description:</label>
    				<span th:if="${item.description}" th:text="${item.description}">Description: </span> <br/>
    				<span th:if="${item.description eq null}">No description available</span> <br/>
    				
    				<label>Color:</label>
    				<span th:if="${item.color}" th:text="${item.color}">Color: </span> <br/>
    				<span th:if="${item.color eq null}">No color available</span> <br/>
    				
    				<label>Type:</label>
    				<span th:if="${item.type}" th:text="${item.type}">Type: </span> <br/>
    				<span th:if="${item.type eq null}">No type available</span> <br/>
    				
    				<label>Price:</label>
    				<span th:if="${item.price}" th:text="${item.price}">Price: </span> <br/>
    				<span th:if="${item.price eq null}">No price available</span> <br/>
    				</dt>
    				</dl>
    				</div>
    				
    				<div th:if="${#lists.isEmpty(items)}">	
    				<p>You have no items in your inventory</p>
    				</div>
    Java Code:
    	
    	// inventory view
    	@GetMapping("/inventory")
    	public String inventory(HttpSession session, Model model) {
    		
    		List<Item> itemList = item.findAll();
    		
    		ArrayList<String> itemsImgUrlList = new ArrayList<String>();
    		
    		for (int i =1;i < item.count(); i++) {
    			String id = itemList.get(i).getImgUrl();			
    			itemsImgUrlList.add(id);
    		}
    		
    		if (itemList != null) {
    			model.addAttribute("items", itemList);
    		}
    		
    		boolean hasUserRole = hasUserRole();
    		boolean hasAdminRole = hasAdminRole();		
    		if (hasUserRole) {
    			session.setAttribute("userrole", hasUserRole);
    		}
    		else if (hasAdminRole) {
    			session.setAttribute("adminrole", hasAdminRole);
    		}
    		return "inventory";
    	}

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

    Default Re: use string from db column as img src for thymeleaf?

    Why do you want to add the URLs to a separate List?
    You already have the URL, in the Item.
    I'm not sure what you expect to gain?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default Re: use string from db column as img src for thymeleaf?

    yeah I had that same thought and stopped. I figured out the problem though. It is in the img src for the th:src. I used the @{} instead of ${} . I did that becaue from how i understood the documentation on their website I thought I was supposed to use the @for href and I was using an random image from the internet so I think thats why I associated it together. I changed it to a $ and now every thing works fine.

    Now I am working on figuring out how to upload images to my web app.

Similar Threads

  1. help with Thymeleaf please
    By Blue2525989 in forum Advanced Java
    Replies: 3
    Last Post: 03-29-2017, 08:32 PM
  2. Replies: 0
    Last Post: 10-18-2013, 06:03 PM
  3. Replies: 6
    Last Post: 10-04-2013, 11:52 AM
  4. Replies: 0
    Last Post: 01-17-2013, 12:43 PM
  5. Database Column vs String Concatenation
    By varun_33 in forum JDBC
    Replies: 1
    Last Post: 12-11-2007, 04:14 PM

Posting Permissions

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