[英]Not able to iterate list of object in jstl jsp
I am creating web app using spring boot and jsp. 我正在使用Spring Boot和JSP创建Web应用程序。 The JSP page is just showing data which i am showing set in firm object. JSP页面仅显示我要显示在固定对象中的数据。 I am trying to fetch values from java object in c:foreach but every time getting exception every time i hit localhost to open user page. 我试图从c:foreach中的java对象获取值,但是每次我每次访问localhost打开用户页面时都遇到异常。
javax.el.PropertyNotFoundException: Property [text] not found on type [java.lang.String] javax.el.PropertyNotFoundException:在类型[java.lang.String]上找不到属性[text]
Controller class 控制器类
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.model.ListModel;
import com.model.User;
@Controller
public class UserController {
@RequestMapping(value = "/user", method = RequestMethod.GET)
public ModelAndView user() {
User user = new User();
List<ListModel> modelList = new ArrayList<>();
ListModel ls = new ListModel();
ls.setText("s1");
ls.setSelcted(true);
modelList.add(ls);
ls = new ListModel();
ls.setText("s2");
ls.setSelcted(true);
modelList.add(ls);
user.setFavoriteFrameworks(modelList);
ModelAndView modelAndView = new ModelAndView("user", "command", user);
return modelAndView;
}
@RequestMapping(value = "/users", method = RequestMethod.POST)
public String addUser(@ModelAttribute("favoriteFrameworks") User
user,BindingResult bindingResul, ModelMap model) {
model.addAttribute("username", user.getUsername());
model.addAttribute("password", user.getPassword());
model.addAttribute("address", user.getAddress());
model.addAttribute("receivePaper", user.isReceivePaper());
List<ListModel> modelList = user.getFavoriteFrameworks();
for(ListModel ll : modelList){
System.out.println(ll.getText());
}
model.addAttribute("favoriteFrameworks",
user.getFavoriteFrameworks());
return "users";
}
@ModelAttribute("webFrameworkList")
public List<ListModel> getWebFrameworkList() {
return getModel();
}
private List<ListModel> getModel() {
List<ListModel> modelList = new ArrayList<>();
ListModel ls = new ListModel();
ls.setText("s1");
ls.setSelcted(true);
modelList.add(ls);
ls = new ListModel();
ls.setText("s2");
ls.setSelcted(true);
modelList.add(ls);
ls = new ListModel();
ls.setText("s3");
ls.setSelcted(true);
modelList.add(ls);
ls = new ListModel();
ls.setText("s4");
ls.setSelcted(true);
modelList.add(ls);
return modelList;
}
}
JSP page view JSP页面视图
<%@taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>Spring MVC Form Handling</title>
</head>
<body>
<h2>User Information</h2>
<form:form method = "POST" action = "/users">
<table>
<tr>
<td><form:label path = "username">User Name</form:label></td>
<td><form:input path = "username" /></td>
</tr>
<tr>
<td><form:label path = "password">Age</form:label></td>
<td><form:input path = "password" /></td>
</tr>
<tr>
<td><form:label path = "address">Address</form:label></td>
<td><form:textarea path = "address" rows = "5" cols = "30" /></td>
</tr>
<tr>
<td><form:label path = "receivePaper">Subscribe Newsletter</form:label></td>
<td><form:checkbox path = "receivePaper" /></td>
</tr>
<tr>
<td><form:label path = "favoriteFrameworks">Favorite Web Frameworks</form:label></td>
<!-- <td><form:checkboxes items = "${webFrameworkList}" path = "favoriteFrameworks" /></td> -->
<c:forEach items="${webFrameworkList} " var="department">
<tr><td>${department.text}</td></tr>
<!-- <tr><td><form:checkbox
path="favoriteFrameworks" value='${department.text}' checked='${department.selcted}' />${department.text}</td></tr> -->
</c:forEach>
</tr>
<tr>
<td colspan = "2">
<input type = "submit" value = "Submit"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
Why am I getting this error? 为什么会出现此错误?
It looks like you have incorrectly set webFrameworkList
attribute you're trying to iterate over. 看来您尝试迭代的webFrameworkList
属性设置不正确。 The exception is telling you that it can't find a getText()
method on the department
var defined in the jsp (which, according to the exception message, is a String
and not your ListModel
). 异常告诉您无法在jsp中定义的department
var上找到getText()
方法(根据异常消息,该方法是String
而不是ListModel
)。
Consider eliminating the annotated @ModelAttribute
method and instead setting the model attribute in the ModelAndView user()
method. 考虑消除带注释的@ModelAttribute
方法,而在ModelAndView user()
方法中设置模型属性。
...
ModelAndView modelAndView = new ModelAndView("user", "command", user);
modelAndView.addObject("webFrameworkList", getModel());
return modelAndView;
Edit 编辑
Regarding the comment about form submission, the post handler method addUser
specifies 关于表单提交的评论,后处理程序方法addUser
指定
@ModelAttribute("favoriteFrameworks") User
but the form doesn't have a modelAttribute
defined. 但是表单没有定义modelAttribute
。
So there are two problems here. 因此,这里有两个问题。
favoriteFrameworks
is a List
, not a User
. favoriteFrameworks
是一个List
,而不是User
。 Change the post handler's signature to 将帖子处理程序的签名更改为
public String addUser(@ModelAttribute("user") User user,BindingResult bindingResul, ModelMap model)
Then change the form
opening tag to 然后将form
开始标签更改为
<form:form method = "POST" action = "/users" model attribute="user">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.