【问题标题】:can't get bean to show on jsp无法让 bean 在 jsp 上显示
【发布时间】:2014-12-23 12:40:18
【问题描述】:

我有一个名为 User.java 的 bean。

在 DAO 中,(从数据库中)提取一个名为 classRegistrationDate 的字段,并将其设置为 User.classRegistrationDate。

我创建了一个名为 isRegistrationLocked 的布尔值,如果 registrationDate 在 2014 年 12 月 20 日之前,我将其设置为 true,否则设置为 false。

豆子:

public Boolean isRegistrationLocked= false;

public boolean getIsRegistrationLocked() {
    return isRegistrationLocked;
}

public void setRegistrationLocked(boolean isRegistrationLocked) {
    this.isRegistrationLocked= isRegistrationLocked;
}

道:

DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date classRegistrationDate= user.getClassRegistrationDate();
Date releaseDate= df.parse("12/20/2014");

if(classRegistrationDate.before(releaseDate))
{  fi.setRegistrationLocked(true); }
else
{  fi.setRegistrationLocked(false); }

数据已经过验证,值正在被正确传递和设置。

在jsp中,我需要根据isRegistrationLocked的值来显示和隐藏一个。

JSP:

<html:hidden property="user.isRegistrationLocked" value="${user.isRegistrationLocked}" />

<c:if test="${user.isRegistrationLocked eq false}">
    <tr>
        <td class="text"> FALSE </td>
    </tr>
</c:if>

<c:if test="${user.isRegistrationLocked ne true}">
    <tr>
        <td class="text"> TRUE </td>
    </tr>
</c:if>

但该值始终为 false,因此无法正确访问。 我试过只使用输出值,甚至没有显示;它在 html 中显示为空白。

我哪里出错了,我怎样才能得到价值来评估?

【问题讨论】:

  • 数据库返回的日期是多少
  • true eq false 为 false 且 true 不等于 true 也为 false,... 提示:要么在 getter 方法中设置断点,要么直接将值写入 jsp,进行验证。
  • 如果将public Boolean isRegistrationLocked= false; 替换为public Boolean isRegistrationLocked= true;,您的jsp 中是否总能得到一个true?如果您可能没有访问您认为的实例。
  • 尝试将你的 getter 重命名为 isRegistrationLocked 而不是 getRegistrationLocked ?
  • @Brovoker,我的真/假测试有 2 个日期 - 2014 年 12 月 1 日、2014 年 12 月 23 日。重命名也没有用。

标签: java spring jsp


【解决方案1】:

如果你想与${} 取得联系,你应该提出要求。不幸的是,我现在不使用什么来处理请求。可以建议两种方式:

1)Servlet/JSP。

将对象请求:

request.setAttribute("user", User);

2) 弹簧控制器。 把对象放到模型中,像这样:

@RequestMapping(value = "/", method = RequestMethod.POST)
public String user(HttpServletResponse response, Model model) {
    ***
    Configuring "user" object
    ***
    model.addAttribute("user", user);
    return "redirect:/*to your jsp*";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-17
    • 1970-01-01
    • 2013-04-26
    相关资源
    最近更新 更多