Home > other >  JSP table not display data from database
JSP table not display data from database

Time:04-08

I try everything but it still not working.I use tomcat 9 and java 8. I already check the database connectivity and it's fine. Data just don't display in jsp file. sorry for my english. This is pom file

            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
   <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.3.9</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.3.9</version>
        </dependency>

This is JSP file

            <h1>Contact List</h1>
            <h3><a href="new">New Contact</a></h3>
            <table border="1" width="1000px" height="50px">
                <tr>
                    <th>No</th>
                    <th>Name</th>
                    <th>Email</th>
                    <th>Address</th>
                    <th>Phone</th>
                    <th>Action</th>
                </tr>
                <c:forEach var="c" items="${listContact}" varStatus="status">
                    <tr>
                        <td>${status.index 1}</td>
                        <td>${c.name}</td>
                        <td>${c.email}</td>
                        <td>${c.address}</td>
                        <td>${c.phone}</td>
                    </tr>
                </c:forEach>
            </table>
        </div> ```

This is main controller file

```  @Autowired
    private ContactDAO contactDAO;

    @RequestMapping(value = "/",method = RequestMethod.GET)
    public ModelAndView listContact(ModelAndView mv) throws IOException {
        List<Contact> getListContact = contactDAO.getAll();
        mv.addObject("listContact", getListContact);
        mv.setViewName("index");
        return mv;```

CodePudding user response:

Your request mapping should not be empty. Try to map the correct view (maybe value = "/index")? Try something like this:

@Autowired
ContactDAO contactDAO;

@RequestMapping(value = "/index", method = RequestMethod.GET)
public ModelAndView listContact() {
    // Create new ModelAndView
    ModelAndView mv = new ModelAndView(); 
    // Get DAO resources
    List<Contact> getListContact = contactDAO.getAll();
    Contact c = new Contact();
    // Add objects to view
    mv.addObject("contact", c); 
    mv.addObject("listContact", getListContact);
    mv.setViewName("index");
    // return ModelAndView
    return mv;      
}

Also if you want me to help you i need more: - Full stack - Contact.java - index.jsp

If you'd like a way to properly structure and map your frontend try https://tiles.apache.org/framework/tutorial/basic/pages.html

CodePudding user response:

I found the solution the index.jsp is in web pages I try moving the index to web pages/WEB-INF/views and then when I rerun the program then it's working.

  • Related