0
votes

I need display data in jsp with controller, i have List with information for print in jsp.

When run this code i get error:

HTTP Status 500 - Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [entities.Pupil]: No default constructor found; nested exception is java.lang.NoSuchMethodException: entities.Pupil.()

Controller

 @Controller
   public class PupilController {
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public List add(@ModelAttribute Pupil pupil){
        System.out.println(pupil.toString());
        List<Pupil> pupilList = new ArrayList<Pupil>();
        pupilList.add(new Pupil(1, "Name", "Last", 13));
        pupilList.add(new Pupil(2, "Name", "Last", 55));
        pupilList.add(new Pupil(3, "Name", "Last", 41));
        return pupilList;
    }
   }

index.jsp

<body>
    <h2>Hello World!</h2>
    <a href="hello">click</a>
    <form action="/add" method="post">
        <p>1:</p><input type="text" name="one">
        <p>2:</p><input type="text" name="two">
        <p>3:</p><input type="text" name="three">
        <p>4:</p><input type="text" name="four">
        <input type="submit" value="button">
    </form>
</body>

add.jsp

<body>
  <h3>This is add.jsp</h3>
  <table>
      <thead>
        <tr>
            <td>ID</td>
            <td>NAME</td>
            <td>LAST</td>
            <td>YEAR</td>
        </tr>
      </thead>
      <tbody>
        <c:forEach  items="${pupilList}" var="tester">
            <tr>
                <td>${tester.id}</td>
                <td>${tester.name}</td>
                <td>${tester.last}</td>
                <td>${tester.year}</td>
            </tr>
        </c:forEach>
      </tbody>
  </table>
</body>
2
Make a default constructor in your Pupil class. - Naman Gala
Oh, i create default constructor in Pupil class, but in jsp page don't show data from pupilList only thead block - Bohdan Olehovich
Change return type of controller method from List to List<Pupil> - Naman Gala
changed but it did not help - Bohdan Olehovich
Did you include the tag lib for <c:forEach> in your jsp? Tag lib <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> - Naman Gala

2 Answers

0
votes

Try this

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ModelAndView add(@ModelAttribute Pupil pupil){
        System.out.println(pupil.toString());
        List<Pupil> pupilList = new ArrayList<Pupil>();
        pupilList.add(new Pupil(1, "Name", "Last", 13));
        pupilList.add(new Pupil(2, "Name", "Last", 55));
        pupilList.add(new Pupil(3, "Name", "Last", 41));
        ModelAndView view = new ModelAndView();
        view.addObject(pupilList);
        return view;
    }

Or

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(@ModelAttribute Pupil pupil, ModelMap model){
        System.out.println(pupil.toString());
        List<Pupil> pupilList = new ArrayList<Pupil>();
        pupilList.add(new Pupil(1, "Name", "Last", 13));
        pupilList.add(new Pupil(2, "Name", "Last", 55));
        pupilList.add(new Pupil(3, "Name", "Last", 41));
        model.put("pupilList", pupilList);
        return "page-name";
    }
0
votes

Make below changes in your code.

  1. Add default constructor in your Pupil class.
  2. Add tag lib in your jsp file. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  3. Include pom entry for JSTL.

    <dependency>
        <groupId>javax.servlet.jsp.jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>