Struts 2 "if" tag - struts2

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.

Related

How to filter in a g:select?

I am new to Grails and gsp. I tried to find documentation on if this was possible - I found a similar question but was not able to successfully apply it.
I have this g:select statement:
<g:select name="users" multiple="multiple"
optionKey="id"
optionValue="name"
value="${user?.users*.id}"
from="${user.getUsers()}"
/>
I was wondering if it was possible to add a filter to this, or a condition. I can only select users that are in a certain country (users.country).
I was wondering if it was possible to add a filter to this, or a
condition.
It is.
It looks like you have a user property in the model and that object is an instance of some class that has a collection named users.
Something in your app is putting user in the model, likely a controller action. Best practice is to not embed much logic in a GSP so I would recommend either the controller do the filtering and put the qualifying objects in a separate model variable that the GSP can reference instead of user.getUsers(), or you could create a custom GSP tag which accepts a user and generates the select.

Play Framework - Variables in URL instead of as ? parameters

I'm a just starting to learn Play, so this is a basic question, but I've searched every term I can think of and can't find the answer.
All I want to do is have a page that on submit takes an ID from a text field and puts it in the URL directly (e.g. /myservice/person/123). Instead, the URL being generated contains the ID as a parameter (e.g. /myservice/person?id=123).
I know my controller is being invoked correctly if I type the URL in by hand, so I'm inclined to think my routes file is correct. This is what my entry looks like:
GET /person/:id controllers.PersonActions.getPerson(id: String)
So I'm assuming something is going wrong in my template, which looks like this:
#form(routes.PersonActions.getPerson(personID)) {
#* #inputText(personForm("id")) *#
<input type="text" name="id" value="#personID">
<input type="submit" value="Get">
}
You can see that I've commented out another way using #inputText also, but it behaves identically for me. I'm not tied to either method.
I've also tried this using POST, which removes the ID from the URL entirely, which I don't understand either, but since I'm just doing a simple query, I'd rather use GET.
Can anyone help me understand what's going on here? I feel like there's something fundamental about how routing/URLgeneration works that I'm not understanding, but I've gone through the tutorial and the docs so many times today I'm at a loss.
Thanks in advance.
Oh, and I'm using Java 7, Play 2.1, and Eclipse Kepler
I think you are trying to skip a step and map form request data from a view directly to a controller method. You will need to submit the form, have a controller parse the data and then render the next appropriate view based on the data you parsed from the form.
Remember that the personId in your view is a parameter that is bound server-side when the view is rendered. In this case, the submit url of the form is hard coded to whatever personId is passed in to the view at render time -- it doesn't change dynamically when the input box changes.
To fix, add a controller method to accept requests from /person (I'm guessing this based on the part in your question that says the form is being submitted to /person?id=123, in any case it should be the URL of the form you've shown above)
e.g. if you want to use a GET method for the form add:
GET /person controllers.PersonActions.findPerson(id: String)
and in your PersonActions controller (I'm assuming you're using Java, if scala I'm sure you can adapt it from the docs)
public static Result findPerson(String id){
/*
* I'm assuming this method exists and works because you say
* when you type in /person/123 manually the routing works
* and your router says getPerson is the method name for that URL.
*/
return getPerson(id);
}

Preserving objects through actions in Struts2

Can I preserve object across actions in Struts2 without using session?
I'm working on a Struts2 project and curious if there are some ways to preserving object
value once an action is ended.
What I'm trying to do is:
Calling an action to read from an uploaded file and prepare its contents as a list of objects.
Then I display the list as an example for the user.
Then the user can click on a submit button to call an action to process the list which is created from the first action.
Usually the list would be lost once the action is end. So, I have to store the list as a session, but I think it should have some better methods to achieve the I'm working on.
If you want to preserve data across requests the session is the normal mechanism for doing so.
If you don't use the session you'd need to essentially duplicate its functionality to maintain the data associated with the current user/conversation.
If you need to preserve the List, then you have to use the Session.
But if you (if I've understood your problem) just need to handle the List through
ActionOne (that creates the List) ->
JSPOne (thast shows the List to the user) ->
ACtionTwo (that receives the List from JSPOne and does some business with it)
, without having to worry about the fact that the user can change the List client-side (for example manipulating the web page with FireBug), then you don't need the session.
You just need the List object to be declared on ActionOne and ActionTwo (with getters and setters, at least the getter on ActionOne and at least the setter on ActionTwo), and to include List name and index on name attribute of JSP tags.
If you just draw it (like with <s:property/> tag), instad of using some tag that will post the value, like <s:textfield />, then just use an <s:hidden /> to post te value.
For example, if you have:
List<MyObject> myList //with getters and setters
and assuming that MyObject has id and value fields (with getters and setters),
in JSP instead of
<s:iterator value="myList" >
<s:property value="id"/>
<s:textfield name="value"/>
</s:iterator>
use
<s:iterator value="myList" status="ctr" >
<s:hidden name="myList[#ctr.index].id" value="id"/>
<s:property value="id"/> <!-- no need here, you won't post this -->
<s:textfield name="myList[#ctr.index].value" value="value" />
</s:iterator>
You will eventually validate a List like this throught the Visitor Validator (somewhewre in the future after all the rest is done :)
Enjoy

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.

How to map form values to an object for ASP.NET MVC HTTP-Post scenario?

i have a simple form for an ASP.NET MVC application. I have a form property that is named differently (for whatever reason) to the real property name.
I know there's [Bind(Exlcude="", Include="")] attribute, but that doesn't help me in this case.
I also don't want to have a (FormsCollection formsCollection) argument in the Action method signature.
is there another way I can define the mapping?
eg.
<%= Html.ValidationMessage("GameServer", "*")%>
results in ..
<select id="GameServer" name="GameServer">
<option value="2">PewPew</option>
</select>
this needs to map to..
myGameServer.GameServerId = 2; // PewPew.
cheers!
i believe you will need to define it in your controller arguments or else it wouldnt have any clue what to accept.
public ActionResult GameServer(string GameServer){
GServer myGameServer = new GServer();
myGameServer.GameServerId.ToString() = GameServer;
return View("GameServer");
}
you can pass in the name/id of the parameter your trying to go for on your view page, it will automagically know the value to recieve based on the id on your view.
What's wrong with having a FormCollection passed as argument? I had to do the same thing as you and I just excluded the GameServer property in the BindAttribute.
Another thing you have to note is that Html.ValidationMessage("GameServer", "*") won't work because the underlying model doesn't contain a GameServer property. You have to add it to the model. I don't know if there is a better way to do it, I didn't find but it was required to make ValidationMessage works
You can create you own ModelBinder (see how to) to do the custom mapping.
An overkill IMO, but you might have your reasons...

Resources