logic:iterate

Hi,

I am using the following code for getting an ArrayList into a JSP using form bean. The code is poulating entire ArrayList at a stretch. I want it to display one by one. VCan u pls help me in this.

Thanx in advance.

<logic:iterate id="selectionCriteriaForm" name="selectionCriteriaForm" property="quesArray">

<bean:write name="selectionCriteriaForm" property="quesArray]"/>

</logic:iterate>

selectionCriteriaForm is the form bean name which in turn uses the struts-config.xml to point to actual form bean SelectionCriteriaform. quesArray is the property that i want to get into a JSP. It is an arraylist.

Kindly help me in this. I worked on this so much that i did'nt get any help on this. Pls help me in this.

[781 byte] By [dev_phan] at [2007-11-26 12:16:42]
# 1
Hi why dont u try to set the array list in the session or in request and get it directly, instead of setting it in the form bean and taking it,u can also get each elements in the object too.....
peak-balu at 2007-7-7 14:53:09 > top of Java-index,Archived Forums,Socket Programming...
# 2

Ya, set the ArrayList data in a session and have a <logic:present ...> to see if that session exists, and iterate through the ArrayList.

so it would be something like this

<logic:present name="arrayListSessionObject">

<logic:iterate id="someIdYouWant" name="arrayListSessionObject">

<bean:write name="someIdYouWant"/>

</logic:iterate>

</logic:present>

I'm guessing this is going in a form selection drop down? If so then the name of that form element should be the thing in your form bean.

bah screw it, just upgrade to Struts 2.x or WebWork 2.x and avoid form beans altogether. :)

Message was edited by:

pchukwura

Message was edited by:

pchukwura

pchukwura at 2007-7-7 14:53:09 > top of Java-index,Archived Forums,Socket Programming...
# 3
Use StringTokenizer.
skp71 at 2007-7-7 14:53:09 > top of Java-index,Archived Forums,Socket Programming...
# 4
nah it would be best to keep java code out of the JSP and use the struts tags as much as possible for page display.
pchukwura at 2007-7-7 14:53:09 > top of Java-index,Archived Forums,Socket Programming...
# 5

Hi all,

sorry for late reply.

i solved the problem using two logic:iterate tags only.

but i wrote some loop to avoid the duplication of values.

the idea is that the second logic iterate tag offset will be the index of first one and use some loop to display only first value.

Thanx for ur help

Phani Kumar

dev_phan at 2007-7-7 14:53:09 > top of Java-index,Archived Forums,Socket Programming...
# 6

Dear All,

I m using struts 1.2.9

i have jsp like

<beutification code>

<html:select property="doc">

<logic:present name="data">

<logic:iterate id="reclist" name="data">

<bean:define id="Id" name="reclist" property="id"/>

<html:option value="<%=Id%>">

<bean:write name="reclist" property="id"/>

</html:option>

</logic:iterate>

</logic:present>

</html:select>

--

Struts_config code like

<form-beans>

<form-bean name="selectDocForm" type="com.myapp.struts.selectDocActionForm"/>

<form-bean name="selectDocActionForm" type="com.myapp.struts.selectDocActionForm"/>

<form-bean name="setDocForm"

type="com.myapp.struts.setDocForm"/>

</form-beans>

<action path="/setdoc"

name="setDocForm"

scope="session"

input="/patOp.jsp"

type="org.apache.struts.setDocAction">

<forward name="sucess" path="/patopNext.jsp"/>

</action>

<action path="/selectDoc"

type="org.apache.struts.actions.DispatchAction"

name="selectDocActionForm"

scope="session"

input="/patOp.jsp"

parameter="method">

<forward name="sucess" path="/patopNext.jsp"/>

</action>

-

setDocAction.java

package com.myapp.struts;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

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 org.apache.struts.action.ActionErrors;

import javax.sql.DataSource;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.*;

//import java.util.ArrayList;

//import java.util.List;

import java.sql.*;

import org.apache.struts.actions.DispatchAction;

public class selectDocAction extends DispatchAction

