Struts 2 “if” tag

旧时模样 提交于 2019-12-25 02:22:05

问题


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.



来源:https://stackoverflow.com/questions/8686763/struts-2-if-tag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!