简体   繁体   中英

how to display information in jsp page from controller Spring MVC

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>

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";
    }

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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM