File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Help on Struts2 if tag to implement logic:equal and logic:present Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Help on Struts2 if tag to implement logic:equal and logic:present" Watch "Help on Struts2 if tag to implement logic:equal and logic:present" New topic
Author

Help on Struts2 if tag to implement logic:equal and logic:present

Prathima Gajibiji
Greenhorn

Joined: Oct 06, 2007
Posts: 6
Hi,
I am new to Struts2 and currently working on Struts1.x to Struts2 migration. I need to change logic:equal and logic:present tags in the Jsp. Here is the problem I am facing:

STConstants.java has some constants such as:
public static final string ALT_PBR = "fusion";

Customer.java is a POJO having the following fields with getters and setters:
firstname, lastname,addressline1,addressline2,city,state,postalCode

In NewCmpAction.java we have:
Customer cust = new Customer();
//code which sets all the fields in cust object
.
.
//cust object is placed in session
session.put(STConstants.ALT_PBR,cust);

In slsDetail.jsp, I want to check if "cust" objects exists in session, if it exists then if the value of city field in "cust" object is equal to "CA".
In Struts 1.x we used logic:equal and logic:present but in Struts2 we do not have logic:equal and logic:present and I assume we have to implement this logic using if tag. I tried various combination but it didnt work.
I know if I use #session.fusion.city I will get the city value which I can compare to "CA" but I want to get the value "fusion" by referring to STConstants.ALT_PBR in the jsp. How can I do that? For eg: #session.???.city where ??? = STConstants.ALT_PBR.

Please help.
Also, how can I check if session has "cust" object? or how can I check if it is not present in session.


Thanks,
Prathima

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Try #session[@static.pkg.and.class@CONSTANT_NAME]

Just check if it's null or not.
Prathima Gajibiji
Greenhorn

Joined: Oct 06, 2007
Posts: 6
David Newton wrote:Try #session[@static.pkg.and.class@CONSTANT_NAME]

Just check if it's null or not.


I tried to first print the constant value in the Jsp to see if its working using s:property but it didnt work.
#session[@static.com.hcl.st.STConstants@ALT_PBR]
#session[@static.com.hcl.st.and.STConstants@ALT_PBR]
#session[@static.STConstants@ALT_PBR]

com.hcl.st is tha package name
STConstants is class name
ALT_PBR is constant name

None of them worked.

Please help.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

...

remove the word "static".

Issues like this are easily solved by looking at the Struts 2 documentation:

http://struts.apache.org/2.1.8/docs/ognl-basics.html
Prathima Gajibiji
Greenhorn

Joined: Oct 06, 2007
Posts: 6
Thanks David! It worked

<s:if test="#session[@com.hcl.st.and.STConstants@ALT_PBR].city.equals('CA')> ---> This works perfect! Thank you for your help on this.

However I am not able to check if the cust object exists in session.
For example:
<s:if test="#session[@com.hcl.st.and.STConstants@ALT_PBR] == null"> ---> This didnt work
<s:if test="#session[@com.hcl.st.and.STConstants@ALT_PBR] !=null"> ---> This didnt work
Both of them didnt work

How can I check if an object is present in a session? Please help!

Thanks
Prathima G
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help on Struts2 if tag to implement logic:equal and logic:present