aspose file tools*
The moose likes Struts and the fly likes Value returned from a select tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Value returned from a select tag" Watch "Value returned from a select tag" New topic
Author

Value returned from a select tag

Mattia Merenda
Ranch Hand

Joined: Dec 14, 2006
Posts: 47
Hi everybody,
I have this select tag in the form Page1.jsp:
<html:select name="joblogs" property="groups" styleClass="dropdown" size="1" >
<html ption value="">ALL</html ption>
<html ptions name="joblogs" property="list" />
</html:select>
And I have this select tag in the form Page2.jsp:
<html:select name="joblogs" property="users" styleClass="dropdown" size="1" >
<html ption value="">ALL</html ption>
<html ptions name="joblogs" property="list2" />
</html:select>
where list and list2 call the ArrayList methods getList() and getList2() in my bean Joblogs.java.
I need to show with getList2() method the users of the group selected in the select tag in Page1.jsp.
In the same bean Joblogs.java I use the getters and setters methods for the groups and the users.
I add the code of getList() and getList2():

public ArrayList getList() {
String sqlgroups;
ArrayList groupsList = new ArrayList();
try {
// Load the Driver class file
Class.forName("com.mysql.jdbc.Driver");
Connection con1 = DriverManager.getConnection("jdbc:mysql://Hostname:3306/DBNAME?user=USER&password=PASSWORD");
//verifichiamo che l'username sia unico
sqlgroups = "SELECT DISTINCT(groups) FROM joblogs";
// Create the statement
PreparedStatement statement1 = con1.prepareStatement(sqlgroups);
ResultSet rs1 = statement1.executeQuery();
while (rs1.next()) {
groupsList.add(rs1.getString(1));
}
return groupsList;
}
.... }

public ArrayList getList2() {
ArrayList usersList = new ArrayList();
String sqlusers;
String group = getGroups();

try {
// Load the Driver class file
Class.forName("com.mysql.jdbc.Driver");
Connection con1 = DriverManager.getConnection("jdbc:mysql://Hostname:3306/DBNAME?user=USER&password=PASSWORD");

sqlusers = "SELECT DISTINCT(user) FROM joblogs WHERE (groups LIKE "+"'%" +group+ "%'"+")";
// Create the statement
PreparedStatement statement1 = con1.prepareStatement(sqlgroups);
ResultSet rs1 = statement1.executeQuery();
while (rs1.next()) {
usersList.add(rs1.getString(1));
}
return usersList;
} ...}
The first method works fine.The second method does not return what I want.I think the problem is getGroups return a null value and it does not return the value selected in the select tag in Page1.jsp.

How to solve this problem?Any advice?
Thanks,Mattia.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I don't really like the idea of opening database connections and executing queries directly in the action form, but that is a separate matter. If the problem is that the property "group" is null when getList2 is called, then you need to solve that problem. If you are just guessing at the problem then I suggest that you spend a few cycles learning how to view your program in a debugger. If you would like help figuring out why group is null then posting your action mappings and relevant action code would help.

- Brent
Mattia Merenda
Ranch Hand

Joined: Dec 14, 2006
Posts: 47
Hi everybody,
I solved this problem.I will explain it here,in this way,I hope to help anybody else.
The method getList is the same.
The method getList2 must be changed in this way in the JavaBean:
public ArrayList getList2(String group) {
ArrayList usersList = new ArrayList();
String sqlusers;
try {
// Load the Driver class file
Class.forName("com.mysql.jdbc.Driver");
Connection con1 = DriverManager.getConnection("jdbc:mysql://Hostname:3306/DBNAME?user=USER&password=PASSWORD");

sqlusers = "SELECT DISTINCT(user) FROM joblogs WHERE (groups LIKE "+"'%" +group+ "%'"+")";
// Create the statement
PreparedStatement statement1 = con1.prepareStatement(sqlgroups);
ResultSet rs1 = statement1.executeQuery();
while (rs1.next()) {
usersList.add(rs1.getString(1));
}
return usersList;
} ...}

In Page1.jsp the select tag has the same code.The Action class for the Page1.jsp is it called Groups.java.It must have this code:

public class Groups extends Action {
private final static String SUCCESS = "success";
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
JoblogsActionForm j = (JoblogsActionForm) form;
HttpSession session = request.getSession(true);
session.setAttribute("test",j.getJoblogs().getList2(request.getParameter("groups")));
return mapping.findForward(SUCCESS);
}}

In this way,the value selected in the select tag is passed as String at getList2 method and the results are saved in a collection called test.
To show the value of the users that are in the group selected it needs to modify the select tag in page2.jsp in this way:
Mattia Merenda
Ranch Hand

Joined: Dec 14, 2006
Posts: 47
<html:select name="joblogs" property="users" styleClass="dropdown" size="1" >
<html ption value="">ALL</html ption>
<html ptions name="test" />
</html:select>
In this way it si working very fine.
Thanks,
Mattia
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Value returned from a select tag