简体   繁体   English

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

[英]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. 我需要在带有控制器的jsp中显示数据,我具有带有要在jsp中打印的信息的List

When run this code i get error: 运行此代码时出现错误:

HTTP Status 500 - Request processing failed; HTTP状态500-请求处理失败; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [entities.Pupil]: No default constructor found; 嵌套的异常是org.springframework.beans.BeanInstantiationException:无法实例化[entities.Pupil]:没有找到默认的构造函数;默认是0。 nested exception is java.lang.NoSuchMethodException: entities.Pupil.() 嵌套的异常是java.lang.NoSuchMethodException:Entitys.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 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 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. 在您的Pupil类中添加default constructor
  2. Add tag lib in your jsp file. 在您的jsp文件中添加标签库。 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  3. Include pom entry for JSTL . 包括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.

相关问题 在Spring MVC中从控制器调用Jsp页面 - Calling Jsp page from controller in Spring MVC JSP /页面中的Spring MVC Controller类名 - Spring MVC Controller classname from JSP / page 如何检查JSP是否在Spring MVC中转到控制器,以及如何显示从控制器到JSP的列表? - How to check if jsp went to the controller in spring mvc and how to display list from controller to jsp? Spring MVC和JSP:如何将参数从控制器传递给JSP? - Spring MVC & JSP: How to pass a parameter from the controller to JSP? Spring MVC将一个值列表从JSP页面传递给控制器 - Spring MVC pass a list of values into controller from JSP page 在jsp页面中显示来自Controller MVC的异常消息 - Display exception's message from Controller MVC in the jsp page 如何使用Spring MVC在控制器中重定向另一个JSP页面? - how to redirect another jsp page in controller using spring MVC? 如何在下拉菜单中选择一个选项,从Spring MVC Controller到jsp页面获取值列表 - how to get the values list from Spring MVC Controller to jsp page on selecting an option in dropdown Spring MVC-如何将数据从jsp页面的按钮传递到Controller? - Spring MVC - How to pass data from jsp page's button to Controller? 如何在同一JSP页面中的Spring MVC中从控制器的选项卡单击中动态加载数据? - How to load data dynamically on tab click from controller in spring mvc in same jsp page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM