无法迭代jstl jsp中的对象列表

时间:2018-07-11 20:25:39

标签: java jsp jstl

我正在使用Spring Boot和JSP创建Web应用程序。 JSP页面仅显示我要显示在固定对象中的数据。 我试图从c:foreach中的java对象中获取值,但是每次我每次访问localhost打开用户页面时都遇到异常。

  

javax.el.PropertyNotFoundException:在类型[java.lang.String]上找不到属性[text]

控制器类

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页面视图

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

为什么会出现此错误?

1 个答案:

答案 0 :(得分:0)

您似乎错误地设置了要迭代的webFrameworkList属性。例外是告诉您无法在jsp中定义的getText()变量上找到department方法(根据异常消息,它是String,而不是您的{ {1}}。

请考虑删除带注释的ListModel方法,而在@ModelAttribute方法中设置模型属性。

ModelAndView user()

修改

关于表单提交的评论,后处理程序方法... ModelAndView modelAndView = new ModelAndView("user", "command", user); modelAndView.addObject("webFrameworkList", getModel()); return modelAndView; 指定

addUser

但该表单未定义@ModelAttribute("favoriteFrameworks") User

所以这里有两个问题。

  1. modelAttributefavoriteFrameworks,而不是List
  2. 该表单仍未指定

将帖子处理程序的签名更改为

User

然后将public String addUser(@ModelAttribute("user") User user,BindingResult bindingResul, ModelMap model) 开头标签更改为

form