现在的位置: 首页 > 综合 > 正文

struts tree 结构

2013年05月21日 ⁄ 综合 ⁄ 共 2605字 ⁄ 字号 评论关闭

http://localhost:8080/SSHLearning/tags/ssh_tree.action

 struts.xml

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>

<package name="com.ssh.tags" extends="struts-default" namespace="/tags">

	<action name="ssh_*" class="com.ssh.actions.ExampleAction" method="{1}">
		<result name="success">/WEB-INF/pages/tags/{1}.jsp</result>
	</action>
	
 </package>
 
</struts>    

 bean

package com.ssh.beans;
import java.util.List;


public class AreaInfo {

	private int id;
	private String areaName;
	private String phoneNo;
	private String postCode;

	private List<AreaInfo> areas;
	
	
	public AreaInfo(int id, String areaName) {
		super();
		this.id = id;
		this.areaName = areaName;
	}
	public AreaInfo() {
	}
	public AreaInfo(int id, String areaName, String phoneNo, String postCode) {
		super();
		this.id = id;
		this.areaName = areaName;
		this.phoneNo = phoneNo;
		this.postCode = postCode;
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getAreaName() {
		return areaName;
	}
	public void setAreaName(String areaName) {
		this.areaName = areaName;
	}
	public List<AreaInfo> getAreas() {
		return areas;
	}
	public void setAreas(List<AreaInfo> areas) {
		this.areas = areas;
	}
	public String getPhoneNo() {
		return phoneNo;
	}

	public void setPhoneNo(String phoneNo) {
		this.phoneNo = phoneNo;
	}

	public String getPostCode() {
		return postCode;
	}

	public void setPostCode(String postCode) {
		this.postCode = postCode;
	}
}

 

Action

package com.ssh.actions;

import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.ssh.beans.AreaInfo;

public class ExampleAction extends ActionSupport {
	
	public String tree(){

		List<AreaInfo> listPro = new ArrayList<AreaInfo>();// 省集合

		AreaInfo pro = new AreaInfo(1, "福建");// 省
		List<AreaInfo> listCitys = new ArrayList<AreaInfo>();// 省对应的城市集合
		listCitys.add(new AreaInfo(10, "福州"));
		listCitys.add(new AreaInfo(11, "厦门"));
		listCitys.add(new AreaInfo(12, "泉州"));
		pro.setAreas(listCitys);// 将城市添加到省里
		listPro.add(pro);// 将省添加到省集合里

		pro = new AreaInfo(2, "江西");// 省
		listCitys = new ArrayList<AreaInfo>();// 省对应的城市集合
		listCitys.add(new AreaInfo(20, "南昌"));
		listCitys.add(new AreaInfo(21, "九江"));
		listCitys.add(new AreaInfo(22, "赣州"));
		pro.setAreas(listCitys);// 将城市添加到省里
		listPro.add(pro);// 将省添加到省集合里

		ActionContext.getContext().put("listPro", listPro);
		
		return SUCCESS;
	}
}

jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ taglib uri="/struts-dojo-tags" prefix="sx"%>

<html>
	<head>
		<sx:head />
	</head>

	<body>
		<s:iterator value="#listPro">
			<sx:tree label="%{areaName}">
				<s:iterator value="%{areas}">
					<sx:treenode label="%{areaName}"></sx:treenode>
				</s:iterator>
			</sx:tree>
		</s:iterator>
	</body>
</html>

 

抱歉!评论已关闭.