STRUTS下拉列表

 

<html:select>标签生成HTML<select>元素.基本形式:

<html:select property="propertyname"multiple="true"size="6">

重要属性:

size属性:指定每次在网上显示的可选项的数目

multiple属性:指定是否支持项选,默认为false

property属性:ActionForm Bean中的某个属性对应,这个属性用来存放用户在列表上选中选项的值.单选时对应简单类型,多项时对应数组类型.


第一种直接使用把所有选项写在中间

<html:option value="0-15">0-15</html:option>
<html:option value="15-20" >15-20</html:option>
<html:option value="20-30" >20-30</html:option>
<html:option value="20 or above">30 or above</html:option>

第二种:把选项放在一个Collection中(这里使用List).在实际项目中,更多的是可能数据来源于db,文件等。这种情况用得比较多。

<html:options collection="AList" property="value" labelProperty="label"/>
把option放在list中的过程在Action中作处理
//prepare the age selector list.
List ageList =new ArrayList();
ageList.add(new LabelValueBean("0-15","0-15"));
ageList.add(new LabelValueBean("15-20","15-20"));
ageList.add(new LabelValueBean("20-30","20-30"));
ageList.add(new LabelValueBean("30 or above","30 or above"));
request.setAttribute("AList",AList);

这里使用了LabelValueBean,可以不用的,象
<html:options collection="AList" labelProperty="value" property="id" />
只要在AList中填入的bean有value和id属性就可以

第三种,把此list 作为Form 的一个属性.
<html:optionsCollection property="AList" />
在Form 中添加AList 的setter和getter. Form中作如下处理。
//the list can be a form property.
f.setAgeList(AList);

1. 从数据库中获得数据,你应该在Action里面取得数据后,将数据放到Request里面
2. 数据取出来后放在一个List或Collection或Map里面,我习惯用List
3. 从List或其它的容器中取数据应该用<html:options> 或<html:optionsCollection>
4. <html:options> 和<html:optionsCollection>外层必须用<html:select property="">,所以这个属性你必须在FormBean里定义
5. 由于你要用到这些标签,所以你必须定义FormBean
6. 从Action取数据,以List为例

List list = xxxxx;//从数据库中取得下拉列表中的数据
request.setAttribute("list",list);

在页面显示
<html:form action="xxxx">
...
<html:select property="xxx">
<html:options collection="list" labelProperty="下拉框中显示的内容,一般是name或其它相似属性" property="各选项对应的值,一般是id" />
</html:select>
...
</html:form>

补充一点点:

因为数据你要从 数据库去取, 所以一般在 action 里调用 DAO ,作为 request 的一个属性传到页面上; 这时一般用 <html:options .../> 标签

另外,如果数据不从数据库去取,而是代码固定的,则一般把这种放到 ActionForm 里,作为属性在页面上取,这时一般用 <html:optionsCollection ... />

一个实例:

1. 定义相应页面(client.jsp)的form bean,这里假设为ClientForm;注意在struts_config.xml中定义映射关系;client.jsp中包含了你需要的html form内容,比如一个select下拉框;

这里是form bean的代码(其实就是一个java bean,继承了ActionForm,然后需要重载reset和validate方法):
-----------------------------------------------
package com.egi.core.ioblock.form;

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

/**
* Copyright: Copyright (c) 2002</p> <p>
*@author sjoy
*@created 2003年6月4日
*@version 1.0
*/

public class LoginForm extends ActionForm {

//-----------------------------Instance Variable
private String appName = null;
private String type = null;

public String getAppName() {
return appName;
}

public void setAppName(String appName) {
this.appName = appName;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public void reset(ActionMapping mapping, HttpServletRequest request) {
appName = null;
type = null;
}

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {

ActionErrors errors = new ActionErrors();
if (appName == null || appName.length() < 1) {
errors.add("application name", new ActionError("error.appname.required"));
}
return errors;
}
}
-----------------------------------------------


这里是ActionServlet代码,继承Action:
-----------------------------------------------
package com.egi.core.ioblock.action;

import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import com.egi.core.ioblock.form.LoginForm;
import com.mainet.core.spreadsheet.db.MenusTreeTable;
import com.mainet.core.spreadsheet.ProjectFactory;

/**
* Copyright: Copyright (c) 2002</p> <p>
*@author sjoy
*@created 2003年6月4日
*@version 1.0
*/

public class LoginAction extends Action {

public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {

ActionErrors errors = new ActionErrors();
String appName = ((LoginForm) form).getAppName();

//下面是你所需要的一些逻辑
...
HttpSession session = request.getSession();
...

return mapping.findForward("success");
}
}

-----------------------------------------------


2. 写一个bean,专门用来保存select的option集合。代码如下:
-----------------------------------------------
package com.egi.core.ioblock.util;

import java.io.Serializable;

/**
* Description: This class is a bean, used to represent one option in an HTML
* drop-down &apos;&apos;select&apos;&apos; list. It contains two properties - see {@link
* getDisplayName()} and {@link getInternalId()} for a description. Useful in a
* struts Form class for constructing a select list to pass to the jsp with the
* <tt><html:select></tt> and <tt><html:option></tt> tags.</p> <p>
*@author sjoy
*@created 2003年6月4日
*@version 1.0
*/

public class HtmlSelectOption implements Serializable {
private String id;
private String displayName;

/**
* Constructor for the HtmlSelectOption object
*/
public HtmlSelectOption() { }

/**
* Constructor for the HtmlSelectOption object
*
*@param id Description of the Parameter
*@param displayName Description of the Parameter
*/
public HtmlSelectOption(String id, String displayName) {
this.id = id;
this.displayName = displayName;
}

public String getDisplayName() {
return displayName;
}

public void setDisplayName(String displayName) {
this.displayName = displayName;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}
}
-----------------------------------------------

3. ok,接下来从db或者其它地方去取下拉列表中的具体内容;
java.util.Iterator iter = ....;//这里假设从数据库中取得数据
java.util.ArrayList list = new java.util.ArrayList();
String obj;
while(iter.hasNext()){
obj = (String)iter.next();
list.add(new com.egi.core.ioblock.util.HtmlSelectOption(obj,obj));
}
pageContext.setAttribute("appNames", list);

注意:这段逻辑也可以写在ClienetForm中通过javabean的方式在页面上获得这个集合。

4. 然后就是页面上使用啦:)
<html:select property="type">
<html:options collection="appNames" property="id"
labelProperty="displayName"/>
</html:select>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值