| Author |
logic:iterate in struts
|
Betsy Camel
Ranch Hand
Joined: Oct 02, 2003
Posts: 119
|
|
hi, I have a strtus application in which the model is returning an array (str[][]). the action is creating an object of this model & storing the array in the request.attribute. Display obj[][]=new Display(); request.setAttribute("obj",obj); map.findForward("success"); in the jsp i am getting this request attribute String[][] hello= (String[][])(request.getAttribute("obj")); i want to use logic:iterate to iterate through this array & display the values in the array. Please help. its urgent. Regards Betsy
|
 |
Betsy Camel
Ranch Hand
Joined: Oct 02, 2003
Posts: 119
|
|
HI all can anyone help me with the below problem ??
|
 |
Brent Sterling
Ranch Hand
Joined: Feb 08, 2006
Posts: 948
|
|
Sorry, I do not have any great ideas for you. Is this line of code correct? Display obj[][]=new Display(); It does not look valid to me and it does not seem to match up with the rest of the code. You are getting a two-dimensional array of Strings? I cannot think how to use the logic:iterate tag for that. You might be about to use javascript. - Brent
|
 |
Merrill Higginson
Ranch Hand
Joined: Feb 15, 2005
Posts: 4864
|
|
Like Brent, I'm having a hard time making sense of your code. However, I can tell you how to use <logic:iterate> with a two-dimentional String array. If you code this in your Action: String[][] rowsAndCols = {{"A1", "A2", "A3"}, {"B1", "B2", "B3"}, {"C1", "C2", "C3"}}; request.setAttribute("rowsAndCols", rowsAndCols); And this in your JSP: <logic:iterate id="row" name="rowsAndCols"> <logic:iterate id="col" name="row"> <bean:write name="col" /> </logic:iterate> </logic:iterate> You'll get this: A1 A2 A3 B1 B2 B3 C1 C2 C3
|
Merrill
Consultant, Sima Solutions
|
 |
Betsy Camel
Ranch Hand
Joined: Oct 02, 2003
Posts: 119
|
|
hi i tried the above code and got the following javax.servlet.ServletException: Cannot find bean col in any scope org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:846) org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779) org.apache.jsp.searchResults_jsp._jspService(org.apache.jsp.searchResults_jsp:123) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:99) javax.servlet.http.HttpServlet.service(HttpServlet.java:802) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:325) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245) javax.servlet.http.HttpServlet.service(HttpServlet.java:802) org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1063) org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:386) org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:229) org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194) org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432) javax.servlet.http.HttpServlet.service(HttpServlet.java:709) javax.servlet.http.HttpServlet.service(HttpServlet.java:802) Please help
|
 |
Merrill Higginson
Ranch Hand
Joined: Feb 15, 2005
Posts: 4864
|
|
Betsy, Before I posted the above code, I tried it in a test application and it ran exactly as I outlined. Can you show us your code? Merrill
|
 |
Betsy Camel
Ranch Hand
Joined: Oct 02, 2003
Posts: 119
|
|
hi please find the code below. <%@ page language="java" contentType='text/html; charset=UTF-8' %> <%@ taglib uri="/WEB-INF/tags/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/tags/struts-bean.tld" prefix="bean" %> <html> <body> <br><br><br> <table border="1" align="center"> <tr bgcolor="#D1CBD8"> <th><bean:message key="app.server"/></th> <th><bean:message key="app.status"/></th> <th><bean:message key="app.filesize"/></th> <th><bean:message key="app.timestamp"/></th> </tr> <% String[][] hello= (String[][])(request.getAttribute("obj")); %> <logic:iterate id="row" name="hello" scope="request"> <logic:iterate id="col" name="row" scope="request"> <bean:write name="col" /> </logic:iterate> </logic:iterate> </body> </html> This is in the jsp and in the servlet, i have set the attribute value. Please give me the solution
|
 |
Merrill Higginson
Ranch Hand
Joined: Feb 15, 2005
Posts: 4864
|
|
Change <logic:iterate id="row" name="hello" scope="request"> to <logic:iterate id="row" name="obj" scope="request"> The "hello" variable is not in any scope, so you can't use it in a <logic:iterate> tag unless you first put it in a scope, for example: <% request.setAttribute("hello", hello); %> Note: this will not work unless "obj" actually is of type String[][].
|
 |
Betsy Camel
Ranch Hand
Joined: Oct 02, 2003
Posts: 119
|
|
hi \i have changed hello to obj <logic:iterate id="row" name="obj" scope="request"> <logic:iterate id="col" name="row"> <bean:write name="col" /> </logic:iterate> </logic:iterate> </body> </html> but still i am getting the same error!
|
 |
Merrill Higginson
Ranch Hand
Joined: Feb 15, 2005
Posts: 4864
|
|
The only explanation I can think of is that "obj" must not be of type String[][]. The code that you gave us in your first post: Display obj[][]=new Display(); Is not valid Java code, and will not compile. Can you show us the code for your Display class?
|
 |
Betsy Camel
Ranch Hand
Joined: Oct 02, 2003
Posts: 119
|
|
//package strut; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts.action.ActionError; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import com.DTO; import com.Display; import java.io.IOException; import java.util.HashMap; public class ControlAction extends Action{ /** * @param args */ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res)throws Exception { String path = ((getterssetters) form).getPath(); String filename = ((getterssetters) form).getFilename(); if ( path == "" ) { ActionErrors errors = new ActionErrors(); errors.add(ActionErrors.GLOBAL_ERROR,new ActionError("pathvalue",path)); // Report any errors we have discovered if (!errors.isEmpty()) { saveErrors(req, errors); } } if (filename==""){ ActionErrors errors = new ActionErrors(); errors.add(ActionErrors.GLOBAL_ERROR,new ActionError("filenamevalue",filename)); if (!errors.isEmpty()) { saveErrors(req, errors); } } System.out.println("in controlActiopm"+path); Display object = new Display(path,filename); String[][] str = object.getDetails(); //req.setAttribute("value",str); // req.setAttribute("server",array); // DTO value=object.getDetails(); //HttpSession session = req.getSession(); req.setAttribute("obj", str); return mapping.findForward("success"); } }
|
 |
Merrill Higginson
Ranch Hand
Joined: Feb 15, 2005
Posts: 4864
|
|
Try this: <logic:iterate id="row" name="obj" scope="request"> <logic:notEmpty name="row > <logic:iterate id="col" name="row"> <logic:notEmpty name="col"> <bean:write name="col" /> </logic:notEmpty> </logic:iterate> </logic:notEmpty> </logic:iterate>
|
 |
 |
|
|
subject: logic:iterate in struts
|
|
|