• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Problem with a select tag

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Santosh Ramachandrula
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
reply
    Bookmark Topic Watch Topic
  • New Topic