I have a form in struts 2, that I show the value of a cookie like this:
<s:set var="name">${cookie["name"].value}</s:set>
<s:textfield name="name" value="%{#name}"/>
But everytime the cookie is null (when the user first logs in), it isn't blank, but it is showing "javabrains.model.User@17030961". I want that value to be blank in my form. So I tried to use if tag in Struts2, so whenever the name is null, it is set to blank, like this:
<s:if test="#name == ''"><s:set var="name" value="''"/></s:if>
Now it works, I can see my form being blank when there is no cookie present, but I want it to show the cookie's value, so I tried to use an else tag to get the cookie value:
<s:else><s:set var="name">${cookie["name"].value}</s:set> </s:else>
Now it doesn't work, because when the cookie is absent, it is still showing "javabrains.model.User@17030961".
Is there another solution that works better? To clarify I'm trying to make a form have blank values when there is no cookie present, and when there is a cookie, I want it to fill in that value.
It seems that your code is based on this answer to another question of your. In this code:
<s:set var="name">${cookie["name"].value}</s:set>
<s:textfield name="name" value="%{#name}" />
#name
is the <s:set>
variable's name, NOT the cookie name. To make it clear:
<s:set var="foobar">${cookie["name"].value}</s:set>
<s:textfield name="name" value="%{#foobar}" />
Then, your attempt to check the cookie value OUTSIDE the <s:set>
declaration is wrong, because you are trying to test the <s:set>
variable instead (and before it is even created)!
You should check the cookie value with something like:
<s:if test="#cookie.name!=null">
<s:set var="name">${cookie["name"].value}</s:set>
</s:if>
<s:else>
<s:set var="name" value="''"/>
</s:else>
, but since you are using only the CookieProvider interceptor, try using the ternary operator directly in JSP EL:
<s:set var="name">${not empty cookie["name"] ? cookie["name"].value : ''}</s:set>