# 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!!
# 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
# 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...