sorting table in a jsp

Hello All,

I want to sort a result set by column name...I'm using MVC pattern ....so I have an if clause to decide the sorting order as:

<c:if test="${sort == asc}">

<a href="Controller?sort=desc" >Student</a>

</c:if>

<c:if test="${sort == desc}">

<a href="Controller?sort=asc" >Student</a>

</c:if>

I'm using a method in my action which sorts the given list ...so my question is how can i save the resultant List to be sorted ...if I put in request ...it can be displayed only one time and can i keep that in session....or is there any other better way to do this....

Thanks,

Maithri

[700 byte] By [Maithria] at [2007-11-27 4:20:29]
# 1
When you get back the results, why not just do the sort on the jsp page instead of posting back to the server?
kdajania at 2007-7-12 9:27:33 > top of Java-index,Java Essentials,Java Programming...
# 2
Thanks for your quick reply...I dont want to use javascript for sorting ....is there any better way to do this in jsp ...I have written a method because the user can sort by different columns...Thanks...
Maithria at 2007-7-12 9:27:33 > top of Java-index,Java Essentials,Java Programming...
# 3
You don't need javascript. Just do it with the javacode in the jsp. You can either: - write a jsp just for sorting, and call that jsp whenever you want to sort from the display jsp.- Write a method in the display jsp to do the sort.
kdajania at 2007-7-12 9:27:33 > top of Java-index,Java Essentials,Java Programming...
# 4
> how can i save the resultant List to be sortedPut it in the user's session.
DrClapa at 2007-7-12 9:27:33 > top of Java-index,Java Essentials,Java Programming...
# 5
Thanks for your quick replies ...I'm not allowed to use scriptlets in jsp and I need to make less use of session attributes...Any other go?Thanks....
Maithria at 2007-7-12 9:27:33 > top of Java-index,Java Essentials,Java Programming...
# 6

> I need to make less use of session attributes...

If you want to preserve data from one request to another then you need to put it in the user's session. Or alternatively you need to retrieve it again from wherever you retrieved it from the first time. Suit yourself. There are no magic solutions, only ordinary answers.

DrClapa at 2007-7-12 9:27:33 > top of Java-index,Java Essentials,Java Programming...
# 7
Can it be done like this:request.setAttribute("list", list) ->display in jsp and again set in request ...so that the action class can get it from request again ....is there a way to set an object in jsp...Thanks...
Maithria at 2007-7-12 9:27:33 > top of Java-index,Java Essentials,Java Programming...
# 8

The JSP generates HTML that is loaded into the browser. Are you asking whether something can be done in that HTML to put information into the next request?

Yes, it can. You could put a copy of the entire table into the next request by making it a whole lot of hidden parameters for the "Controller?sort=asc" URL. But now you are out into the realm of really stupid solutions, so don't do that.

The best solutions are:

1. Sort the data on the client using Javascript.

2. Store the data in the user's session and sort it on the server.

But you have already been told this several times.

DrClapa at 2007-7-12 9:27:33 > top of Java-index,Java Essentials,Java Programming...
# 9

Thanks a lot for your replies ...

I'm going with user's session and doing something like this:

HttpSession userSession = request.getSession();

userSession.setAttribute("list",list);......the first time in action

from jsp back to action ...

if(userSession.getAttribute("list") != null ) {

if(sort.equals("asc"))

{

sort(list,asc);

}

Thanks....

Maithria at 2007-7-12 9:27:33 > top of Java-index,Java Essentials,Java Programming...
# 10
hello,can anyone give me an example of sorting a table by column using javascript...does it affect 508 compliancy ...Thansk,Maithri...
Maithria at 2007-7-12 9:27:33 > top of Java-index,Java Essentials,Java Programming...
# 11
Here is an example http://javascript.internet.com/forms/sort-data-table.html
kdajania at 2007-7-12 9:27:33 > top of Java-index,Java Essentials,Java Programming...