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

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.

Related

adding an icon menu from another jsp file as a column to a datatable in liferay

There are 2 tables here:
The table below is using purely container to display populate the tables from database whereas the one above is using datatable.
However i wish to duplicate the last column of displaying a icon menu for edit and delete actions which is contained inside actions.jsp inside the table above.
This is my partial code in view.jsp displaying the datatables.
<%
List<Guestbook> guestbookList = GuestbookLocalServiceUtil.getGuestbooks(scopeGroupId);
request.setAttribute("guestbookList", guestbookList);
%>
<table id="gbdb" class="table table-bordered table-striped" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<c:forEach items="${guestbookList}" var="guestbook">
<tr>
<td>${guestbook.name}</td>
<td>${guestbook.status}</td>
<td><jsp:include page="${actions.jsp}" flush="true" /></td>
</tr>
</c:forEach>
</tbody>
</table>
However as you can see my function of diplaying actions.jsp is not similar to the method for using container where i can just referenced the path using
<td> <liferay-ui:search-container-column-jsp
align="right"
path="/guestbookadminportlet/guestbook_actions.jsp" /></td>
Please help me with the correct way of displaying by referencing to actions.jsp
You should not include whole jsp as an action column if using jQuery data-table,
Let's assume you need to add Delete book action so your td should be replace by following code.
<td>
<portlet:actionURL name="<%=DELETE_BOOK%>" var="deleteBookUrl">
<portlet:param name="bookId" value="${book.id}"/>
</portlet:actionURL>
<input type="button" onclick="location.href='${deleteBookUrl}'" value="Delete"/>
</td>
Hope this will help you.

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 edit fields of a table?

I have a table to show a long list of items, I am wondering how I can edit the fields and submit the form to update them?
<form name="edit" method="POST" action="edit">
<table border="4">
<tbody>
<c:forEach items="${basket.items}" var="item">
<tr>
<td>
<input name="item.id" value="${item.id}"/>
</td>
<td>
<input label="Price" value="${item.product.price}"/>
<br/>
</td>
</tr>
</c:forEach>
</tbody>
</table>
this is a new one
<input id="edit" type="submit" name="edit" value="Edit"/>
</form>
You are using Struts2, with JSTL and EL instead of Struts Tags and OGNL... is there a particular reason that forces you to drop most of the framework mechanics ?
That said, your inputs aren't valid (no type specified) and the "this is a new one" sentence in the HTML seems to indicate the willing to insert a new row, instead of editing the existing entres. Your description and your code seem to ask two different things... to insert a new one, just make a call to another method of the action (or another action) called "add" instead of "edit", sending one single element and adding it to the collection. No need to use AJAX here...
If instead, the question is really:
how I can edit the fields and submit the form to update them ?
this is the way:
<s:form method="POST" action="edit">
<table border="4">
<tbody>
<s:iterator value="basket.items" var="item" status="ctr">
<tr>
<td>
<s:textfield name="item[%{#ctr.index}].id" />
</td>
<td>
<s:textfield name="item[%{#ctr.index}].product.price" />
</td>
</tr>
</s:iterator>
</tbody>
</table>
<s:submit value="Edit"/>
</form>
I would suggest you make an AJAX call using jquery to update the new one. And then in the success handler you can append the new line to the existing table. Before doing that you need to give your table proper ids so that its easier to use JQUERY.
var newLine = document.createElement("tr");
var cellName = document.createElement("td");
$(cellName).text("itemId");
$(newLine).append(cellName);
// similarly create other td's
$("#modelTable").append(newLine);// replace modelTable by the id of your table

Spring MVC - Submit an Object from JSP

I have a JSP that presents a list of customers (ArrayList searchResults). I want to be able to pick one of those, and submit it to a Spring MVC controller. However, it appears that I cannot pass the selected object, only a property of it, such as customerId. I really need to pass the entire object.
Is there a standard way to do this in Spring 3.x?
<c:forEach items="${searchResults}" var="searchResult">
<tr>
<td><c:out value="${searchResult.customerId}" /></td>
<td><c:out value="${searchResult.firstName}" /></td>
<td><c:out value="${searchResult.lastName}" /></td>
<td>
<form method="POST" ACTION="./customercare">
<input type="SUBMIT" value="Select This Customer"/>
<input type="hidden" name ="searchResult" value="${searchResult}"/>
</form>
</td>
</tr>
</c:forEach>
You can use Spring's form taglib instead of plain <form> to post back to a Spring MVC Controller and then it will Bind the values back to the model you specify.
<form:form method="post" action="addContact.html">
<table>
<tr>
<td><form:label path="firstname">First Name</form:label></td>
<td><form:input path="firstname" /></td>
</tr>
...
#RequestMapping(value = "/addContact", method = RequestMethod.POST)
public String addContact(#ModelAttribute("contact")
Contact contact, BindingResult result) {
See this Post: http://viralpatel.net/blogs/spring-3-mvc-handling-forms/
You might want to consider giving id to each and nested tags to differentiate between row that you want to POST
<c:forEach items="${searchResults}" var="searchResult">
<tr>
....
<form:form id="${searchResults.key}-form" method="POST" ACTION="./customercare">
<form:input id="${searchResults.key}-btn" type="SUBMIT" value="Select This Customer"/>
<form:input id="${searchResults.key}-hidden" type="hidden" name ="${searchResults.key}" value="searchResult['${searchResults.key}']"/>
</form:form>
</tr>
On the backend side you will have to write the controller as suggested by #PatBurke
You could have 1 form per customer with a lot of hidden inputs in. When that customer is selected you can POST that form. Spring can then bind all the hidden inputs to your customer object.
(Generally, I would just send the id only and load the customer info, as an entity, from a Database. However, I assume you must have a good reason for not wanting to do this)

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