Display spring model list as simple fields - java

It is possible to bind a List of String (List) and display them in a jsp in a combo box like this:
<form:select path="countryId">
<form:option value="" label="Please Select"></form:option>
<form:options items="${countryList}" itemValue="countryId" itemLabel="countryName"/>
</form:select>
I want this list to display in <td> or <form:input> like fields, not in combo box.
I am binding String list in model as
Map referenceData = new HashMap();
referenceData.put("OutputsList", Outputs);
In JSP I use
<c:forEach var="OutputsList" items="${Outputs}">
${OutputsList}
</c:forEach>
But list is not printed. What could be the reason?

do it that way.
<c:forEach var="country" items="${countryList}">
<tr>
<td>${country.countryId}</td>
<td>${country.countryName}</td>
</tr>
</c:forEach>
and on the server side use ModelAndView object
List<Country> countryList;
ModelAndView mv = new ModelAndView("index");
mv.addObject("country",countryList);

There was a wrong approach while using it in a jsp. From the code in question just swap OutputsList
Map referenceData = new HashMap();
referenceData.put("OutputsList", Outputs);
In JSP I use
<c:forEach var="item" items="${OutputsList}">
${item}
</c:forEach>
It will work.

Related

Outputting Map<String, Map<String,List<String>>> in jsp using jstl

I've data in the below format.
Map<String, Map<String,List<String>>>
example - {ProfileAdaptarRepository={active=[true,false,true,true]}}}
I did set page context in mymap in the below scriptlets and it works absolutely fine.
<% pageContext.setAttribute("mymap",example);%>
I need data in the below html tabular format:
Sample HTML source code for the above image.
<html>
<body>
<table border="1">
<tr>
<th>Component</th>
<th>Properties</th>
<th>J01</th>
<th>J02</th>
<th>W01</th>
<th>W02</th>
</tr>
<tr>
<td>StoreConfiguration</td>
<td>active</td>
<td>true</td>
<td>false</td>
<td>true</td>
<td>false</td>
</tr>
<tr>
<td>DynamoConfiguration</td>
<td>enabled</td>
<td>true</td>
<td>false</td>
<td>true</td>
<td>false</td>
</tr>
</table>
</body>
</html>
I tried to present the above data in webpage using the below jstl code, but it didn't work for plural component values (more than 1 components)
<c:forEach var = "comp" items="${mymap}">
<tr>
<td>${comp.key}</td>
<c:forEach var="prop" items="${comp.value}">
<td>${prop.key}</td>
<c:forEach var="val" items="${prop.value}">
<td>${val}</td>
</c:forEach>
</c:forEach>
</tr>
</c:forEach>
Can someone please help or guide me in achieving the desired tabular format?
Many Thanks in advance..:)
I was able to run your code with multiple components. I created a data structure like yours with
List<String> myListStore = new ArrayList<>();
List<String> myListDynamo = new ArrayList<>();
List<String> myListProfile = new ArrayList<>();
Map<String, List<String>> myMapStore = new HashMap<>();
Map<String, List<String>> myMapDynamo = new HashMap<>();
Map<String, List<String>> myMapProfile = new HashMap<>();
Map<String, Map<String, List<String>>> myBigMap = new HashMap<>();
myListDynamo.add("true");
myListDynamo.add("false");
myListDynamo.add("true");
myListDynamo.add("false");
myMapDynamo.put("enabled", myListDynamo);
myBigMap.put("Dynamo", myMapDynamo);
myListStore.add("true");
myListStore.add("false");
myListStore.add("true");
myListStore.add("true");
myMapStore.put("disabled", myListStore);
myBigMap.put("Store", myMapStore);
myListProfile.add("true");
myListProfile.add("false");
myListProfile.add("true");
myListProfile.add("true");
myMapProfile.put("disabled", myListProfile);
myBigMap.put("Profile", myMapProfile);
request.setAttribute("mymap", myBigMap);
I used your exact jstl code and was able to generate the table. Are you sure that your data is in the correct format in the data structure?

Unable to catching multiple table rows with JSTL forEach in Spring MVC