{

private String RESULT = null;

/** Creates a new instance of selectDocAction */

public selectDocAction() {

}

// This sets the list as a session bean

public ActionForward disp(ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response)

throws IOException, ServletException {

List list = new ArrayList();

Connection con = null;

PreparedStatement pst;

ResultSet results;

try

{

String url = "jdbc:mysql://localhost/doctor";

Class.forName("com.mysql.jdbc.Driver");

con = DriverManager.getConnection (url, "root", "admin");

pst = con.prepareStatement("SELECT docName FROM doctorLogin");

results = pst.executeQuery();

while(results.next())

{

list.add("results.getString(1)");

}//while

RESULT = "SUCCESS";

request.setAttribute("data",list);

}

catch (Exception s)

{

System.out.println("SQL Error

");

System.out.println(s.getMessage());

}//catch

return (mapping.findForward("RESULT"));

}

public ActionForward login(ActionMapping mapping,ActionForm form,HttpServletRequest request,

HttpServletResponse response)throws IOException, ServletException

{

selectDocActionForm selectDocActionForm = (selectDocActionForm)form;

try

{

if(selectDocActionForm!=null)

{

request.setAttribute("login","ok");

}

}//try

catch (Exception s)

{

System.out.println("SQL Error

");

System.out.println(s.getMessage());

}//catch

return (mapping.findForward("sucess"));

}

}

-

selectDocActionForm.java

package com.myapp.struts;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionMapping;

//import org.apache.struts.action.ActionError;

import org.apache.struts.action.ActionErrors;

import java.util.List;

import java.util.ArrayList;

public class selectDocActionForm extends org.apache.struts.action.ActionForm

{

private List doctors = new ArrayList();

private String[] doctorSelect = null;

boolean id = false;

private String name=null;

private String password=null;

public List getDoctors() {

return this.doctors;

}

public void setdoctors(List value) {

this.doctors = new ArrayList(value);

}

public selectDocActionForm() {

super();

}

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {

ActionErrors errors = new ActionErrors();

return errors;

}

}

--

setDocAction.java

package com.myapp.struts;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

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 org.apache.struts.action.ActionErrors;

import javax.sql.DataSource;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Vector;

import java.util.ArrayList;

import java.util.List;

public class setDocAction extends Action

{

private String RESULT = null;

/** Creates a new instance of selectDocAction */

public setDocAction() { }

// This sets the list as a session bean

public ActionForward execute(ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response)

throws IOException, ServletException {

return (mapping.findForward("sucess"));

}

}

--

setDocForm.java

package com.myapp.struts;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.action.ActionError;

import org.apache.struts.action.ActionErrors;

public class setDocForm extends ActionForm

{

private String id = null;

private String doc=null;

public String getDoc() {

return doc;

}

public void setDoc(String doc) {

this.doc = doc;

}

public String getId()

{

return (id);

}

public void setId(String id)

{

this.id = id;

}

public void reset(ActionMapping mapping,

HttpServletRequest request)

{

this.id = "";

}

public ActionErrors validate(ActionMapping mapping,

HttpServletRequest request)

{

ActionErrors errors = new ActionErrors();

if ( (id == null ) || (id.length() == 0) )

{

errors.add("id", new ActionError("errors.lookup.id.required"));

}

return errors;

}

}

-

Kindly revert back

i m totally confused...............

thank u in advance!!

lil_gayu at 2007-7-7 14:53:09 > top of Java-index,Archived Forums,Socket Programming...
# 7
dear all,getting the error like:reclist cannot be found in any scopethank u..........in advance
lil_gayu at 2007-7-7 14:53:09 > top of Java-index,Archived Forums,Socket Programming...
# 8
Put an if condition or logic:notEmpty to check for null for reclist. That should solve the issue.
skp71 at 2007-7-7 14:53:09 > top of Java-index,Archived Forums,Socket Programming...
# 9
I guess you need to define "reclist" from bean in your struts-config.xml file.
SirGeneral at 2007-7-7 14:53:09 > top of Java-index,Archived Forums,Socket Programming...
# 10

dear all,

i m tottaly lost!

i m accessing list in jsp using JSTL I incuded plugin for my list but donno wat went wrong!

can u please suggest a code to

1. get list from database

2. set it in some scope

3. access list in jsp

4. print it in drop down list form

kindly revert back...........

thanks in advance.

lil_gayu at 2007-7-7 14:53:09 > top of Java-index,Archived Forums,Socket Programming...
# 11

> dear all,

> i m tottaly lost!

>

> i m accessing list in jsp using JSTL I incuded plugin

> for my list but donno wat went wrong!

> can u please suggest a code to

> 1. get list from database

In your action class, go to the database and retrieve the data. Populate the records in a collection object. Now direct the navigation to your page to display the data.

> 2. set it in some scope

Set the scope of the action in your struts-config.xml file. For your requirement you can set it to request.

> 3. access list in jsp

> 4. print it in drop down list form

Once the values are set into the collection object in your form bean you can retrieve them in your jsp. For populating a dropdown you can write the following code:

<html:select property="selectedValue">

<html:option value="" />

<html:optionsCollection property="collectionList" />

</html:select>

"collectionList" is the collection object variable in your backing form bean with getters and setters. When you select a particular value from the dropdown and submit the form, the selected value is retireved in "selectedValue" variable.

>

> kindly revert back...........

>

> thanks in advance.

SirG

SirGeneral at 2007-7-7 14:53:09 > top of Java-index,Archived Forums,Socket Programming...
# 12
hi SirGeneral,i hav tried it ..........but cant make it possible.can u plz tell me relevent code in action form , form bean and struts_config.xml file?I would be so thankful.............
lil_gayu at 2007-7-7 14:53:09 > top of Java-index,Archived Forums,Socket Programming...
# 13

Let me give u an example:

My Action Class:

protected ActionForward execute(ActionMapping mapping,

ActionForm form, HttpServletRequest request,

HttpServletResponse response) throws Exception {

//Build the banks list dropdown.

Collection banksList = AccountAdminUtil.getBanksList() ;

//Setting the collection object into your form bean

//I had defined it as a lazy validator form in my struts-config

//Define ur form bean here and set the values

//Banks list is a collection variable in your form bean

((LazyValidatorForm) form).set("banksList", banksList );

return mapping.findForward("success");

}

My Utility Class:

public static Collection getBanksList() {

ArrayList banksList = new ArrayList();

LabelValueBean labelValueBean;

labelValueBean = new LabelValueBean("010 - California", "10");

banksList.add(labelValueBean);

labelValueBean = new LabelValueBean("011 - Arizona", "11");

banksList.add(labelValueBean);

labelValueBean = new LabelValueBean("021 - New Mexico", "21");

banksList.add(labelValueBean);

labelValueBean = new LabelValueBean("031 - Arkansas", "31");

banksList.add(labelValueBean);

labelValueBean = new LabelValueBean("041 - Utah", "41");

banksList.add(labelValueBean);

labelValueBean = new LabelValueBean("051 - Neveda", "51");

banksList.add(labelValueBean);

labelValueBean = new LabelValueBean("061 - Texas", "61");

banksList.add(labelValueBean);

Collections.sort(banksList, (LabelValueBean.CASE_INSENSITIVE_ORDER));

return banksList;

}

My JSP:

<TABLE>

<tr>

<TD class="formLabel" align="right" valign="bottom">

<bean:message key="label.account.bank.no" />

</TD>

<TD>

<html:select property="bankNumber">

<html:option value="" />

<html:optionsCollection property="banksList" />

</html:select>

</TD>

</tr>

</TABLE>

And the above code works perfectly fine!!

SirG

SirGeneral at 2007-7-7 14:53:09 > top of Java-index,Archived Forums,Socket Programming...
# 14
hi,i m populating select tag by database values in jsp(struts) but my problem is each time i run application select tag list is appended by wat it previously had!kindly revert back.......waiting 4 ur replies!!thanks in advance
lil_gayu at 2007-7-7 14:53:09 > top of Java-index,Archived Forums,Socket Programming...
# 15
Clear your collection object before fetching the data from the database.SirG
SirGeneral at 2007-7-7 14:53:11 > top of Java-index,Archived Forums,Socket Programming...
# 16

hi,

if i initialise it to null then nothing is displayed in select box...

i hav tried a lot......

my action class is...

public class loginAction extends Action {

private String SUCCESS = null;

private DataSource dataSource = null;

public ArrayList doctorList = new ArrayList();

public ArrayList patientList = new ArrayList();

private ArrayList getdoctors(){

Connection conn = null;

Statement stmt = null;

PreparedStatement prpStmt = null;

ResultSet rs = null;

StringBuffer resultString ;

try{

//data sourse has been set in struts_config.xml

dataSource = (DataSource)servlet.getServletContext().getAttribute("doctorLogin");

conn = dataSource.getConnection();

String sqlQuery = "SELECT docName FROM doctorLogin";

prpStmt = conn.prepareStatement(sqlQuery);

rs = prpStmt.executeQuery();

while (rs.next()) {

//row.java is maintained which has name as private member

doctorList.add(new row(rs.getString(1)));

}

rs.close();

} catch ( SQLException e ) {

System.err.println("SQL Exception occured while accessing the table" );

e.printStackTrace();

return null;

} catch ( Exception e ) {

e.printStackTrace();

return null;

}

return doctorList;

}

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)

throws Exception {

HttpSession session = request.getSession();

/** Here the method that connects to the datasource is called: */

SUCCESS = "patLogin";

doctorList = getdoctors() ;

if(doctorList != null){

session.setAttribute("allDoctors", doctorList);

}

return (mapping.findForward(SUCCESS));

}

}

kindly revert back......

thanks in advance...

lil_gayu at 2007-7-7 14:53:11 > top of Java-index,Archived Forums,Socket Programming...