tags:

views:

790

answers:

2

HI,

I m doing the folling stuff in the jsp code I need to do it using Struts or using JSTL tag can any body have relevant idea please share..

The following is my JSP code

<%

         Object category = request.getAttribute("categoryDetails");
         Hashtable<String, Hashtable<String, Integer>> cat = (Hashtable<String, Hashtable<String, Integer>>) category;
         //out.print(cat.entrySet());

         Set<String> functions = cat.keySet();

         for(String fun : functions){

          out.print("-----------");
          out.print(fun);
          out.print("-----------");

          Hashtable<String, Integer> obj = cat.get(fun);

          Vector<String> subFunction = new Vector<String>(obj.keySet());

          Collections.sort(subFunction);

          for(String str : subFunction){                            
           out.print("#"+str+"-"+obj.get(str));
           }
         }



         %>

Thanks in advance.

A: 

I wouldn't use either, looking at the logic involved, I would rather write a custom jsp tag to achieve this. JSTL/Struts are equally good/horrible at doing this.

questzen
A: 

You can use a custom tag or create a temporary view structure like this:

public class FunctionView {
    String functionName;
    List<SubFunctionView> subfunctions;

    public FunctionView(String functionName, List<SubFunctionView> subfunctions) {
        this.functionName = functionName;
        this.subfunctions = subfunctions;
    }

    public String getFunctionName() {
        return functionName;
    }

    public void setFunctionName(String functionName) {
        this.functionName = functionName;
    }

    public List<SubFunctionView> getSubfunctions() {
        return subfunctions;
    }

    public void setSubfunctions(List<SubFunctionView> subfunctions) {
        this.subfunctions = subfunctions;
    }
}

public class SubFunctionView {
    String subFunctionName;
    Integer value;

    public SubFunctionView(String subFunctionName, Integer value) {
        this.subFunctionName = subFunctionName;
        this.value = value;
    }


    public String getSubFunctionName() {
        return subFunctionName;
    }

    public void setSubFunctionName(String subFunctionName) {
        this.subFunctionName = subFunctionName;
    }

    public Integer getValue() {
        return value;
    }

    public void setValue(Integer value) {
        this.value = value;
    }
}

Then you just expost a method called List getFunctionsView() on your controller and use a simple nested jstl foreach loop.

Edit: Had to rework this one slightly.

krosenvold