Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Select Options HTML Tags question

Posted on 2006-03-09
13
Medium Priority
?
266 Views
Last Modified: 2010-03-31
Hi,

I have coded lil bit in struts but i am not very good at it. I need help in how do the following:

I have developed a Add User(JSP) page where the username,password and userrole are just text fields. Once entered the data is directed to Action Class where it is stored in a database table.

Now what i need to do is modify the same page a little, i.e., the user roles need to be pulled from a database table and shown in a drop down list on the initial JSP for the user to select from one of them.

I know that options or options collection can be used. But i am not clear yet as to how i can display the data from the database in the initial JSP i.e, AddUser JSP.

Please help,

Thnaks.
0
Comment
Question by:geek4u
  • 7
  • 4
  • 2
13 Comments
 
LVL 92

Expert Comment

by:objects
ID: 16149021
if the two have different needs then use two different pages, one to addd and the other to edit
0
 
LVL 92

Expert Comment

by:objects
ID: 16149037
To display the user details use something like:

Name: <%= user.getName() %> <br/>
Password: <%= user.getPassword() %><br/>
0
 
LVL 92

Expert Comment

by:objects
ID: 16149042
or if they are edittable display them in a field

Name: <input name="name"><%= user.getName() %></input>

user is the bean containing your user details
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:geek4u
ID: 16149060
Not two different needs. I mean to say that when admin wants to add the new user i want to present him with a list of possible userroles which he can assign that new user.
0
 

Author Comment

by:geek4u
ID: 16149104
i need to have this list in the initial page itself.

So should i have to write an action class which connects to the database, gets the roles and populates the JSP form which is displayed to the admin.
0
 
LVL 92

Expert Comment

by:objects
ID: 16149118
<select name ="role">
<%
   Iterator i = roles.iterator();
   while (i.next()) {
      Role role = (Role) i.next();
%>
      <option value="<%= role.getId() %>"><%= role.getName() %></option>
<%
   } %>
0
 
LVL 92

Expert Comment

by:objects
ID: 16149125
yes yoiu will need to grab the list from the database
0
 

Author Comment

by:geek4u
ID: 16149156
so how can the list be returned from the action class to the JSP. I mean i know how to connect to the database, i can change the struts-config.xml to first direct the admin to the action class from there to the JSP. But my point is how to send the list from action class to JSP and how to use it in the JSP?


I guess that list is "roles" in your sample code above.
0
 
LVL 92

Expert Comment

by:objects
ID: 16149164
include it in your form, or add it to the request
0
 
LVL 11

Accepted Solution

by:
fargo earned 1600 total points
ID: 16149204
Hi,

In the action class u can get the drop down list as follows

// Model class code say i call it Roles

public class Roles{

public List getRoles(){

String query="SELECT role AS label, role AS value FROM USER_ROLES";
// execute the query
// get the results back
// put it in LabelValueBean from struts and create a list of it

List rolesList = new ArrayList();
// iterate over the resultset and populate the below code
rolesList.add(new LabelValueBean(rs.getString("label"), rs.getString("value")));

return rolesList;
}
}

// In the action class

Roles roles = new Roles();
List list = new ArrayList();
list = roles.getRoles();

//in the action class only, put the list in session or request
request.getSession("rolesList",list);

// FINALLY in the jsp
<html:select name="addUserForm" property="role" >
         <html:options collection="rolesList" property="value" labelProperty="label" />
</html:select>

Hope this helps.
fargo
0
 

Author Comment

by:geek4u
ID: 16149247
This is more clear and what i wanted. Thanks very much.
0
 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
ID: 16149254
to add it to the request use something like:

request.setAttribute("roles", roles);
0
 
LVL 11

Expert Comment

by:fargo
ID: 16149284
oh...sorry objects.

>> request.getSession("rolesList",list);
request.getSession().setAttribute("rolesList",list);

or to set in request, as objects said.

fargo

0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month15 days, 7 hours left to enroll

575 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question