Struts 2 - comparing objects - struts2

i'm trying to compare a value to an Enum in Struts:
<s:if test="%{myValue == com.app.core.Values.ALL}" />
where myValue is declared in the Action and com.app.core.Values is declared in another project that is referenced to this one.
Any tips?
Thanks

See Accessing static properties in the Struts documentation.
I think what you need is:
<s:if test="myValue == #com.app.core.Values#ALL">
You may also need to set the struts configuration constant struts.ognl.allowStaticMethodAccess to true.

Related

struts 2 assigning value of property tag to hidden field

I want to assign the value from field Description to a hidden field test.
But the problem is the "Description" contains sequence of words and the following code is assigning only first word to "test"
<s:hidden value=<s:property value="Description" /> name="test">
I am kind of new to struts. Can someone please help.
Also it would be nice if i get to know good tutorial links of struts2.
If this is a property in your action class you need not to use <s:property value="Description" /> as the Description will be available at the top of value stack and you can use OGNL to fetch the value from value-stack.This is what you need to do
<s:hidden value="%{description}" name="test" />
Please make sure the value in hidden filed should be similar to the name of property in your action class as it will be resolved to either the getter and setter in your action class or the public property defined in your action.
So this means value="%{description}" will be converted by OGNL like getDescription() and will try to find the getter in your action class to fetch the property value.
<s:hidden value="%{description}" name="test" />

Struts 2 "if" tag

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?
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.

<c:if> test="" in jsf

I'm using primefaces and jstl to loop a datatable.I have a List in backing bean for the columns.
private List<String> visableCols;
public initCols(){
visableCols.add("andOr");
visableCols.add("operator");
......
}
// getter & setter
In the xhtml page.
<p:comlumns var="col" value="#{theBean.visableCols}" >
<c:if test="#{col == 'andOr'}">
<!-- do sth here -->
</c:if>
</p:comumns>
but I found the c:if always false.I tried to print out the #{col} and compare w/ 'andOr',they are the same value.
If you are using PrimeFaces and you want to dynamically add and remove columns, PrimeFaces provides a specific way of doing that. You don't need any JSTL tags. For a good example of how to do it just look at their showcase example. It is pretty involved but fairly clean.
Note: you use the p:columns tag instead of p:column.

how to set a value to <s:hidden > from the action class without using list and iterator in struts 2?

In action class i am reading value from the database.
Let's say "abc".Now the "abc" value should be populated to jsp page.i.e "abc" value should set to s:hidden field in the jsp page.
Since it is single value , i don't want to use List in the action class.
Is there any other way to do that ?
Why would you want to use a list? Just provide the appropriate getter like:
public Object getAbc(){
return abc;
}
and in your page access it with simple OGNL expression like:
<s:hidden name="filedYouWantToSetThisValueTo" value="%{abc}"/>
Hope I got it right.

SparkViewEngine: Using Url.Content in global variable in Application.Spark

I am using ASP.NET MVC 2 with SparkViewEngine 1.1 and I get the error message An object reference is required for the non-static field, method, or property 'Spark.Web.Mvc.SparkView.Url.get' when I use the following code in the Application.spark view file
<global baseImagePath='Url.Content(Context,"~/Content/_images")' type="string" />
What I am trying to accomplish is basically define a base image path as a global variable that can be referenced in the page templates.
For example, in Home/Index.spark, which uses the master Application.spark, I would have the following code snippet in the view
<img src="${baseImagePath}" alt="..." />
As a workaround, I can set a hard-coded value when I set baseImagePath by doing <global baseImagePath='"~/Content/_images"' type="string" />, but I want to also be able to resolve the "~" before hand.
Any suggestions?
Thank you!
Edit
I am adding my solution here per Rob's suggestions
In Shared/_global.spark, I added <use namespace="System.Web" /> so that I have access to the System.Web namespace.
In Shared/Application.spark, I added
<global baseImagePath='VirtualPathUtility.ToAbsolute("~/Content/_images")' type="string" />
The problem here is that Url.Content(... is accessing the UrlHelper class in System.Web.Mvc from the SparkView instance. The problem is that when the Application.spark is being parsed for globals, you don't yet have an instance of a view because global variables are defined before the generated view class is instantiated. Therefore accessing and instance variable like Url from the view is not possible.
From the looks of it though, you're just trying to get an absolute path from a relative one. For this you could potentially just use the static VirtualPathUtility class from System.Web.
There are a number of methods on there like ToAbsolute(...) that make life easier. Alternatively, you could get it from the HttpContext.Current.Server.MapPath(...) method if you have the current context.
Hope that helps,
All the best,
Rob G

Resources