Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Prathima Gajibiji
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try #session[@static.pkg.and.class@CONSTANT_NAME]

Just check if it's null or not.
 
Prathima Gajibiji
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...

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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic