如何使用JSTL

时间:2018-04-02 07:46:03

标签: java spring jsp spring-mvc jstl

Profile.jsp

<c:forEach var="p" items="${profile}">
            <tr>
            <td>${p.userId} </td>
             <td><c:out value="${p.name}" /></td>
            <td>${p.phone}</td>
            <td>${p.email}</td>
            <td>${p.address}</td>
            <td>${p.loginName}</td>
            </c:forEach>

userController.java

  @RequestMapping("/profile")
public String profile(Model m,HttpSession session) {
    Integer userId = (Integer) session.getAttribute("userId");
    m.addAttribute("profile", userService.profile(userId));
    return "profile"; // JSP
}

域类

package in.capp.domain;

 public class User {
private Integer userId;
private String name;
private String phone;
private String email;
private String address;
private String loginName;
private String password;
private Integer role;
private Integer loginStatus;


public User() {
    super();
}

public Integer getUserId() {
    return userId;
}

public void setUserId(Integer userId) {
    this.userId = userId;
}

public String getName() {
    return name;
}

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

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getLoginName() {
    return loginName;
}

public void setLoginName(String loginName) {
    this.loginName = loginName;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public Integer getRole() {
    return role;
}

public void setRole(Integer role) {
    this.role = role;
}

public Integer getLoginStatus() {
    return loginStatus;
}

public void setLoginStatus(Integer loginStatus) {
    this.loginStatus = loginStatus;
}

 }

浏览器错误

    org.apache.jasper.JasperException: An exception occurred processing JSP 
   page /WEB-INF/view/profile.jsp at line 46

  43:               </tr>
  44:               </c:if>
  45:               ${profile} }
   46:               <c:forEach var="p" items="${profile}">
  47:               <tr>
 48:                <td>${p.userId} </td>
 49:                 <td><c:out value="${p.name}" /></td>

如果我只是在${profile}中使用JSP标记打印h1,则会成功打印对象[in.capp.domain.User@812ee27]。 这意味着我正在获取对象[仅一个对象]但不能迭代该对象 我如何在我的jsp中打印该对象值? 我在我的项目中添加了jstl jar,但面临这个问题。

1 个答案:

答案 0 :(得分:0)

看起来你正在做几乎所有事情,但你使用的是错误的jstl标签。 c:forEach将用于迭代集合,这是您现在不需要的。试试吧:

<tr>
<td><c:out value="profile.userId" /></td>
<td><c:out value="profile.name" /></td>
<td><c:out value="profile.phone" /></td>
<td><c:out value="profile.email" /></td>
<td><c:out value="profile.address" /></td>
<td><c:out value="profile.loginName" /></td>
...