Join two Map on Jsp page - Struts2 application - java

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

Related

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.

If statement inside TreeMap in Struts 2

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}">

Unable to print from a DB using <logic:iterate> and <logic:bean>

I am working on a small Struts 1.2.4 Phonebook app.
The app contains forms in a single JSP to add , search , and edit/delete the data. My problem lies in displaying the ResultSet which is Obtained on performing a wildcard search. Here's the flow of the program :
User enters a search string.
Request is passed to a DispatchAction class's search() method , which in turn calls a searchContact(String name , HttpServletRequest request) method of a 'model' class.
The method executes SELECT query on the DB using a PreparedStatement, which returns a resultset, the values of which are put in an ArrayList< PhoneBook >. The class PhoneBook is a JavaBean with two fields, 'c_name' and 'p_num' , with getters and setters.
logic:bean and logic:iterate tags are used to print the contents of the ArrayList< PhoneBook > in the JSP.
In the JSP :
<html:form action="/PhoneBookAction.do" method="post" >
<table>
<tr>
<td>Name</td>
<td><html:text property="name"/></td>
</tr>
</table>
<br>
<html:submit property="method" value="Search"/>
<br><br><br>
<table align="center" border="2" bordercolor="royal blue">
<thead>
<tr>
<td><b> Select </b></td>
<td><b> Name </b></td>
<td><b> Phone Number </b></td>
</tr>
</thead>
<logic:iterate id="search_resId" name="phonebookform" property="search_res">
<tr>
<td> <bean:write name="search_resId" property="c_name" /> </td>
<td> <bean:write name="search_resId" property="p_num" /> </td>
</tr>
</logic:iterate>
</table>
</html:form>
This usually gives a "Bean not found in any scope" error, or prints nothing. I tried the fixes suggested in some similar threads, But none of them seem to work. Looking forward to a solution. Thank you.

JSTL: for Each loop to iterate over collection

following is my snippet, which was working good untill I migrated from spring 2 to spring 3 and Jstl 1.1 to jstl 1.2. Now, its not working and keep on giving the error
fEvents cannot found on object location
<c:forEach items="${location.fEvents}" var="item" varStatus="loop">
<tr><td><form:input path="fEvents[${loop.index}].hostName" size="30" maxlength="200"/></td>
<td><form:input path="fEvents[${loop.index}].directory" size="30" maxlength="200"/></td>
<td><form:input path="fEvents[${loop.index}].userName" size="20" maxlength="20"/></td>
<td><form:input path="fEvents[${loop.index}].password" size="20" maxlength="20"/></td>
</tr>
</c:forEach>
need to iterate the ftpEvents and show them on jsp
Any suggestion is appreciated!!!
It looks like the object called "location" does not have an fEvents property. Is it actually called ftpEvents? Do you need to just change the variable name?
Even with that, though, you'll probably want to do something more like this:
<c:forEach items="${location.ftpEvents}" var="item">
<tr><td><form:input path="item.hostName" size="30" maxlength="200"/></td>
<td><form:input path="item.directory" size="30" maxlength="200"/></td>
<td><form:input path="item.userName" size="20" maxlength="20"/></td>
<td><form:input path="item.password" size="20" maxlength="20"/></td>
</tr>
</c:forEach>
...you don't need to even use the loop.index at all, if I'm interpreting your code correctly.

Spring MVC Error Messages

Spring MVC Error Messages
Hello, Spring Fellows,
I have a form that is validated by the Spring Validation once submitted. Each field on the form may contain multiple errors messages if validation fails, so error messages are displayed below the field, not next to it. Here's the code snippet.
<tr>
<td><form:input path="name" /></td>
</tr>
<tr>
<td>
<form:errors path="name*" />
</td>
</tr>
Note that there is a star at the end of the path value to indicate that all error messages for the name must be displayed.
As you can see, the problem is that, if there is no error message, there will be an extra row on the page that looks out of place to the user. The code above is an overly simplied version, so the actual code has a lot more stuff in it, which prevents me from moving the <form:errors> tag inside the tag containing the field.
Is there a way to find out if there is any message associated to a given path on the JSP level? Basically, I would like to do the following:
<c:if test="${what do I write here?}">
<tr>
<td>
<form:errors path="name*" />
</td>
</tr>
</c:if>
Thanks!
You can do something like this (notice that bind is from spring taglib):
<spring:bind path = "name*">
<c:if test="${status.error}">
<tr>
<td>
<form:errors path="name*" />
</td>
</tr>
</c:if>
</spring:bind>
I solved your problem by doing this:
<table>
<form:errors path="firstName">
<tr>
<td colspan="2">
<form:errors path="firstName"/>
</td>
</tr>
</form:errors>
<tr>
<td><form:label path="firstName"><spring:message code="helloworld.label.firstName"/></form:label></td>
<td><form:input path="firstName"/></td>
</tr>
</table>
errors tag body will be evaluated only if there are errors on the path.
The simplest answer is to not use tables for page layout. Utilizing div tags alleviates this problem altogether since divs are completely dimensionless if set to hidden.

Categories