Select Options HTML Tags question

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.
geek4uAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
if the two have different needs then use two different pages, one to addd and the other to edit
0
objectsCommented:
To display the user details use something like:

Name: <%= user.getName() %> <br/>
Password: <%= user.getPassword() %><br/>
0
objectsCommented:
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

geek4uAuthor Commented:
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
geek4uAuthor Commented:
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
objectsCommented:
<select name ="role">
<%
   Iterator i = roles.iterator();
   while (i.next()) {
      Role role = (Role) i.next();
%>
      <option value="<%= role.getId() %>"><%= role.getName() %></option>
<%
   } %>
0
objectsCommented:
yes yoiu will need to grab the list from the database
0
geek4uAuthor Commented:
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
objectsCommented:
include it in your form, or add it to the request
0
fargoCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
geek4uAuthor Commented:
This is more clear and what i wanted. Thanks very much.
0
objectsCommented:
to add it to the request use something like:

request.setAttribute("roles", roles);
0
fargoCommented:
oh...sorry objects.

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

or to set in request, as objects said.

fargo

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.