I am trying to create a table to display some data I pulled out from a database into a JSP page (all part of a Struts2 application), and I don't know what I'm doing wrong here...
This is the part of my JSP page where I create the table:
<table>
<s:iterator value="table" id="row">
<tr>
<s:iterator value="row" id="cell">
<td><s:property /></td>
</s:iterator>
</tr>
</s:iterator>
</table>
I have an ArrayList<ArrayList<String>> named table in my action class, and I'm pretty sure I have it populated with the correct values. I'm sure this is some easy syntactical error, but I'm still a beginner to Struts2.
might need # character added:
do a test to see if your table value is returning anything:
<table>
<s:iterator value="%{table}" id="row">
<tr>
<s:iterator value="%{#row}" id="cell">
<td><s:property value="%{#cell}"/></td>
</s:iterator>
</tr>
</s:iterator>
</table>
I took your JSP code, and wrote a getter in an action like this, using your JSP, it worked fine. Your JSP code is fine. There appears to be something wrong with your getter method or the population of the 'table'. If you post that, maybe we can figure out what's wrong with it.
public String execute()
{
m_arrayList = new ArrayList< ArrayList< String > >();
for( int i = 0; i < 10; ++i ) {
ArrayList< String > strs = new ArrayList< String >();
for( int j = i; j < 10 + i; ++j ) {
strs.add( Integer.toString( j ) );
}
m_arrayList.add( strs );
}
return SUCCESS;
}
private ArrayList< ArrayList< String > > m_arrayList;
public ArrayList< ArrayList< String > > getTable()
{
return m_arrayList;
}
Related
I have a list of object to display in a table with wicket.
Those object are composed of a String and an Array of String.
My problem is that i don't know how to add this array into my table. And second, i have some css that I need to apply to each of my String of the array, so each of them have to be on a different div/span/li.
Can it be a good idea to concatenate all those elements and add the "div" manually ?
Thank you for your help :)
I have been to obssesed with using a datatable. By using a list in a list it work just fine !
JAVA :
ListView<Profil> listProfil = new ListView<Profil>("listProfils", profils) {
protected void populateItem(ListItem<Profil> item) {
Profil tmpProfil = item.getModelObject();
item.add(new Label("libelle", tmpProfil.getLibelle()));
item.add( new ListView("listChamps", tmpProfil.getChamps()) {
protected void populateItem(ListItem item) {
item.add(new Label("libelleChamps", item.getModel()));
}
});
}
});
And the associate HTML template :
<tr wicket:id="listProfils">
<div class="row">
<td class="col-xs-4 col-md-3 col-lg-3 text">
<span wicket:id="libelle"></span>
</td>
<td class="col-xs-7 col-md-8 col-lg-8 text" colspan="3">
<span wicket:id="listChamps">
<span wicket:id="libelleChamps"
class="label label-default badge badge-tag" >Champs</span>
</span>
</td>
</div>
</tr >
As a good practice in Wicket, to build a Table in HTML that is being fed from a List, you need the following elements:
HTML
<table wicket:id="yourWicketIdOfDataTableObject">[table]
</table>
JAVA
A POJO (pojoObject) that represents each element (o regiser) of your table
A DataProvider (dataProviderObject) class that extends from SortableDataProvider<pojoObject, String>
You need to override the iterator(), size() and model() methods according to your needs.
A List<IColumn<pojoObject,String>> columnsObject
The above object will represent the colums of your table.
You could add columns as follows:
columnsObject.add(new PropertyColumn<pojoObject,String>(new Model<String>("nameOfTheColum"),pojoObjectPropertyName))
A DefaultDataTable tableObject:
DataTable<pojoObject, String> = new DefaultDataTable("yourWicketIdOfDataTableObject", columnsObject, dataProviderObject, NumOfColumns)
The above object will represent the table.
As you might see pojoObject wraped by columnsObject and dataProviderObject, and these two will be wrapped by tableObject at the end. Your array will be accessed in the iterator() method of the dataProviderObject, because it needs to retrieve the iterator of the list; The pojoObject that represent the each element of your actual list will be necessary (in case you don't have one yet)
At the end, you only need add tableObject in their Wicket Parent, as any other Wicket Component.
public class Students()
{
private String title;
private String name;
//getters setters
}
I have a map
Map<String,List<Students>> mapList
What i want to is, i send a key to get value from map and iterate that lists.
My approach
jsp
<c:forEach items="${mapList['${title}']}" var="actualDetails">
//printing the values
</c:forEach>
You can't do EL expressions inside EL expressions.
It's already an EL expression, so it should just be:
<c:forEach items="${mapList[title]}" var="actualDetails">
Suppose ${mapList} points to a Map<String,List<Students>>, then you can use the following EL expressions
<c:forEach items="${mapList}" var="entry">
${entry.key}<br>
<c:forEach items="${entry.value}" var="studentDetails">
${studentDetails.title}<br>
${studentDetails.name}<br>
</c:forEach>
</c:forEach>
I have a list of courses depending on school code. This list of courses is added to another list, making it a list-of-lists.
Action Class:
private List<String> schoolList;
private List<String> socList;
private List<String> sobList;
private List<String> sodList;
private List<String> genlist;
private List<List<String>> courseList = new ArrayList<List<String>>();
#Override
public String execute() {
FacultyManager fm = new FacultyManager();
schoolList = fm.getColumn("school_description", "school");
genlist = fm.getCoursesBySchoolCode("GEN");
sobList = fm.getCoursesBySchoolCode("SOB");
socList = fm.getCoursesBySchoolCode("SOC");
sodList = fm.getCoursesBySchoolCode("SOD");
courseList.add(genlist);
courseList.add(sobList);
courseList.add(socList);
courseList.add(sodList);
return SUCCESS;
}
JSP:
<c:forEach var="school" items="${schoolList}" varStatus="ctr">
<ul>
<li>${school}
<ul>
<c:forEach var="course" items="${courseList}">
<li>${course}</li>
</c:forEach>
</ul>
</li>
</ul>
</c:forEach>
Output
How do I make it so the output is:
GENERAL EDUCATION
DEPARTMENT OF GENERAL EDUCATION
SCHOOL OF BUSINESS
BSBA - FINANCIAL MANAGEMENT
BSBA - MARKETING AND ADVERTISING
... and so on.
You are iterating the whole list of courses in all schools over and over again, thus repeating the same output as if you called courseList.toString() method multiple times.
You should instead iterate over a concrete list of courses in a current school, which most likely (but nowhere stated in your question) depends on the current iteration index of the outer <c:forEach> loop. This is in turn captured in the index property of the exported ctr variable (that is zero-based) of the school list iteration index.
Thus, you should iterate over a specific list of the courseList list, depending on the current school. You can do this by calling courseList.get(ctr.index), assuming you are on EL2.2+.
This all yields:
<c:forEach var="school" items="${schoolList}" varStatus="ctr">
<ul>
<li>${school}
<ul>
<c:forEach var="course" items="${courseList.get(ctr.index)}">
<li>${course}</li>
</c:forEach>
</ul>
</li>
</ul>
</c:forEach>
That said, List<List<String>> does not sound as a good model choice. For instance, if you change insertion order you'll get the wrong courses in your view. Map<String, List<String>> construct fits better your domain model as it can be used to map school name to a list of course names unambiguously.
Another option to consider seriously is the usage of objects (you are doing OOP in the end). You should turn away from plain strings and move into objects domain. To start, investigate the following class hierarchy:
public class Course {
//...
private School school;
//...
}
public class School {
//...
private List<Course> courses;
//...
}
If you are trying to iterate the list inside the list , you can achive it by
<c:forEach var="school" items="${schoolList}" varStatus="ctr">
<ul>
<li>${school}
<ul>
<c:forEach var="course" items="${school}">
<li>${course}</li>
</c:forEach>
</ul>
</li>
</ul>
</c:forEach>
You need to iterate the inner list from the list you pass it to the jsp.
Update
In the case if you are using Map<String, List<String>> , you can iterate it as
<c:forEach var="school" items="${schoolList}" varStatus="ctr">
<ul>
<li>${school.key}
<ul>
<c:forEach var="course" items="${school.value}">
<li>${course}</li>
</c:forEach>
</ul>
</li>
</ul>
</c:forEach>
You can use the key and value to get the values from the Map
I am trying to print the various values received from an database in action class to a JSP page by s:property tag. However it is showing only one value.
ACTION CLASS:
ResultSet r = st.executeQuery("select unique(emailid) from pagequery");
while (r.next()) {
uniquemail=r.getString(1);
}
JSP PAGE:
The JSP page should show all the unique email id's. But it is showing only the first one.
<td>
<s:property value="uniquemail"/>
</td>
Here
while (r.next()) {
uniquemail=r.getString(1);
you are overriding a single variable with the new value on every iteration. You need to use a List or an array, and add new elements to it, otherwise you will always have only the last iterated value stored.
public class YourAction exteds ActionSupport{
private List<String> mails = new ArrayList<String>();
/* put Getter and Setter for mails here */
public String execute(){
/* ... other stuff here ... */
ResultSet r = st.executeQuery("select unique(emailid) from pagequery");
while (r.next()) {
mails.add(r.getString(1));
}
return SUCCESS;
}
}
Then use an iterator in JSP to render your collection.
Mails:
<ul>
<s:iterator value="mails">
<li><s:property/></li>
</s:iterator>
</ul>
If uniquemail is an array you have to iterate through it like this
<s:iterator value="uniquemail">
<s:property/>
</s:iterator>
i have three logic iterates in my jsp, actually where the error occurs is when i fill the wrong data and submit its shows the error, but the list which am setting to the logic iterate is not population in the JSP, but i have the values in the list as the list not setting the logic iterate didnt work.
<TR increment="<%=row++%>" bgcolor="lightblue" id="rel<%= element2.getID()%>" style="<%= (element2.getIsRetrive()==0) ? "display:''" : "display:'none'" %>" >
<TD align="center" id="<%= element2.getID()%>">
<html:select property="companyID" styleId="ID" name="element2" indexed="true" >
<html:options collection="IDList" property="value"/>
</html:select>
<TD align="center">
<html:select property="Type" name="element2" indexed="true">
<html:options collection="TypeList" property="value"/>
</html:select>
</TD>
setup form in sampleForm.java, am getting the values for the list. but its not passing to logic iterate
protected Vector sampleList = new Vector();
/**
* #return the sampleList
*/
public Vector getsampleList() {
return sampleList;
}
/**
* #param sampleList the sampleList to set
*/
public void setsampleList(Vector sampleList) {
this.sampleList = sampleList;
}
in action class am setting the list like this
sampleForm.setSampleList(sampleList);