无法在jsp中读取bean属性

时间:2014-05-16 05:30:38

标签: java forms jsp spring-mvc el

我正在尝试从jsp中的bean访问属性,我遇到了奇怪的行为。 我之前做过这种事情,我不确定我在这里做错了什么。

控制器方法

    @RequestMapping(value = "/users/{userId}")
public String get(@PathVariable final String userId, final ModelMap model) {
    logger.debug("Entering get with userId as {}", userId);
    final UserDetails userDetails = new UserDetails();
    userDetails.setUser(userService.get(userId));
    userDetails.setRoles(roleService.fetchRoles());
    userDetails.setStatuses(Status.values());
    model.addAttribute("userDetails", userDetails);
    return VIEW_PREFIX + "editUser";
}

UserDetails bean:

public class UserDetails {

private User user;
private List<Role> roles;
private Status[] statuses;

public List<Role> getRoles() {
    return roles;
}

public Status[] getStatuses() {
    return statuses;
}

public User getUser() {
    return user;
}

public void setRoles(final List<Role> roles) {
    this.roles = roles;
}

public void setStatuses(final Status[] statuses) {
    this.statuses = statuses;
}

public void setUser(final User user) {
    this.user = user;
}

}

JSP:

                                                                                                                                                                                                                                                   

                        </tr>
                        <tr>
                            <td valign="top"><fmt:message key="page.viewusers.username" /></td>
                            <td valign="top"><form:input path="user.username" cssClass="required" disabled="true" id="username"/></td>

                        </tr>
                        <tr>
                            <td valign="top"><fmt:message key="page.viewusers.emailid" /></td>
                            <td valign="top"><form:input path="user.email" cssClass="required email"/></td>

                        </tr>
                        <tr>
                            <td valign="top"><fmt:message key="page.adduser.role" /></td>
                            <td valign="top">
                                <form:select path="user.role.id" cssClass="required select" cssStyle="width:220px !important;">
                                    <form:option value="-1">--Select--</form:option>
                                    <c:forEach items="${userDetails.roles}" var="role">
                                        <c:out value="${role.id}"></c:out>
                                    </c:forEach>
                                </form:select>
                            </td>
                        </tr>
                        <tr>
                            <td valign="top"><fmt:message key="page.viewusers.status" /></td>
                            <td valign="top">
                                <form:radiobuttons path="user.status" items="${userDetails.statuses}" itemLabel="label" cssClass="required" />
                        </tr>

我得到的错误是:

SEVERE: Servlet.service() for servlet [myapp] in context with path [/myapp] threw exception [An exception occurred processing JSP page /WEB-INF/views/user/editUser.jsp at line 178

175:                                    <form:select path="user.role.id" cssClass="required select" cssStyle="width:220px !important;">
176:                                        <form:option value="-1">--Select--</form:option>
177:                                        <c:forEach items="${userDetails.roles}" var="role">
178:                                            <c:out value="${role.id}"></c:out>
179:                                        </c:forEach>
180:                                    </form:select>
181:                                </td>


Stacktrace:] with root cause
 java.lang.NumberFormatException: For input string: "id"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at javax.el.ArrayELResolver.coerce(ArrayELResolver.java:166)
at javax.el.ArrayELResolver.getValue(ArrayELResolver.java:46)
at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67)
at org.apache.el.parser.AstValue.getValue(AstValue.java:169)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:985)
at org.apache.jsp.WEB_002dINF.views.user.editUser_jsp._jspx_meth_c_005fout_005f0(editUser_jsp.java:920)
at org.apache.jsp.WEB_002dINF.views.user.editUser_jsp._jspx_meth_c_005fforEach_005f0(editUser_jsp.java:890)
at org.apache.jsp.WEB_002dINF.views.user.editUser_jsp._jspService(editUser_jsp.java:394)

如果将代码更改为

<td valign="top">
                                <form:select path="user.role.id" cssClass="required select" cssStyle="width:220px !important;">
                                    <form:option value="-1">--Select--</form:option>
                                    <form:options items="${userDetails.roles}" itemLabel="name" itemValue="id"/>

                                </form:select>
                            </td>

我收到以下错误

   May 16, 2014 10:58:25 AM org.apache.catalina.core.StandardWrapperValve invoke
 SEVERE: Servlet.service() for servlet [myapp] in context with path [/myapp] threw     exception [An exception occurred processing JSP page /WEB-INF/views/user/editUser.jsp at line 177

 174:                               <td valign="top">
  175:                                  <form:select               path="user.role.id" cssClass="required select" cssStyle="width:220px !important;">
  176:                                      <form:option value="-1">--Select--</form:option>
177:                                        <form:options items="${userDetails.roles}" itemLabel="name" itemValue="id"/>
178:                                            
179:                                    </form:select>
180:                                </td>


  Stacktrace:] with root cause
 org.springframework.beans.NotReadablePropertyException: Invalid property 'id' of bean class [[Ljava.lang.Object;]: Bean property 'id' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:707)
at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:699)
at org.springframework.web.servlet.tags.form.OptionWriter.doRenderFromCollection(OptionWriter.java:202)
at org.springframework.web.servlet.tags.form.OptionWriter.renderFromCollection(OptionWriter.java:180)
at org.springframework.web.servlet.tags.form.OptionWriter.writeOptions(OptionWriter.java:133)
at org.springframework.web.servlet.tags.form.OptionsTag.writeTagContent(OptionsTag.java:168)
at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:103)
at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
at org.apache.jsp.WEB_002dINF.views.user.editUser_jsp._jspx_meth_form_005foptions_005f0(editUser_jsp.java:886)
at org.apache.jsp.WEB_002dINF.views.user.editUser_jsp._jspService(editUser_jsp.java:391)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)

ROLE.java

 @Entity
 @Table(name = "ods.Role")
 public class Role implements Serializable {

private static final long serialVersionUID = -1045869404415273225L;
@Id
@Column
private int id;

@NotBlank
@Column
private String name;

@Column
private String description;



public String getDescription() {
    return description;
}

public int getId() {
    return id;
}

public String getName() {
    return name;
}



public void setDescription(final String description) {
    this.description = description;
}

public void setId(final int id) {
    this.id = id;
}

public void setName(final String name) {
    this.name = name;
}

2 个答案:

答案 0 :(得分:1)

尝试更改

private int id;

private Integer id;

Role课程中,以及getter和setter。

答案 1 :(得分:0)

我已经解决了。

以下是回购代码(我使用的是spring jpa repos)

@Repository("roleRepo")
public interface RoleRepo extends JpaRepository<Role, Integer> {

@Query(value = "Select r.id, r.name, r.description from Role r")
List<Role> getAllRoles();

}

在这种情况下,即使签名显示List<Role>返回的类型为List<Object[]>

因此找不到id和name属性。

但是不明白为什么会这样做?如果有人可以帮忙解决这个问题,那就太好了。