The goal: adding multiple table rows dynamically based on user inputs and catch the data through controller.
What I have so far(all simplified):
POJO:
public class Item(){
String price;
String weight;
getters and setters...
}
public class ItemForm(){
List<Item> items;
getter and setter...
}
JSP:
<form:form action="/create" method="POST" modelAttribute="itemForm">
<table>
<tr>
<td><input type='text' name='price'/></td>
<td><input type='text' name='weight'/></td>
</tr>
</table>
<c:forEach items="${itemForm.items}" var="item" varStatus="status">
<tr>
<td align="center">${status.count}</td>
<td><input name="items[${status.index}].price" value="${item.price}" /></td>
<td><input name="items[${status.index}].weight" value="${item.weight}" /></td>
</tr>
</c:forEach>
</form:form>
Controller:
private List<Item> items = new ArrayList<>();
#RequestMapping(value = "/create", method = RequestMethod.POST)
public String saveMultipleRows(#ModelAttribute("itemForm") ItemForm itemForm) {
items = itemForm.getItems();
if(items != null && items.size() > 0){
System.out.println("The list is not null!");
}
System.out.println("didn't get into the if statement");
return null;
}
I skipped the Javascript on adding table rows, if you think that have anything to do with this question, I will update my post and put the Javascript code.
The idea is to create a ItemForm class that contains a list of Item object, and in the JSP using JSTL c:foreach to save all the data from users to the list. And in my controller, if the list is not empty, I simply want to print out a message so that I know the list is not empty. But now if I run the program, it prints out "didn't get into the if statement".
So the problem I am currently having is the list is empty, that means I am not able to save the user input data to the list. Can anyone help me and let me know where I did wrong?
Below is the corrected code
since you have item in scope you can directly access the props of Item,You dont need to explicitly declare the index
<input name="${item.price}" value="${item.price}" />
<input name ="${item.weight}" value="${item.weight}" />

How to generate table dynamically from the Map of String and List<String> using JSP and Servlet?

