Struts N00b - creating dynamic HTML from databases

Hello,

First of all, I'm new to struts, but I'm giving it a try because I think the general environment is well suited to my project. Of course, if there's a better system out there, please let me know.

I have a database of users for a website (nothing special - I'm planning to write a class extending the struts User and put it in a MemoryUserDatabase). I want to be able to list the users and a few of their attributes on an html page (there will probably be several pages of users). The styling each user receives is complex enough that I'll probably want to put it in a JSP page rather than in java code.

Unfortunately there are a few things I don't know how to do - for one, I'm not sure how to dynamically determine which users to display on a particular page of results. Second, I'm not sure how to create a separate file which determines how a user will be displayed, and dynamically include a certain number of pseudo-copies of that file in another jsp page.

Though I have tried, I have been unable to find a tutorial that makes it clear how to do these things.

Any hints?

[1127 byte] By [leptogenesisa] at [2007-11-27 5:33:18]
# 1

[nobr]Ok, lets break things down a bit.

For displaying a list of users, rather than defining a jsp page and including it multiple times, a better approach would be to use a loop in the JSP page to repeat a certain block of HTML once for every user. The struts logic:iterate tag will do this as will the JSTL c:forEach.

I am a fan of JSTL and it integrates very well with struts jsp pages so I would recommend that approach :-)

something like

<c:forEach var="user" items="${userList"}>

<c:out value="${user.name}"/><br>

</c:forEach>

The JSP page should worry about displaying the users, nothing about where they come from. The best way to do that is to provide a List of users as a request attribute on the page. (in this case I called it userList)

Your struts action then has the responsibility of loading the users (from somewhere) and putting the data into a list of java beans before forwarding to the jsp to display the result. You can then make your action as simple or complicated as you like in retrieving the list of users, and all your jsp does is display the result.

Does that all make sense?[/nobr]

evnafetsa at 2007-7-12 15:00:13 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

That makes sense, for the most part...

I guess I'm still not sure how Struts accomplishes the communication between JSP and java, though...does the class that extends ActionSupport behave as a bean, so you can access its fields from the JSP page? Or do I have to create new beans (writing a new class to contain them), store them in some bean server and access them from there? What would that look like in the Java code?

Thanks for your help.

leptogenesisa at 2007-7-12 15:00:13 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Umm...anyone care to answer the question from my second post?
leptogenesisa at 2007-7-12 15:00:13 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...