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 new to Struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "new to Struts" Watch "new to Struts" New topic
Author

new to Struts

Bala Raju Mandala
Ranch Hand

Joined: Sep 21, 2006
Posts: 40
Hi All,

I am new to Struts. I need your help.

I need to display a list of values in selected tag. The values are from a Form Bean. I am able to display two other columns correctly, but i am getting confused using the selected field. I am adding the coding part here. Please help me in correcly using the Selected tag.

Action Form class
===============================
package code;

import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public final class LoginForm extends ActionForm {
private String name = null;
private String pwd = null;
private ArrayList coll1 = null;

private String action = "Set";

public void setName(String name) {
System.out.println("form setName " + name);
this.name = name;
}

public String getName() {
System.out.println("form getName " + name);
return name;
}

public void setPwd(String pwd) {
System.out.println("form setPwd " + pwd);
this.pwd = pwd;
}

public String getPwd() {
System.out.println("form getPwd " + this.pwd);
return pwd;
}

public void setColl(ArrayList coll) {
this.coll1 = coll;
}

public ArrayList getColl() {
return coll1;
}
}
===============================

Out Put JSP:
===============================
<%@ taglib uri="/WEB-INF/app.tld" prefix="app"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ page import="code.LoginForm"%>
<html:html>
<head>
<title>
Success!!!
</title>
</head>
<body>
<font color="blue">
Name:
<bean:write name="loginForm" property="name"/>
<br><br>
Password:
<bean:write name="loginForm" property="pwd"/>
<br><br>
<html:select name="loginForm" property="coll">
<html ptions collection="coll1" property="coll" labelProperty="coll"/>
</html:select>

</font>
</body>
</html:html>
===============================
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
You need a property on your form to hold the value of the selected item. Depending on your list, this could be about any type. I would typically use a long but some cases might be a String or other type. Since I don't know what you are getting at with the name "coll" I picked a name that made sense to me. Say the user needs to select a server, then you would add these properties to your form.

private long serverId;
private List serverValuesList;

The JSP code would then look like this. I really like using htmlptionsCollection. This is mostly because the names of the properties matches up with the other html tags.

<html:select property="serverId">
<htmlptionsCollection property="serverValuesList" value="??" label="??" />
</html:select>

As far as the value and label attributes...that depends on what type the objects you are adding to serverValuesList. If you are using LabelValueBean, then you do not need to specify these attributes (the default for value is "value" and the default for label is "label"). If I was adding more complex objects to the list, then I might have a value setting of "serverId" and a label setting of "serverName".

- Brent
[ March 12, 2008: Message edited by: Brent Sterling ]
Bala Raju Mandala
Ranch Hand

Joined: Sep 21, 2006
Posts: 40
Thank you for your reply Brent.

I am bit confused here the way i can use the Selected tag.

my requirement behind this is based on a user name i entered, we create a list and that list will be selected in select box tag.

So for that i am creating the ArryList in Action class and set the list to form bean. I am facing the problem getting this list to JSP. As i am using the selected tag, i am confused with the values i need to give to selected tag and options tag. Can you explain me what are these tags stand for, from my problem perspective?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: new to Struts