来自「如何使用<html:select> 标签」· 代码 · 共 37 行

TXT
37
字号
如何使用<html:select> 标签

注意: 一定要先写好 action 和 actionFrom

这是测试actionForm.

package test;

import org.apache.struts.action.*;import javax.servlet.http.*;

public class test extends ActionForm{  private String value="2";   值为2. 将在select中自动选中该项.只做测试用.  public String getValue()  {    return value;  }  public void setValue(String value)  {    this.value = value;  }  public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest 

httpServletRequest)  {    /**@todo: finish this method, this is just the skeleton.*/    return null;  }  public void reset(ActionMapping actionMapping, HttpServletRequest 

httpServletRequest)  {  }}

以下是一个action

package test;

import org.apache.struts.action.*;import javax.servlet.http.*;

public class testAction extends Action{  public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, 

HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) 

throws Exception  {   return null;  }}

以下是一个JSP文件的示例.

<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %><%@ taglib uri="/WEB-INF/struts-template.tld" prefix="template" %><%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %><%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %><%@ page contentType="text/html; charset=iso-8859-1" %><html:html><head><title>te.jsp</title></head><body><h1>JBuilder Generated Struts JSP for ActionForm test.test</h1>

<!--  以下是一个创建一个ArrayList. 也可在DB中获取数据.  这里只是一个演示.   在程序中可以用 request 进行对象传送...--><% java.util.Collection list = new java.util.ArrayList();    for(int i=0;i<5;i++)     {      java.util.HashMap li = new java.util.HashMap();      li.put("id",i+"");              file://这里存放select选项的值.      li.put("name","name"+i);        file://这里存放select选项名称.      list.add(li);     }

     pageContext.setAttribute("myList", list);%><p><html:form action="/testAction.do" method="POST"><html:text property="value"/><br><html:select property="value"> <html:options collection="myList" property="id" labelProperty="name"/></html:select><br><html:submit property="submit" value="Submit"/><br><html:reset value ="Reset"/></html:form></body></html:html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?