comparing two valuestack string values in JSP - struts2 - struts2

Thanks in advance for your time.
I need to preselect a radio button if it has a saved value. I basically need to compare 2 strings in the valuestack to determine this.
(I can't use <s:radio at the moment because of some business rules I need to attach based on other input elements in the form).
I tried to do <s:set the value of the saved id inside s:iterate like below and then compare them like below but obviously I didnt get it right.
<s:set var="savedId" value="%{flag.problemId}"/>
<s:iterator value="problemIdList">
<s:set var="currentId" value='<s:property value="id"/>' />
<s:if test="%{#currentId.equals(#savedId)}" >
<input checked="checked" type="radio" name="problemId" id="problemId" value='<s:property value="id"/>'/> <s:property value="description"/> <br/>
</s:if>
<s:else>
<input type="radio" name="problemId" id="problemId" value='<s:property value="id"/>'/> <s:property value="description"/> <br/>
</s:else>
</s:iterator>
Basically I need to compare the two strings, my code is below. I know I can't compare with equals() like I have below - any ideas?
Thanks a bunch!
<s:set var="savedId" value="%{flag.problemId}"/> <s:iterator value="problemIdList">
<s:if test=' <s:property value="id"/>.equals(<s:property value="savedId"/>) '>
<input checked="checked" type="radio" name="problemId" id="problemId" value='<s:property value="id"/>'/> <s:property value="description"/> <br/>
</s:if>
<s:else>
<input type="radio" type="radio" name="problemId" id="problemId" value='<s:property value="id"/>'/> <s:property value="description"/> <br/>
</s:else>
Regards,
VeeCan

Have you tried:
<s:if test='id.equals(savedId)'>
If "id" is a string OGNL will allow you to use methods of String.

Access value form <s:iterator> into <s:if> (ognl with <s:if> and <s:iterator>)
For example: Suppose loadUserList is the iterator(containing UserName and Address) to be iterated in jsp,
UserName:<s:property escape="false" value="userName" />
Address:<s:property escape="false" value="address" />
<s:if test='userName.equals("Admin")'>This is admin User</s:if>
<s:else>This is not an admin user!</s:else>
OGNL with s:if and s:iterator

Related

Grails - Edit - does not retain the values when created

Grails issue :
When clicking over the Edit in the following screen : Domain and Variable values does not seem to retain their values (When creating it, we are able to enter the values - Domain and Variable, but it does not retain the values when editing..
Clicking over EDIT in the following screen :
does not retain the values of domain and variable alone:
.gsp code below
<tr>
<td>Domain<span style="color: red">*</span></td>
<td><g:if test="${isCreate}">
<g:select class="statSele" id="domain_name" name="domain_name" from="${com.datumrite.master.DomainMaster.list()}" optionKey="id" noSelection="['':'Select Domain']" onchange="${remoteFunction(
action:'driveVariableFromDomain',
params:'\'id=\'+escape(this.value)',
update: 'variable_select'
)}"></g:select> <br>
</g:if>
<g:else>
<input disabled="true" id="dN" name="name" type="text" style="margin-bottom:4px" value="${(data.domain_name instanceof String)?'':com.datumrite.master.DomainMaster.get(data.domain_name)}" />
</g:else>
</td>
</tr>
<tr>
<td>Variable<span style="color: red">*</span></td>
<td><g:if test="${isCreate}">
<span id="variable_select">
<g:select class="statSele" name="var_name" from="${[]}" value="${data?.var_name}" noSelection="['':'Select Variable']"></g:select></span> <br>
</g:if>
<g:else>
<input disabled="true" type="text" name="name" style="margin-bottom:4px;margin-left:5px" value="${data?.var_name}" />
</g:else></td>
</tr>
<tr>
<tr>
<td>Condition<span style="color: red">*</span></td>
<td><g:if test="${isCreate}">
<g:select class="statSele" id="constrain_type" name="constrain_type"
from="${['Lesser than','Greater than','Lesser than nor Equal','Greater than nor Equal','Equal To','Not Equal To']}"
value="${data?.constrain_type}" noSelection="['':'Select Condition']">
</g:select>
</g:if>
<g:else>
<input disabled="true" name="name" value="${data?.constrain_type}" type="text" style="margin-bottom:4px"/>
</g:else></td>
</tr>
And, I am using MySQL DB for the same.
can anyone help me with this issue ?
You need to make sure the subscription instance is passed when you click Edit link. (possibly under <g:link...> tag) The gsp you've posted is for create view. Check your List of Subscription view. (possibly show.gsp)

How to calculate sum in nested iteration in Struts2 tags

Total spots is not printed in the span
<s:set var="totalSpots" value="0"/>
<s:iterator var="spotSchedule" value="spotSchedules">
<s:set var="totalSpots" value="#totalSpots + %{spotSchedule.noOfSpots}" />
</s:iterator>
<span style='cursor:pointer;text-decoration:underline;padding-left:5px;color:blue;'>
<s:property value="#totalSpots"/>
</span>
Use # sign to reference your spotSchedule variable inside <s:iterator> tag.
<s:set var="totalSpots" value="0"/>
<s:iterator var="spotSchedule" value="spotSchedules">
<s:set var="totalSpots" value="#totalSpots + #spotSchedule.noOfSpots" />
</s:iterator>
<span style='cursor:pointer;text-decoration:underline;padding-left:5px;color:blue;'>
<s:property value="#totalSpots"/>
</span>

