Results 1 to 8 of 8

Thread: no data when run my webapp

  1. #1
    Join Date
    May 2012
    Posts
    16

    Default no data when run my webapp

    Hi,

    I'm trying to create a HotelBooking application in SpringMVC.

    I create a database in MySQL named: dbHotels with a table: tblhotels with a few columns (name,address,...)

    In Eclipse I create a Config.java, HotelController.java, HotelDAO.java, HotelDAOcoll.java and a Hotel.java for my modelling

    In my Home.jsp I want to list all the hotels, For this moment I have one record in my table (tblhotels)

    When I try to run the webapp I see nothing ...

    This is my code for my Home.jsp, I dont know what to write in my value, I'm not sure its hotel.name

    Code:
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>
            <h1 style="color:red">Welcome on hotels.com!</h1>
            <p>test</p>
            <c:out value="${hotel.name}"/>		
        </body>
    </html>
    In my HotelDAOColl i have this code
    Code:
    package eu.hotel.dao;
    import eu.hotel.example.model.Hotel;
    import java.util.List;
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    import javax.persistence.Query;
    
    import org.springframework.stereotype.Repository;
    
    
    
    @Repository
    public class HotelDAOcoll implements HotelDAO{
    	@PersistenceContext
    	private EntityManager entityManager;
    	
    	@SuppressWarnings("unchecked")
    	public List<Hotel> findAll(){
    		
    		Query query = entityManager.createQuery("select h.name from Hotel h");
    		return query.getResultList();
    		
    	}
    }
    Thnx!

  2. #2
    Join Date
    Nov 2009
    Location
    Montreal, Quebec
    Posts
    398

    Default

    Could you post your @Controller class? Are you adding the result of findAll() as a model attribute when handling the request from the client?

  3. #3
    Join Date
    May 2012
    Posts
    16

    Default

    hi pgrimard, this is my controller class
    Code:
    package eu.hotel.example.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.validation.BindingResult;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import eu.hotel.example.dao.HotelDAO;
    import eu.hotel.example.dao.ReservationDAO;
    import eu.hotel.example.model.Reservation;
    
    @Controller
    public class HotelController {
    	@Autowired
    	private HotelDAO hoteldao;
    	@Autowired
    	private ReservationDAO reservationdao;
    		
    		
    	@RequestMapping("/")
    	public String getHotels(Model model) {
    		model.addAttribute("Hotel", hoteldao.findAll());
    		Reservation reservation = new Reservation();
    		//model.addAttribute("tblreservations", reservation);
    		return "home";
    	}
    }
    In mij HotelDAO I have my findall
    Code:
    package eu.hotel.example.dao;
    
    import java.util.List;
    import eu.hotel.example.model.Hotel;
    
    public interface HotelDAO {
    	List<Hotel> findAll();
    }

  4. #4
    Join Date
    Nov 2009
    Location
    Montreal, Quebec
    Posts
    398

    Default

    Well I see 2 potential problems.

    First is that findAll() will return a List or collection of Hotels, but in your view, you're not iterating over the collection. You're just attempting to access the name property of the "hotel" attribute. The "hotel" attribute is a collection, so the name property doesn't exist.

    Secondly, I think model attributes are case sensitive. In your controller, you're adding a model attribute as "Hotel", but in your view you're referencing a model attribute named "hotel".

  5. #5
    Join Date
    May 2012
    Posts
    16

    Default

    Thnx for your reply
    I changed some code in my view but it still doesnt work :-( :

    Code:
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>
            <h1 style="color:red">Welcome on hotels.com!</h1>
            <p>test</p>
            <c:forEach var="hotel" items="${Hotel}">
    			<tr>
    				<td>${hotel.name}</td>
    			</tr>
    		</c:forEach>
        </body>
    </html>

  6. #6
    Join Date
    Nov 2009
    Location
    Montreal, Quebec
    Posts
    398

    Default

    Well your view looks ok now, aside from not wrapping your <c:forEach> with a <table>.

    If we break things down to their basic elements, could you create a quick unit test to make sure that your findAll() method actually returns data?

    Besides that, I noticed in your first post that your view's file name is Home.jsp. I think view names are case sensitive too, so since you're returning "home" in your handler mapping, maybe it's not even loading the correct view. Are you able to confirm that?

    Btw, are there any exceptions being thrown when you browse to your page?

  7. #7
    Join Date
    May 2012
    Posts
    16

    Default

    Hi,

    How can I create a unit test in Eclipse?
    If I run my project I see my Home.jsp so thats not a problem
    I see my Welcome screen but no data...

  8. #8
    Join Date
    Nov 2009
    Location
    Montreal, Quebec
    Posts
    398

    Default

    Creating a unit test is pretty simple. You just have to make sure you using your Spring configuration to inject the Hotel repository. In the example below, the @ContextConfiguration is where you specify the location of your Spring configuration.

    So you might have something like this. If this unit test passes, then your DAO is indeed returning data and you should see the name of each hotel in the system console.

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "classpath:/META-INF/spring/applicationContext.xml" })
    public class TestHotelRepository {
    	
    	@Autowired
    	private HotelDAO hotelDao;
    	
    	@Test
    	public void list() {
    		List<Hotel> hotels = hodelDao.findAll();
    		
    		Assert.assertTrue(hotels.size() > 0);
    		
    		for(Hotel hotel : hotels) {
    			System.out.println(hotel.getName());
    		}
    	}
    }

Posting Permissions

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