If statement inside TreeMap in Struts 2 - java

I have a TreeMap<Long,ArrayList<String>> that I need t print it in Struts2 jsp page. I have to add an If condition in such a way that If the content of ArrayList<String> contains certain character sequence I need to print it in one color and if does not have that character sequence I need to print it in different color.
In the below code, only else part is getting printed,
<s:set name="string1" value="check_Char" />
<s:iterator value="lgMap">
<h3>
<s:property value="key" />
</h3>
<table>
<s:iterator value="value">
<s:if test="%{<s:property />.indexOf(#string1)) == -1}">
<tr>
<td><font color="green"><s:property /></font> </td>
</tr>
</s:if>
<s:else>
<tr>
<td><font color="red"><s:property /></font></td>
</tr>
</s:else>
</s:iterator>
</table>
</s:iterator>
There is problem with the if statement i believe. Can one suggest how to reach the if part?

You can't nest Struts Tags.
Change this
<s:if test="%{<s:property />.indexOf(#string1)) == -1}">
to this
<s:if test="%{top.indexOf(#string1) == -1}">

Related

Join two Map on Jsp page - Struts2 application

I am working on Struts2 Application;
my displaying objects are Map objects:
Map<String, HashMap<String, List<PlanDAO>>> channelRoomTypePlanmap =
new HashMap<String,HashMap<String,List<PlanDAO>>>();
Map<String, List<RateInventoryDAO>> rateInventoryMap =
new HashMap<String, List<RateInventoryDAO>>();
Here, I have two objects, where rateInventoryMap key is dependent on channelRoomTypePlanmap value combination.
Like when I print first value of channelRoomTypePlanmap I want to create key using combination of its value, and find it in rateInventoryMap , if its true then it'll print its value.
<div>
<table id="mainTable" >
<tr>
<td>
<s:iterator value="channelRoomTypePlanmap">
<table border="1">
<tr>
<td><s:property value="key" />
</td>
<td>
<table>
<s:iterator value="value">
<tr>
<td><s:property value="key" />
</td>
<td>
<table border="1">
<s:iterator value="value">
<tr>
<td><s:property value="planName" />
</td>
<s:iterator value="rateInventoryMap">
<s:iterator value="value">
<td><s:property value="currentRate" />
</td>
</s:iterator>
</s:iterator>
</tr>
</s:iterator>
</table>
</td>
</tr>
</s:iterator>
</table>
</td>
</tr>
</table>
</s:iterator>
</td>
</tr>
</table>
</div>
I tried this, which is showing in wrong format, means showing all data from second Map in single row for all. I've also heard that scriptlet code should be avoided.
How can I join these two Maps on JSP ?
Current View using above code_
Your model structure
new HashMap<String,HashMap<String,List<PlanDAO>>>();
should be improved; it is too noisy, you need five nested iterators in your page to do what you need, never know in which iterator you are without counting them, and, more important, even if this structure can be used to display data, it can't be used to send it back (you never know).
I strongly suggest you to read the following answer:
Struts2 Object Modelling
That said, when dealing with Collections in a JSP you must read about two great features of OGNL:
OGNL List Selection
OGNL List Projection

How to retrieve the data of <s:radio> within <s:iterator> tag into Struts2's action

I need to do something in webpage like this :
a bunch of rows that display questions and choices , the choices are made up of radio buttions.
I don't konw how to get these radio button's data into my java code.
This is my jsp code:
<s:iterator value="questionsList" status="status" var="questionsList">
<tr>
<td><s:property value="title"/></td>
</tr>
<tr>
<td>
<s:radio list="{'A','B','C','D'}" name="[%{#status.index}]answer" theme="simple"/>
</td>
</tr>
</s:iterator>
I've tried
private List answer = new ArrayList();
answer = (List)request.getAttribute("[%{#status.index}]answer");
but it doesn't work.
If you have other means to do this please tell me, thank you!
You can provide indexed property names for the lists you want to get in Java
<s:iterator value="questionsList" status="status">
<tr>
<td><s:property value="title"/></td>
</tr>
<tr>
<td>
<s:radio list="{'A','B','C','D'}" name="questionsList[%{#status.index}].answer" theme="simple"/>
</td>
</tr>
</s:iterator>
Voila! Your question list will be populated with indexes provided by status.index variable.

How to compare two strings using Struts2 tags and OGNL?

