0

i just started to learn struts(+spring) MVC, and I could use some explanation of this situation :

i am using Struts Tiles for my presentation layer and i want to display some data from database in few tiles that with main content tile will form complete page. I can't figure out proper way of doing this, that fits mvc pattern/best practices.

The content of those tiles is fetched form database, its not based on user input, e.g. tile that lists few latests products added to shop etc.

What i did: i have created class 'A' with static method that queries database and returns list of few latests products, and in jsp of this tile i have just put scriptlet like:
<% for each item in A.GetLastestProducts(); dispaly data %>

but i have been told thats bad way of doing this

Can someone explain me how to handle this situation properly, like "create class that will query your database, than create blach that will blach blach, then in your tile jsp blach blach"

thanks

sv13
  • 176
  • 3
  • 13

1 Answers1

1

If you're using struts, I would suggest leveraging the struts architecture for your JSP. I would probably structure it similar to this:

public class MyStutsAction extends ActionSupport implements preparable{

    private List<MyNewProducts> newProducts;

    @Override
    public void prepare() throws Exception {
        if(products == null){
            products = MyNewProductsDAO.getProductList();
        }
    }

    //Struts action stuff

    public List<MyNewProducts> getNewProducts(){
        return newProducts;
    }

    public setNewProducts(List<MyNewProducts> newProdcuts){
        this.newProducts = newProducts;
    }

}

Then from your JSP page you would access it as such:

<s:iterator value="newProducts" status="stat">
    <div> <s:property value="%{attributeName}" /> </div>
</s:iterator>

This will eliminate some unnecessary database calls by maintaining your list of products in the value stack and the conditional in the prepare method will populate the list if for some reason it is null.

Russell Shingleton
  • 3,176
  • 1
  • 21
  • 29
  • thanks, i find it helpful, but with my level of understanding struts, after calling MyStutsAction and displaying page thats mapped to method result, that would populate like one of few tiles on my page, what if i want another tile on the same page with content also form database, but i want it to be separate form MyStutsAction, as a reusable tile – sv13 Sep 16 '11 at 16:35
  • Use an interface or base action then. – Steven Benitez Sep 16 '11 at 19:00
  • @Steven Benitez, i am not sure what do you mean, let me rephrase the problem. From my understanding, one struts action produces data to be displayed in one view. Maybe i am wrong but i am thinking of one struts Tile as one view, now, i want to build page that will consist of few tiles, all tiles are separated form each other, they can be reused on other pages, so now i am wondering what is a proper way form design point of view to populate all tiles with data – sv13 Sep 16 '11 at 21:48
  • 1
    All the tiles together = 1 view. Tiles just let you reuse common parts of pages, like headers, footers, etc. The final result is one single HTML document for the browser, which is backed by one action. – Steven Benitez Sep 16 '11 at 22:28
  • then, how do i populate each tile with data, since i dont know what tiles will there be on action results page, i cant do it through action – sv13 Sep 17 '11 at 08:27
  • 1
    Another option would make the jsp with your new products an include. On whatever other page you need it on you simply do a . As Steve was mentioning, tiles already built for this functionality and you should know what tiles are present on each action as the action definitions in your struts.xml coupled with your tiles.xml determine which are present. You simply add the above code to each action you need it in. – Russell Shingleton Sep 17 '11 at 11:44