I have my response Object as which contains getter and setters for the Map -
public class DataResponse {
private Map<String, List<String>> attributes = new LinkedHashMap<String, List<String>>();
public Map<String, List<String>> getAttributes() {
return attributes;
}
public void setAttributes(Map<String, List<String>> attributes) {
this.attributes = attributes;
}
}
In the above object, I have a Map of String and List<String>. In the map, keys are my table header and the value in the map is the table data for that header.
Suppose if this is the value in the map -
FirstName is the Key in the same Map
DAVID, RON, HELLO are the values in the map as the List for that key.
Similarly,
LastName is the Key in the same Map
JOHN, PETER, TOM are the values in the map as the List for the `LastName` key.
Then my Table should look like this
FirstName LastName
David JOHN
RON PETER
HELLO TOM
I need to generate my above table dynamically as am passing my dataResponse object to my JSP page as mentioned below -
DataResponse dataResponse = some_code_here;
req.setAttribute("data", dataResponse);
WebUtil.forward(req, resp, this, "/admin/test.jsp");
And below is my table in JSP in which I am using my above object to generate the table in the above format
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="1" style="text-align: center;">
<TR>
<c:forEach var="h" items="${data.attributes}">
<TH>${h.key}</TH>
</c:forEach>
</TR>
//iterate again
<c:forEach var="h" items="${data.attributes}">
//h.value is ArrayList so we can iterate with c:forEach
<c:forEach var="headers" items="${h.value}">
<TR>
<TD>${headers}</TD>
</TR>
</c:forEach>
</c:forEach>
</TABLE>
But somehow my tables are not getting shown in the way I am trying to show in my above example. All the keys are getting shown properly in the Table Headers but all the values are getting shown only in first column.
And size of the list will be same for all the keys.
Any thought how this can be done?
UPDATE:-
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="1" style="text-align: center;">
<TR>
<c:forEach var="h" items="${data.attributes}">
<TH>${h.key}</TH>
</c:forEach>
</TR>
//iterate again
<c:forEach var="h" items="${data.attributes}">
<TR>
<c:forEach var="headers" items="${h.value}">
<TD>${headers}</TD>
</c:forEach>
</TR>
</c:forEach>
</TABLE>
This gives me -
FirstName LastName
David RON HELLO
JOHN PETER TOM
<%
// Create an ArrayList with test data
ArrayList list = new ArrayList();
Map person1 = new HashMap();
person1.put("name", "A");
person1.put("lastname", "A1";
list.add(person1);
Map person2 = new HashMap();
person2.put("name", "B");
person2.put("lastname", "B1");
list.add(person2);
Map person3 = new HashMap();
person3.put("name", "C");
person3.put("lastname", "");
list.add(person3);
pageContext.setAttribute("persons", list);
%>
<html>
<head>
<title>Search result: persons</title>
</head>
<body bgcolor="white">
Here are all persons matching your search critera:
<table>
<TH>Name</th>
<TH>Id</th>
<c:forEach items="${persons}" var="current">
<tr>
<td><c:out value="${current.name}" /><td>
<td><c:out value="${current.lastname}" /><td>
</tr>
</c:forEach>
</table>
This approach is easier and suggested.
If u still want to stick with your code, you can use jsp tags instead of jstl tags to achieve your goal as follow
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="1" style="text-align: center;">
<TR>
<c:forEach var="h" items="${data.attributes}">
<TH>${h.key}</TH>
</c:forEach>
</TR>
//use jsp scriplets to acces both list simultaneoulsy
<% List data= request.getAttribute("data")==null?null:(List) request.getAttribute("data");
List Names=data.get(0);
List LastNames=data.get(1);
for(int i=0;i<Names.length();i++){ %>
<td><%=Names.get(i)%></td><td><%=LastNames.get(i)%></td>
<%
}
%>

get map from jsp-form spring

I am new in Spring MVC, and I have a problem.
I'm sending to FORM LinkedHashMap, and it's showing great.
model.addAttribute("resultForm", resultForm);
Part of my jsp:
<c:forEach items="${resultForm}" var="resultMap" varStatus="status">
<tr id="tableRow" class="table-row">
<td>
${resultMap.key}
</td>
<td>
<select name="resultMap['${resultMap.key}']" class="espa-config-select">
<option selected value ="${resultMap.value}">${resultMap.value}</option>
<c:forEach items="${mainParams}" var="item2">
<c:if test="${item2.key == resultMap.key}">
<c:forEach items="${item2.value}" var = "q">
<c:if test="${resultMap.value != q}">
<option value="${q}"> ${q} </option>
</c:if>
</c:forEach>
</c:if>
</c:forEach>
</select>
</td>
</tr>
</c:forEach>
Now I need to get it back
Here is part of Controller
#RequestMapping( value = "espa/update", method = RequestMethod.POST )
public String save(#ModelAttribute("resultForm") LinkedHashMap<String,String> resultForm) {
System.out.println("resultMap post "+resultForm.toString());
if (resultForm!=null){
//resultForm.put("Port", port);
espaService.setConfiguration(selectedDevice, resultForm);
LOG.debug("Saving configuration: {} /nPort{}",resultForm, selectedDevice);
}
return "redirect:/espa";
}
But it is empty!
How can I fix it?
In your select you are using the name "resultMap". The name attribute needs to correlate to the Model Attribute, which you are called "resultForm".

Key and Value has ArrayList in Map by Using JSTL

i have a
Map<ArrayList<String>, ArrayList<String> myMap = new HashMap<ArrayList<String>,ArrayList<String>>();
List<String> list1 = new ArrayList<String>();
list1.add("Administrator");
list1.add("Lookup Configuration");
List<String> list2 = new ArrayList<String>();
list2.add("User Creation");
list2.add("Branch Creation");
list2.add("Country");
list2.add("Language"):
the above is dummy data, i am creating menu management like this
Administrator (MenuName)
--User Creation (item1)
--Branch Creation (item2)
Lookup Creation (MenuName)
--Country (item1)
--Currency (item2)
i am writing jstl like this
Map,ArrayList> myMap = new LinkedHashMap,ArrayList>();
and i am doing like this
<c:forEach items="${mainMenu}" var="myMenu">
<c:forEach items="${myMenu.key}" var="menuName" varStatus="loop">
<li id="lookup" class="mail">${menuName}<span>26</span>
<ul class="sub-menu">
<c:forEach items="${myMenu.value}" var="items" varStatus="loop">
<li><em>02</em>${items.itemName}<span>14</span></li>
</c:forEach>
</ul>
</li>
</c:forEach>
</c:forEach>
i am getting key perfect, but i am struck the value
and the values are not iterating the realted key
any help would be apreciated
Regards
Pradeep
I am not sure, but maybe you want something like this:
<%
Map<String, List<String>> myMap = new LinkedHashMap<String,List<String>>();
request.setAttribute("mainMenu", myMap);
List<String> adminItemsList = new ArrayList<String>();
adminItemsList.add("User Creation");
adminItemsList.add("Branch Creation");
List<String> lookupItemsList = new ArrayList<String>();
lookupItemsList.add("Country");
lookupItemsList.add("Language");
myMap.put("Administrator", adminItemsList);
myMap.put("Lookup Configuration", lookupItemsList);
%>
<c:forEach items="${mainMenu}" var="myMenu">
<li id="lookup" class="mail">${myMenu.key}<span>26</span>
<ul class="sub-menu">
<c:forEach items="${myMenu.value}" var="item" varStatus="loop">
<li><em>02</em>_${item}_<span>14</span></li>
</c:forEach>
</ul>
</li>
</c:forEach>
out:
Administrator26
02_User Creation_14
02_Branch Creation_14
Lookup Configuration26
02_Country_14
02_Language_14
I used LinkedHashMap to remember order of keys i putted in Map.
Are you using the correct data structure?
Do you mean:
Administrator
--User Creation
--Branch Creation
Lookup Configuration
--Country
--Currency
Wouldn't this be a map of lists? : Map<String,List<String>>
Then you could have a nested loop where you iterate over each key and then over that key's values.
its really helpfull your both posts, thank you very very much
i got the solution on both of your answers
here it is
my map is Map<String,List<String> myMenu = new HashMap<String, List<String>>();
<c:forEach items="${myMenu}" var="menuName" varStatus="loop">
<li id="lookup" class="mail">${menuName.key}
<ul class="sub-menu">
<c:forEach items="${menuName.value}" var="item" varStatus="loop">
<li>${item.itemName}</li>
</c:forEach>
</ul>
</li>
</c:forEach>

Categories

Resources