I am creating web app using spring boot and jsp. The JSP page is just showing data which i am showing set in firm object. 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.
javax.el.PropertyNotFoundException: Property [text] not found on type [java.lang.String]
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
<%@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. 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
).
Consider eliminating the annotated @ModelAttribute
method and instead setting the model attribute in the ModelAndView user()
method.
...
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
@ModelAttribute("favoriteFrameworks") User
but the form doesn't have a modelAttribute
defined.
So there are two problems here.
favoriteFrameworks
is a List
, not a 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 method = "POST" action = "/users" model attribute="user">
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.