views:

847

answers:

2

My situation:

On my jsp site I show a table. When somebody click a row, this row must be marked with an other backround color for example. Also more then one row can be marked. Two things are important. First: I bear in mind which rows are marked. Secend: In which order.

My idea is:

I have a ArrayList and put all row IDs in this. So I solve both problems. Now I put this ArrayList in the session for the next click and the row ID ( req.getSession().setAttribute(req.getParameter("rowID") , ""+arrayList.size()); ) for the jsp file.

My problem in jsp file:

I don't now what's called session variable name.

if this rowId alrady clicked? ..

I have got only the bean name. How can I get the content from the session, if exact this line must be marked?

Normally I set a session variable so: req.getSession.setAttribut("printView", Boolean.TRUE) and get a session variable so:

I work with struts, formbeans and jsp.

Sorry, I haven't good English and this problem is so difficult for me. So I can't declare it better at this time.

A: 

Let's first analyze what you are storing in session:

req.getSession().setAttribute(req.getParameter("rowID") , ""+arrayList.size());

req.getParameter("rowID") returns String value of the request parameter. If your page link is yourPage.jsp?rowID=3 its value will be "3". This value will be used as a key in the invocation of the method HttpSession#setAttribute(String, Object).

""+arrayList.size() is just a String representation of the size of your arrayList in which you are trying to store IDs of clicked rows

Answer to your question "I don't now what's called session variable name." cannot be given since it depends on the value of rowID request parameter.

If you want to check what is in the session just iterate through its attributes:

for (Enumeration elem : req.getSession().getAttributeNames()) {
    String name = elem.nextElement();
    System.out.println(name + " : " + req.getSession().getAttribute(name));
}

If you don't store your arrayList in the session previous code will probably return something like this:

1 : 0
5 : 0
1 : 0

As Vinegar suggested I would do next:

String clickedRowId = req.getParameter("rowID");
List<String> arrayList = (List)req.getSession().getAttribute("clickedRows");
if (!arrayList.contains(clickedRowId)) {
    arrayList.add(clickedRowId);
}
req.getSession().setAttribute("clickedRows", arrayList);

Your arrayList will be holding a list of clicked row IDs and their indices will be telling you about their chronological order.

Note: There are certainly better and more scalable ways to achieve this. Anyway, it's a good idea to implement your solution and then look for a better one.

Boris Pavlović
You got my point, but not fully. Here he needs the information; which row is clicked and when. So we will need a map instead of ArrayList, I believe.
Adeel Ansari
Or better still use LinkedList it will prevent the ordering and you will get that info as well.
Adeel Ansari
"First: I bear in mind which rows are marked. Secend: In which order." - if you add clicked rows sequentially in the arrayList, their indices will answer the second question, aren't they? As far as I can see there's no need to know when some row was clicked
Boris Pavlović
@Vinegar what are the benefits of using LinkedList over ArrayList in this case?
Boris Pavlović
You are absolutely correct here, I confused LinkedList with LinkedHashMap. But LinkedList would perform better in case there is a feature of clicking the row again to remove it from the coloured list.
Adeel Ansari
A: 

Thank you very much. How evaluate the arryList in jsp file, which is in session?

<datagrid:tableEx ...>
<c:if test="${ arrayList.contains(bean.rowID)}">
 <div id="tableheader">
                 <datagrid:column property="bean.rowID" ... bgcolor="#BDC6DD"/>
                 <datagrid:column ... bgcolor="#BDC6DD" />
                 <datagrid:column ... bgcolor="#BDC6DD" />
                  ...
 </div>
</c:if>
<c:if test="${!arrayList.contains(bean.rowID)}">
 <div id="tableheader">
                 <datagrid:column property="bean.rowID"/>
                 <datagrid:column ... />
                 <datagrid:column ... /> 
            </div>
</c:if>

So, my suggestion doesn't give you any hint? .. . let me know where you don get my point exactly.
Adeel Ansari

related questions