一、在List中放置自定义的Bean
1、定义一个MeicdNameForm.java
package com.sunrex.demo02.form; import org.apache.struts.action.ActionForm; public class MeicdNameForm extends ActionForm{ private String meicd; public String getMeicd() { return meicd; } public void setMeicd(String meicd) { this.meicd = meicd; } }
?
2、定义MeicdNameAction.java类
package com.sunrex.demo02.action; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; import com.sunrex.demo02.bean.MeicdBean; public class MeicdNameAction extends DispatchAction { @Override protected ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { return mapping.findForward("index"); } public ActionForward select(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { List<MeicdBean> meicds = new ArrayList<MeicdBean>(); for(int i=0; i<10; i++) { MeicdBean mb = new MeicdBean(); mb.setMeicd("11"+i); mb.setMeinm("店名-->" + i); mb.setMeicdAndMeinm(mb.getMeicd() + ":" + mb.getMeinm()); meicds.add(mb); } request.setAttribute("items", meicds); return mapping.findForward("select"); } }
?3、定义MeicdBean.java
package com.sunrex.demo02.bean; public class MeicdBean { private String meicd; private String meinm; private String meicdAndMeinm; public String getMeicdAndMeinm() { return meicdAndMeinm; } public void setMeicdAndMeinm(String meicdAndMeinm) { this.meicdAndMeinm = meicdAndMeinm; } public String getMeicd() { return meicd; } public void setMeicd(String meicd) { this.meicd = meicd; } public String getMeinm() { return meinm; } public void setMeinm(String meinm) { this.meinm = meinm; } }
?
4、index.jsp
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>Insert title here</title> </head> <body> <a href="select.do?method=select&meicd=116">go</a> </body> </html>
?5、select.jsp
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <%@ taglib prefix="html" uri="http://struts.apache.org/tags-html"%> <%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%> <%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>Insert title here</title> </head> <body> <html:form action="/select.do"> <html:select property="meicd"> <html:options property="meicd" collection="items" labelProperty="meicdAndMeinm"/> </html:select> </html:form> </body> </html>
?
注意:<html:select property="meicd">与MeicdNameForm.java中的meicd属性对应。
点击index.jsp页面的go后,运行结果是:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>Insert title here</title> </head> <body> <form name="selectForm" method="post" action="/Struts1_002/select.do"> <select name="meicd"><option value="110">110:店名-->0</option> <option value="111">111:店名-->1</option> <option value="112">112:店名-->2</option> <option value="113">113:店名-->3</option> <option value="114">114:店名-->4</option> <option value="115">115:店名-->5</option> <option value="116" selected="selected">116:店名-->6</option> <option value="117">117:店名-->7</option> <option value="118">118:店名-->8</option> <option value="119">119:店名-->9</option></select> </form> </body> </html>
二、利用Struts中自带的Bean
1、定义Action
package com.sunrex.demo03.action; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; import org.apache.struts.util.LabelValueBean; public class HtmlSelect_Options extends DispatchAction { public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { List<LabelValueBean> list = new ArrayList<LabelValueBean>(); for(int i=0; i<10; i++) { list.add(new LabelValueBean(i+"", ("<font color='red'>计算机_"+i+"_班</font>"))); } request.setAttribute("lists", list); return mapping.findForward("init"); } }
?2、jsp页面的处理
?
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <%@ taglib prefix="html" uri="http://struts.apache.org/tags-html"%> <%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%> <%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>Insert title here</title> </head> <body> <html:form> <html:select property="className"> <html:options collection="lists" property="label" labelProperty="value" filter="false"/> </html:select> </html:form> </body> </html>
?