Using object as key in map (treemap) in spring - spring

I've tried to display on JSP page objects' properties from Map in FormBean. Map is defined as
Map<KeyObject, ValueObject> m
KeyObject has two properties
public class KeyObject implements Comparable<KeyObject> {
private Integer a;
private Integer b;
getters/setters/and rest basic methods
}
On JSP I want to obtain something like code below:
<c:forEach items="${formBean.m}" item="itm">
...
<form:input path="m[itm.key].propertyName" />
...
</c:forEach>
I need to:
display elements in proper order
submit objects to map
So is there any simple solution or I should do some "magic"?
Thanks for your time.
Stefan
Some more information. Each object will have other "view" so I try to use c:import
<c:forEach items="${formBean.m}" item="itm">
<c:import url=${itm.value.name}Page.jsp" />
</c:forEach>
and on ...Page.jsp I want to use form's inputs.

<c:forEach items="${formBean.m}" varStatus="itm">
<tr>
<td>${itm.key.propertyName}</td>
<td>${itm.value.propertyName}</td> <!--which is same as below ... -->
<td>${formBean.m[itm.key].propertyName}</td>
</tr>
</c:forEach>
You can iterate through the maps keys and values, like above, and output different fields as required.

Related

How to retrieve List content in JSP from Hibernate Query Language

I Developed the program that retrieve the content from Database and store it in the List
List<Student> studentList=session.createQuery("from Student s where s.lastName=:lastName and s.firstName=:firstName")
.setParameter("lastName",lname)
.setParameter("firstName",fname).list();
for(Student studentinfo:studentList)
{
System.out.println(studentinfo);
}
model.addAttribute("result",studentList);
And i have passed the result to the JSP File
${result}
<c:forEach var="item" items="${result}">
${item}
</c:forEach>
I am getting the result like this
"Student{id=6, firstName='arul', lastName='suju', address='s;lda'}"
But i want print the value one by one,Can any one suggest better code in JSP
I got the solution by modifying the jsp code like this
<c:forEach var="item" items="${result}">
${item.id}
${item.firstName}
${item.lastName}
${item.address}
</c:forEach>

java springframework <select> tag

I am struggling with "SELECT/OPTIONS" tag. I have 2 ArrayList<<String>>: "pNames" and "pIds". Form should display "pNames", while the values returned to controller should be "pIds". From my spring controller I am passing the following 2 ArrayLists. How do I implement this in Spring MVC?
ArrayList<String> pIds = pps.getPIds();
ArrayList<String> pNames = pps.getPNames();
model.addAttribute("pIds", pIds);
model.addAttribute("pNames", pNames);
<form:select id="pps" name="pps" path="pIds" multiple="multiple">
<form:options items="${pIds}" itemValue="${pNames}" itemLabel="${pNames}"/>
</form:select>
Above code is not working.
You need to convert your Lists to a Map<String, String>. Then you can add the map to your model and you only need to do:
<form:select path="pIds">
<form:options items="${mapName}" />
</form:select>
For more information, please checkout this question: Use <form:select> tag with a map

Simple JSP-Spring 3 dropdown list not working

I know this should be pretty easy but I'm stuck after trying several things.
I'm only trying to display in my jsp a basic dropdown list. Spring version is 3 so I want everything to work with annotations.
JSP form with dropdown list:
<form:form method="post" commandName="countryForm">
<table>
<tr>
<td>Country :</td>
<td><form:select path="country">
<form:option value="Select" label="Select" />
</form:select>
</td>
<tr>
<td colspan="3"><input type="submit" /></td>
</tr>
</table>
</form:form>
CountryForm.java is a plain object with a single String attribute "country", with its getters and setters.
Controller who deals with the GET request is the following:
#Controller
public class CountryFormController {
#RequestMapping(value = "MainView", method = RequestMethod.GET)
public String showForm(Map model) {
CountryForm cform = new CountryForm();
model.put("countryForm", cform);
return "MainView";
}
}
However, when I redirect to the JSP "MainView" I get the typical error:
org.apache.jasper.JasperException: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'countryForm' available as request attribute
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:502)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:424)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
What am I doing wrong?
The select tag in the Spring TagLib needs to be provided with a collection, map or array of options. I'm not sure what you would like these to be so I will make some assumptions.
You need to include a collection, map or array of objects in your controller. Ideally you would have a Country class and create new instances for a set of countries. For the example to work with your code, I just created a static list of countries. Add the list to your model and then modify the select tag, setting the options to ${countries}. Assuming country is a field of type String on CountryForm with appropriate get/set methods, the country should data-bind to field when the form is submitted.
Controller
#Controller
public class CountryFormController {
#RequestMapping(value = "MainView", method = RequestMethod.GET)
public String showForm(Map model) {
List<CountryForm> cfs = new ArrayList<CountryForm>();
cfs.add("United States");
cfs.add("Canada");
model.put("countries", cfs);
model.put("countryForm", cform);
return "MainView";
}
}
JSP
<form:select path="countryForm.country" options="${countries}"/>
I have sample code at GitHub, try it an let me know. Look at landing.jsp and UserController
<form:select path="users[${status.index}].type" >
<form:option value="NONE" label="--- Select ---"/>
<form:options itemValue="name" itemLabel="description" />
</form:select>
HTH

