Struts 2“如果"标签 [英] Struts 2 "if" tag

查看:37
本文介绍了Struts 2“如果"标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用 Struts 2.

In my application, I use Struts 2.

在一个页面中,我想使用 Struts 2 <s:if> 标签,但无法让它工作.

In one page, I want to use the Struts 2 <s:if> tag, but can't get it to work.

在操作中,我向视图公开了一个人"实体.

In the action, I expose a "person" entity to the view.

然后在页面中,我想将当前日期与此人的生日进行比较.

Then in the page, I want to compare the current date with the person's birthday.

我试过了:

<s:if test="person.birthday > new java.util.Date()">xxxx</s:if>

但它不起作用.

如何解决?

推荐答案

我相信您正在使用 Date 作为 person.brithday 的数据类型.您可以这样做方法如下

I believe that you are using Date as a data type for person.brithday.You can do it as follows way

如果您可以更改/修改您的操作,请将新的 java.util.Date() 作为新字段添加到操作中.

If you can change/Modify your action add new java.util.Date() to action as a new field.

另外使用 java.util.Date() 根本不是一个好习惯,因为它的大部分方法都被弃用了所以我建议你使用 java.util.Calendar哪个更灵活.

Additionally using java.util.Date() is not good practice at all since most of its method are deprecated so i suggest you to use java.util.Calendar which is more flexible.

您可以使用Date.equals()、Date.before() 和Date.after() 来比较2 个日期.所有您需要做的事情如下

You can use Date.equals(), Date.before() and Date.after() to compare 2 dates.All you need to do something as follows

<s:if test="%{person.brithday.before(currentdate)}">
    inside If block
 </s:if>
 <s:else>
    else block
</s:else>

我假设 currentDate 正在您的 Action 类中设置,但如果您想更改它以仅在 jsp 页面中使用它,可以更改它.

Where i am assuming that currentDate is being set in your Action class, but if you want to change it to use it only in jsp page can change it.

这篇关于Struts 2“如果"标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