[英]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. 在代码中进行以下更改。
default constructor
in your Pupil class. default constructor
。 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
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.