日期:2014-05-17  浏览次数:20605 次

Strut1.x中<html:select>标签的使用

一、在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>
<hea