Grails If Statement not working?

I am currently trying to implement a nested if statement on a view in my Grails application. I have the code below which firstly checks to to if a value is true and if so it renders some code (this works). Then if this is not true it falls into the else statement, in here I need a nested If Statement to look at a value and render a different piece of code based on the value.
<g:if test="${a?.staticValue == true}">
<g:field type="text" name="staticText" readonly="true" value="${a?.data}"/>
</g:if>
<g:else>
<g:if test="${a?.type == 'Door'}">
<g:field type="text" name="doorField" value="" placeholder="${a?.wood}"/>
</g:if>
<g:elseif test="${a?.type == 'Floor'}">
<g:field type="text" name="floorField" value="" placeholder="${a?.laminate}"/>
</g:elseif>
<g:elseif test="${a?.type == 'Window'}">
<g:field type="text" name="windowField" value="" placeholder="${a?.blinds}"/>
</g:elseif>
</g:else>
Nothing gets rendered within the else statement and only the first box is shown so the nested if statements do not work for some reason and I don’t understand why. If I remove the if statement at the bottom and run the code below the value show "Door".
<g:field type="text" name="typeField" value="" placeholder="${a?.type}"/>
I don’t understand why this isn’t working and I am probably missing something silly but would really appreciate the help on this.
Thanks in advance
***EDIT****
So I have changed my code as suggested below to trim any white-space even though there shouldn’t be any and I get a weird error:
<g:if test="${a?.type.trim().equals('Door')}">
The error below seems to suggest that there is no data being pulled:
No signature of method: com.smstool.Dynam$VarType.trim() is applicable for argument types: () values: []
This is weird because I have added this else statement to the end and this return the value of "Door" to the view:
<g:else>
${a?.type}
</g:else>
Does anyone have any ideas what may be going on here as im confused :S Thanks
Try: if a?.staticValue is String try old java way:
<g:if test="${a?.staticValue}">
<g:field type="text" name="staticText" readonly="true" value="${a?.data}"/>
</g:if>
<g:else>
<g:if test="${a?.type.toString().equals('Door')}">
<g:field type="text" name="doorField" value="" placeholder="${a?.wood}"/>
</g:if>
<g:elseif test="${a?.type.equals('Floor')}">
<g:field type="text" name="floorField" value="" placeholder="${a?.laminate}"/>
</g:elseif>
<g:elseif test="${a?.type.equals('Window')}">
<g:field type="text" name="windowField" value="" placeholder="${a?.blinds}"/>
</g:elseif>
</g:else>
for case insensitive:
equals can be changed to : equalsIgnoreCase

retrieving the value of the included html in struts2

I've included an html in my JSP
< s: include value="test.html" />. this test.html contains radio button. How do i retrieve the value of the radio button and pass it to action?
Add this <s:include /> tag in a form in your main page. e.g.
<s:form action="Welcome">
<s:include value="test.jsp"></s:include>
<s:submit />
</s:form>
test.jsp:
<input type="radio" value="abc" name="test" >abc</input>
<input type="radio" value="def" name="test" >def</input>
And in your Action named Welcome :
HttpServletRequest request = ServletActionContext.getRequest();
System.out.println(request.getParameter("test"));
You can get the value of your radio button. Hope this helps!
<s:form action="sampleAction">
<s:include value="yourjspfile.jsp"/>
<s:submit />
</s:form>
yourjspfile.jsp
<s:textfield name="name" label="Enter your name"/>
You can specify the name property in your action class and can access it directly no need to get it from the request object.

Struts2: Hidden field and anchor tag <s:a> values In Action Class

I am working on a struts2 application. In my jsp page I have 2-3 anchor tag and 2-3 hidden fields like
<s:a href="#">File 1</s:a>
<s:a href="#">File 2</s:a>
<s:a href="#">File 3</s:a>
and
<s:hidden name=" hidden1" />
<s:hidden name=" hidden2" />
<s:hidden name=" hidden3" />
Now please let me know, In my action class how can I get value of all the hidden fields and anchor tag which was clicked.
I had tried following
<s:a href="#" action=”someAction”>File 1</s:a>
Its working but didn’t transfer value of hidden fileds.
Also
<s:a href="#" name=”File1” onclick=”submit”>File 1</s:a>
But no gain.
Looking for your reply.
Like Boris said, you need to put the hidden fields inside a form, and submit that form, OR you can add them as URL parameters to your links. The best method is probably using a form with POST so the hidden fields aren't on your browser's location bar.
Here's an example
<s:form id="myform" name="myform" action="someAction" method="POST">
<s:hidden name=" hidden1" value="first value"/>
<s:hidden name=" hidden2" value="second value"/>
<s:hidden name=" hidden3" value="third value"/>
Submit with link
<s:submit value="%{'Submit with button'}" />
</s:form>
Since this really has nothing to do with struts2, here's an example with pure HTML:
<form id="myform" name="myform" action="someAction.action" method="POST">
<input type="hidden" name=" hidden1" value="first value"/>
<input type="hidden" name=" hidden2" value="second value"/>
<input type="hidden" name=" hidden3" value="third value"/>
Submit with a link
<br/>
<input type="submit" value="Submit with a button"/>
</form>

Resources