I am trying to compare two values : one from session and anther from iterator
<s:iterator value="themes" status="currentRecord">
<s:if test="%{usertheme}) == %{themeName}">
<td align="center" bgcolor="red">
</s:if>
<s:else>
<td align="center" bgcolor="green">
</s:else>
</s:iterator>
But I am unable to compare my values, please can you tell me where I am doing mistakes ?
%{} should be put (if necessary) around all the statement, not in the middle.
For Strings you should use .equals, .equalsIgnoreCase, .contains, .indexOf etc... Not ==.
Change to this:
<s:iterator value="themes" status="currentRecord">
<s:if test="%{#session.usertheme.equalsIgnoreCase(themeName)}">
<td align="center" bgcolor="red">
</s:if>
<s:else>
<td align="center" bgcolor="yellow">
</s:else>
....
this works too:
<s:if test="#session.usertheme.equalsIgnoreCase(themeName)">
(Not an answer, but two suggestions, and I needed formatting; Andrea's answer is correct.)
For the sanity of yourself and those that follow, turn that chunk of JSP into a single line:
<s:iterator value="themes">
<tr>
<s:set var="currTheme" value="%{userTheme == themeName ? 'red' : 'green'}"/>
<td bgcolor="${currTheme}">Cell content</td>
</tr>
</s:iterator>
Consider using theme-named CSS instead of inline CSS and avoid it completely, roughly:
td.theme1 {
background-color: red;
}
td.theme2 {
background-color: green;
}
td.theme3 {
background-color: #daa520;
}
(Assuming themes named "theme1", "theme2", "theme3", but that's not relevant.)
<table class="themed-table">
<s:iterator value="themes">
<tr>
<td class="${themeName}">Cell content</td>
</tr>
</s:iterator>
</table>
It'd be nicer to move the style info "up" a level, e.g., table.theme1 td, but you get the idea. Doing so allows a lot of flexibility in where the theme info comes from and so on.
<!--name attribute inside select tag must be a variable in action class with getter/setter -->
<!-- test variable sets the value of selected item in action class -->
<select name="test">
<!-- name attribute could be anything you want but value attribute must be a model class variable-->
<s:set name="lead_string_LS_ID" value="MasterDataModel.string_LS_ID" />
<!-- value attribute must be a list to iterate, status (an instanceof IteratorStatus will be pushed into stack upon each iteration)or result -->
<!-- var Name used to reference the value pushed into the Value Stack (my list contain leadSource_String_Id)-->
<s:iterator value="leadSource_list" status="result" var="leadSource_String_Id">
<!--#lead_string_LS_ID is value taken from <set> tag above. Note # must be in-front of the name
leadSource_String_Id is element specified in var of <iterator> tag
-->
<s:if test='(#lead_string_LS_ID.equals(leadSource_String_Id))'>
<option value="<s:property value='leadSource_String_Id'/>" selected="selected">
<s:property value="leadSource_String_Name" />
</option>
</s:if>
<s:else>
<option
value="<s:property value='leadSource_String_Id'/>">
<s:property value="leadSource_String_Name" />
</option>
</s:else>
</s:iterator>
</select>

How arrange data from an arraylist in two divs using struts2

I have a set of images in an ArayList. I am trying to arrange these images in rows, with each row containing 4 images. So if there are 10 images in the arraylist, there should be 3 rows with 4 in first, 4 in second and 2 in third row.
Here is my jsp.
<s:iterator value="productList" status="status">
<div class="display">
<div class="block">
<img src="../product/image?imageID=<s:property value="productID"/>&type=thumbnail" />
</div>
</div>
</s:iterator>
Here div with class display acts as the rows.
div with the class block acts as the columns inside the display div.
How can it be done? Any help will be appreciated
There are two approachs that i can think of..
1. Table(easiest)
2. ul li
In the table approach the code will look something like
<table>
<s:iterator value="productList" status="status">
<s:if test="#status.index %4 == 0">
<tr>
</s:if>
<td>
<img src="../product/image?imageID=<s:property value="productID"/>&type=thumbnail" />
</td>
<s:if test="#status.index %4 == 0">
</tr>
</s:if>
</s:iterator>
<table>
Also you might want to check after the iterator if the tag has to be closed

How to use if clause inside a netui data repeater

I'm looking for method to inject value into a <c:if test>. The code below works if I substitute
<% out.print(request.getSession().getAttribute("UserName").toString()); %>
with some constants. But how can I parse a value in <%%> tag into a <c:if test>?
<netui-data:repeater dataSource="pageFlow.availableBooks">
<tr>
<td><netui:label value="${container.item.bookName}" /></td>
<td>
<c:if test="${container.item.createdBy == '<% out.print(request.getSession().getAttribute("UserName").toString()); %>'}">
<netui:anchor action="removeBookAction" value="Remove" formSubmit="true">
<netui:parameter name="bookID" value="${container.item.bookID}" />
</netui:anchor>
</c:if>
</td>
</tr>
</netui-data:repeater>
Thanks in advance!
You can use sessionScope to read the value from session scope. The following will help you to resolve:
<c:if test="${container.item.createdBy == sessionScope.UserName}">

Categories