aspose file tools*
The moose likes Struts and the fly likes <logic:iterate> problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "<logic:iterate> problem" Watch "<logic:iterate> problem" New topic
Author

<logic:iterate> problem

Ann Anderson
Greenhorn

Joined: Dec 29, 2008
Posts: 7
hello

I am using <logic:iterate> to display dynamic retrieved data. but the data is not displayed please help me. I am pasting my code below. I am using Struts and Hibernate.

My Action Class

public class viewCelebrity extends Action {

List listLoginData = new ArrayList();

public List getListLoginData(){
return listLoginData;
}

public void setListLoginData(List listLoginData){
this.listLoginData = listLoginData;

}
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{

SessionFactory sessionFactory;
Session session;
Transaction transaction;
Configuration configuration;
Query query;
HttpSession httpSession=request.getSession();

try {
LoginForm loginForm = new loginForm();
sessionFactory=new Configuration().configure().buildSessionFactory();
session=sessionFactory.openSession();
transaction=session.beginTransaction();
query = session.createQuery("from LoginForm loginData");
listLoginData = query.list();

}
catch(Exception e){
e.printStackTrace();
}

return mapping.findForward("success");
}//end of execute
}

My Action Form is

public class LoginForm extends ValidatorForm{

private String userName;
private String passWord;
private String pin;
private String reType;

public void setUserName(String userName){
this.userName = userName;
System.out.println("Inside setUserName");
}

public void setPassWord(String passWord){
this.passWord = passWord;
System.out.println("Inside setPassWord");
}

public void setPin(String pin){
this.pin = pin;
System.out.println("Inside setPin");
}

public void setReType(String reType){
this.reType = reType;
System.out.println("Inside setReType");
}

public String getUserName(){
System.out.println("Inside getUserName");
return userName;
}

public String getPassWord(){
System.out.println("Inside getPassWord");
return passWord;
}

public String getPin(){
System.out.println("Inside getPin");
return pin;
}

public String getReType(){
System.out.println("Inside getReType");
return reType;
}

public void reset(){

}
}


My JSP page is

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<jsp:useBean id="celebrity" class="com.struts.Celebrity" scope="request"/>

<div id="body">

<table bgcolor="green">
<tr>
<td>User Name</td>
<td>Password</td>
<td>Pin</td>
</tr>
<logic:iterate id="dvd" type="com.struts.LoginForm" name="celebrity" property="listLoginData">
<tr>
<td>Inside tr</td>
<td><bean:write name="dvd" property="userName" /></td>
</tr>
</logic:iterate>
<tr><td>After Iterate</td></tr>
</table>
</div>

THANKS in Advance

Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Since you're using Struts 1, Action classes must be coded as thread-safe. You should therefore not use any instance variables in your action class, which means you should make listLoginData a local variable.

The reason you're not getting any data in the JSP, however, is that your action class does not place the data in any scope that can be retrieved by the JSP. Your JSP is expecting a bean of type Celebrity that has a listLoginData property to be in request scope, when your action class has placed to no such bean in request scope.

It almost looks like you're trying to create some strange hybrid of Struts 1 and Struts 2. In Struts 2, Action classes do have data properties, but there is no ActionForm class, and it uses s:xxx tags rather than bean:xxx or html:xxx tags.


Merrill
Consultant, Sima Solutions
Ann Anderson
Greenhorn

Joined: Dec 29, 2008
Posts: 7
Thanks Merrill for your reply

My problem is solved. But i am confused a bit I am pasting my code again will you please tell me whether i am following the right rules of struts or i am still creating a hybrid of struts1x and struts2. ops: and also I now know what you wanted to say when you said that Action classes must be coded as thread-safe. So I must remove the instance variable and make it local.

Thanks again but please try and solve my problem about tiles mentioned in the previous post.


I am using struts1.3.10

**********************************
This is my Action class
**********************************
public class ViewCelebrity extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{
List listLoginData = new ArrayList();
SessionFactory sessionFactory;
Session session;
Transaction transaction;
Configuration configuration;
Query query;

try {
LoginForm loginForm = new LoginForm();
sessionFactory=new Configuration().configure().buildSessionFactory();
session=sessionFactory.openSession();
transaction=session.beginTransaction();
query = session.createQuery("from LoginForm loginData");
listLoginData = query.list();
request.setAttribute("listLoginData", listLoginData);
}
catch(Exception e){
e.printStackTrace();
}

return mapping.findForward("success");
}//end of execute
}



****************************************
This is my ActionForm calss
****************************************

public class LoginForm extends ValidatorForm{

private String userName;
private String passWord;
private String pin;
private String reType;

public void setUserName(String userName){
this.userName = userName;
System.out.println("Inside setUserName");
}

public void setPassWord(String passWord){
this.passWord = passWord;
System.out.println("Inside setPassWord");
}

public void setPin(String pin){
this.pin = pin;
System.out.println("Inside setPin");
}

public void setReType(String reType){
this.reType = reType;
System.out.println("Inside setReType");
}

public String getUserName(){
System.out.println("Inside getUserName");
return userName;
}

public String getPassWord(){
System.out.println("Inside getPassWord");
return passWord;
}

public String getPin(){
System.out.println("Inside getPin");
return pin;
}

public String getReType(){
System.out.println("Inside getReType");
return reType;
}

public void reset(){

}
}


*******************************************
This is my .jsp page
*******************************************

<tiles:insert attribute="header"/>
<table bgcolor="green" width="100%">
<tr>
<td>User Name</td>
<td>Password</td>
<td>Pin</td>
</tr>
<logic:iterate id="cele" name="listLoginData">
<tr>
<html:form action="">
<td><bean:write name="cele" property="userName" /></td>
<td><bean:write name="cele" property="passWord"/></td>
<td><bean:write name="cele" property="pin"/></td>
</html:form>
</tr>
</logic:iterate>
</table>
<tiles:insert attribute="footer"/>

_______________________________________________________________________

I have one more question. I want each row to be placed on different tile and insert the tiles on this page dynamically. How should i do?

Thanks in advance.

Regards
Ann
Ann Anderson
Greenhorn

Joined: Dec 29, 2008
Posts: 7
Hello Merrill

Thanks again but please try and solve my problem about tiles mentioned in the previous post.

Regards
Ann
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: <logic:iterate> problem