• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

logic:iterate in struts

 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI all
can anyone help me with the below problem ??
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Betsy Camel
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!