-->

Struts 2 “if” tag

2019-08-30 19:55发布

问题:

In my application, I use Struts 2.

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.

I tried this:

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

But it does not work.

How to fix it?

回答1:

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

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

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.

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>

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.