Struts标签输出Map中的内容

1、Action类

package dmeo05.action;

import java.util.LinkedHashMap;
import java.util.Map;

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 MapAction extends DispatchAction {

	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		//字符串
		Map<String, String> maps1 = new LinkedHashMap<String, String>();
		for(int i=1; i<11; i++) {
			maps1.put("key" + i, "value" + i);
		}
		
		/**
		 * 输出LabelValueBean中的值
		 */
		Map<String, LabelValueBean> maps2 = new LinkedHashMap<String, LabelValueBean>();
		for(int i=1; i<10; i++) {
			LabelValueBean lvb = new LabelValueBean();
			lvb.setLabel("00" + i);
			lvb.setValue("value" + i);
			maps2.put("00"+i, lvb);
		}
		
		
		request.setAttribute("maps1", maps1);
		request.setAttribute("maps2", maps2);
		return mapping.findForward("init");
	}
}

 

 

2、jsp输出页面

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic" %>    
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %>    
<!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>
<logic:iterate id="map" name="maps1">
	<bean:write name="map" property="key"/>  --> <bean:write name="map" property="value"/>
	<br/>
</logic:iterate>
<hr>
<logic:iterate id="map2" name="maps2">
	<logic:equal value="002" name="map2" property="key">
		<b><bean:write name="map2" property="value.label"/>  --> <bean:write name="map2" property="value.value"/></b>
	</logic:equal>
	<logic:notEqual value="002" name="map2" property="key">
		<bean:write name="map2" property="value.label"/>  --> <bean:write name="map2" property="value.value"/>
	</logic:notEqual>
	<br/>
</logic:iterate>

</body>
</html>

 

输出结果:

key1 --> value1
key2 --> value2
key3 --> value3
key4 --> value4
key5 --> value5
key6 --> value6
key7 --> value7
key8 --> value8
key9 --> value9
key10 --> value10


001 --> value1
002 --> value2
003 --> value3
004 --> value4
005 --> value5
006 --> value6
007 --> value7
008 --> value8
009 --> value9

你可能感兴趣的:(apache,html,bean,struts,servlet)