繁体   English   中英

如何从控制器Spring MVC在jsp页面中显示信息

[英]how to display information in jsp page from controller Spring MVC

我需要在带有控制器的jsp中显示数据,我具有带有要在jsp中打印的信息的List

运行此代码时出现错误:

HTTP状态500-请求处理失败; 嵌套的异常是org.springframework.beans.BeanInstantiationException:无法实例化[entities.Pupil]:没有找到默认的构造函数;默认是0。 嵌套的异常是java.lang.NoSuchMethodException:Entitys.Pupil。()

控制者

 @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>

尝试这个

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

要么

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

在代码中进行以下更改。

  1. 在您的Pupil类中添加default constructor
  2. 在您的jsp文件中添加标签库。 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  3. 包括JSTL的 pom条目。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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