File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Problem with a select tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Problem with a select tag" Watch "Problem with a select tag" New topic
Author

Problem with a select tag

Mattia Merenda
Ranch Hand

Joined: Dec 14, 2006
Posts: 47
Hi everybody,
I have to use in a JSP page a form with 2 select tags.
The select tags should get their values from 2 different queries.These queries ask the same table of database but 2 different fields.The first query should work like:
"SELECT DISTINCT(field1) FROM table)".
The second query should work like:
"SELECT DISTINCT(field2) FROM table".
How can I make this?Should I use 2 different struts Action?Does anybody know some example?
Of course,I already wrote the getter and setter methods in the Action Form class.I also need to know if I have to write something special in struts-config.xml file.
I need some example before Tuesday if it is possible.
Thanks in advance,
Mattia
Santosh Ramachandrula
Ranch Hand

Joined: Apr 04, 2004
Posts: 252
One Action class should be enough.

Action can talk to the Model, set the return Collection from the two queries to two different scope variables and use them in your jsp.


Thanks,
Santosh
Santosh Ramachandrula
Ranch Hand

Joined: Apr 04, 2004
Posts: 252
Hi Mattia, please post all your comments here so that others can also help you instead of sending private messages/emails thanks.

The code you sent is right except for a simple change instead of two session variables you can use one.



Mattia Merenda
Ranch Hand

Joined: Dec 14, 2006
Posts: 47
Hi everybody,
I've tried something like this in the execute() method with the changes of Santosh in my action:

HttpSession session = request.getSession(true);

if (getField1() != null)
session.setAttribute("field1List",getField1());
if (getField2() != null)
session.setAttribute("field2List",getField2());
return mapping.findForward(SUCCESS);

where getField1 and getField2 are the methods that ask the database.
This is the main part of the method getField1 that returns an ArrayList:

public ArrayList getField1() {
String sql = "SELECT DISTINCT(field1) FROM table";
ArrayList field1List = new ArrayList();
try {

Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://hostname:3306/DBname?user=username&password=password");

statement = con.prepareStatement(sql);
rs = statement.executeQuery();
while (rs.next()) {
field1List.add(rs.getString(1));
}
return field1List;
}
catch ( SQLException e){......}
}
The method getField2 has a similar code.
I used this code in my JSP page:
<th><bean:message key="prompt.field1" /></th>
<logic:notPresent name = "field1List">
<h2>Field1 not in scope!</h2>
</logic:notPresent>
<logic resent name="field1List">
<th>
<html:select property="field1">
<html ptions collection="field1List" property="value" labelProperty="label"/>
</html:select></th>
</logic resent>

I don't know why,but I get the message for <logic:notPresent> tag that I don't want to see.Where is the error?
Thanks,
Mattia
Mattia Merenda
Ranch Hand

Joined: Dec 14, 2006
Posts: 47
Hi everybody,
I've tried something like this in the execute() method with the changes of Santosh in my action:

HttpSession session = request.getSession(true);

if (getField1() != null)
session.setAttribute("field1List",getField1());
if (getField2() != null)
session.setAttribute("field2List",getField2());
return mapping.findForward(SUCCESS);

where getField1 and getField2 are the methods that ask the database.
This is the main part of the method getField1 that returns an ArrayList:

public ArrayList getField1() {
String sql = "SELECT DISTINCT(field1) FROM table";
ArrayList field1List = new ArrayList();
try {

Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://hostname:3306/DBname?user=username&password=password");

statement = con.prepareStatement(sql);
rs = statement.executeQuery();
while (rs.next()) {
field1List.add(rs.getString(1));
}
return field1List;
}
catch ( SQLException e){......}
}
The method getField2 has a similar code.
I used this code in my JSP page:
<th><bean:message key="prompt.field1" /></th>
<logic:notPresent name = "field1List">
<h2>Field1 not in scope!</h2>
</logic:notPresent>
<logic resent name="field1List">
<th>
<html:select property="field1">
<html ptions collection="field1List" property="value" labelProperty="label"/>
</html:select></th>
</logic resent>

I don't know why,but I get the message for <logic:notPresent> tag that I don't want to see.Where is the error?
Thanks,
Mattia
Santosh Ramachandrula
Ranch Hand

Joined: Apr 04, 2004
Posts: 252
HI try adding scope="session" in your logic resent and logic:notPresent tags that is because you are using session scope.

<logic resent name="list" scope="session">

</logic resent>

<logic:notPresent name="list" scope="session">
</logic:notPresent>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with a select tag