wood burning stoves 2.0*
The moose likes Struts and the fly likes Retrieving database values inside the <s:select> tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Retrieving database values inside the <s:select> tag" Watch "Retrieving database values inside the <s:select> tag" New topic
Author

Retrieving database values inside the <s:select> tag

piyush sah
Greenhorn

Joined: Apr 11, 2011
Posts: 12
Hello everyone,
I am able to retrieve database values in my drop down box but not in the manner I want.
1. My employee.jsp is as follows:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<s:form action="AddStudent">
<table border="1" width="80%">
<tr>

<td><s:textfield name="Id" id="Id" value="%{SautoId}" key="SId"/></td>
</tr>
<tr>

<td><s:textfield name="name" id="name" key="SName"/></td></tr>
<tr>

<td><s:textfield name="course" id="course" key="Course" /></td>
<s:select name="DId" value="%{DId}" list="result2"
listkey="DId" label="Designation" >

</s:select>
</tr>
<tr>
<td><s:submit value="ADD"align="center" /></td>
</tr>
</table>
<s:a href="Display.jsp">DISPLAY PAGE</s:a>
</s:form>
</body>
</html>


2. I am getting a list with object values like : com.project.model.StudentVO@7a7a30
com.project.model.StudentVO@2a6a65
com.project.model.StudentVO@4bv67 etc... etc... But I get The exact no. of objects that are there in the datatbase

3. My problem is I am not getting the desired values that are in the database like DES001,DES002,DES003 ....etc...etc...

4. My Action class snippet is as follows :
public String cmbPopulate()
{

IntfStudentBO desgObj = new StudentBO();
result2= desgObj.cmboDid(student1);

if(result2!=null)
{
return SUCCESS;

}
else
{
return ERROR;
}
}



5.My business class snippet :

public List<StudentVO> cmboDid(StudentVO student1) {
List<StudentVO> result= new ArrayList<StudentVO>();
IntfStudentDAO studentDAO = new StudentDAO();
result = studentDAO.cmboDid(student1);
return result;
}
6. My DAO class snippet

public List<StudentVO> cmboDid(StudentVO student1) {
try {
DBConnection dbConnection = new DBConnection();
System.out.println("DATABASE CONNECTED");
try
{

String str= "select D_Id from designation";
String des="";
PreparedStatement ps = dbConnection.getConn().prepareStatement(str);
ResultSet rs =null;
rs=ps.executeQuery();
StudentVO d1;
while (rs.next()) {
d1=new StudentVO();
d1.setDId(rs.getString(1));
des=d1.getDId();
System.out.println("Values of des :"+des);
Slist.add(d1);
}
}
finally{
dbConnection.closeConnection();
}
} catch (SQLException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (NamingException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}

return Slist;
}

Thanks in advance.
Regards,
Piyush N.Sah
Pankaj Shet
Ranch Hand

Joined: Sep 08, 2006
Posts: 219

Hi,

There is a small mistake here in your DAO class




Instead of

Slist.add(d1);
where you are adding the Objects inside the list,

Do
Slist.add(des);
Add values inside the list.


Thanks
-Pankaj Shet

PANKAJ SHET
B.Sc.(I.T.), S.C.J.P., S.C.W.C.D., PGDAC(CDAC)
piyush sah
Greenhorn

Joined: Apr 11, 2011
Posts: 12
Thank You Pankaj for such a quick reply(highly appreciated).But right after I posted this I came up with the solution. & the solution lies in the OGNL...The only slight change that i did to resolve the issue is as followed :
<s:select label="Designation" name="DId" list="result2"
listkey="DId" listValue="DId" value="%{result2.{DId}}" >
</s:select>


P.S. The listValue is DId which is the same as variable DId of my bean class.If The listValue is not entered we'll get no value insise the dropdown.
Pankaj Shet
Ranch Hand

Joined: Sep 08, 2006
Posts: 219

great dude
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Retrieving database values inside the <s:select> tag
 
Similar Threads
Struts 2 convertion type
form visibility
how to display data from database on jsp using struts2
[Struts 2] How to populate select control after selection of a value in another select control
passing form parameters such us textfield values using url tag