Stripes Checkboxes and Textfields

All -
I'm using stripes to do some form input for a problem I'm working on and I'm stuck on how best to submit a a pair of data using stripes and checkboxes.. for example my page looks like the following:
I have a list of options where users can enable a selection by clicking the box, and also supply some input for that item by entering data into the text field next to it:
<tr>
<td><stripes:checkbox name="item.enable" value="${item.id}"/></td>
<td><stripes:text name="item.value" value="${item.value}"/></td>
</tr>
.....
next item...
When the form is submitted I'd expect my Collection<Item> to be populated yet that's not the case..
How can I best submit a pair of items using the check box fields.
Thanks in advance.
..Chris
Read the documentation on indexed properties. You need to tell Stripes that you have multiple items, by naming them items[0], items[1], etc.:
<tr>
<td><stripes:checkbox name="items[0].enable" value="${item.id}"/></td>
<td><stripes:text name="items[0].value" value="${item.value}"/></td>
</tr>
<tr>
<td><stripes:checkbox name="items[1].enable" value="${item.id}"/></td>
<td><stripes:text name="items[1].value" value="${item.value}"/></td>
</tr>
This supposes that you action bean has a setItems(List<Item> items) method, that the Item class has a public no-arg constructor, and has a setEnable(String itemId) and a setValue(String value) method.
I would wrap this in a JSTL 'forEach' tag and I would put the items in a List. Similar to what JB Nizet said, you also need public setters in the action bean. If you are using Collection<Item> with some implementation other than List<Item> the below snippet won't work.
<c:forEach var='itemIndex' begin='0' end='2'>
<c:set scope='page' var='item' value='${items[itemIndex]}'>
<tr>
<td><stripes:checkbox name="items[${itemIndex}].enable" value="${item.id}"/></td>
<td><stripes:text name="items[${itemIndex}].value" value="${item.value}"/></td>
</tr>
</c:forEach>
There is another case when you don't want the list to default to 3 items. The one I'm thinking of is when the list is already populated. If that is the case I would change the 'end' attribute of the <c:forEach> to be: ${fn:length(actionBean.items) == 0 ? 3 : fn:length(actionBean.items)-1}

SpringMVC - JSP Iteration Issue

I have the following class that I am attempting to use as a command object
public class Member {
private String datePeriod;
private String status;
private ArrayList <Project> projectList;
}
On the JSP, I would like the form to prefill with a pre-existing values.
<c:forEach items="${member.projectList}" var="project">
<tr>
<td><form:input path="**<var???>**" value="${project.projectEntry.projectDesc}" /></td>
</tr>
</c:forEach>
I am making an error with path which is causing an error in jsp. Does anyone know the proper syntax with regards to each iteration? Thanks.
My Spring MVC is bit rusty but if I remember correctly, path gets translated as the input name property in HTML eventually. So you can put any Label value in there and that should work.
<form:input path="ProjectDescription" value="${project.projectEntry.projectDesc}" type="text" />
This should get translated to:
<input name="ProjectDescription" type="text" value="<whatever_you_have_in_backing_bean>"/>
Do you want to look up name from a backing bean instead? If so you should be able to do something like below. Without knowing your data structure I am assuming it to be status.
<form:input path="member.status" value="${project.projectEntry.projectDesc}" type="text" />
More on the form tag here.

Resources