实例模拟struts核心流程

当前位置 : 首页 > 网页制作 > CSS > 实例模拟struts核心流程

实例模拟struts核心流程

来源: 作者: 时间:2016-01-28 09:27
span style=font-family:Microsoft YaHei; font-size:18px?xml version=1 0 encoding=UTF-8?action-configaction path = addUser action type = com tigaoban manager AddUserActio
<span style="font-family:Microsoft YaHei; font-size:18px"><?xml version="1.0" encoding="UTF-8"?>
<action-config>
	<action path = "/addUser.action" type = "com.tigaoban.manager.AddUserAction">
		<forward name = "success">/addSuccess.</forward>
		<forward name = "error">/addError.jsp</forward>
	</action>
	
	<action path = "/delUser.action" type = "com.tigaoban.manager.DelUserAction">
		<forward name = "success">/delSuccess.jsp</forward>
		<forward name = "error">/delError.jsp</forward>
	</action>
	
	<action path = "/modifyUser.action" type = "com.tigaoban.manager.ModifyUserAction">
		<forward name = "success">/modifySuccess.jsp</forward>
		<forward name = "error">/modifyError.jsp</forward>
	</action>
	
	<action path = "/queryUser.action" type = "com.tigaoban.manager.QueryUserAction">
		<forward name = "success">/querySuccess.jsp</forward>
		<forward name = "error">/queryError.jsp</forward>
	</action>
</action-config>
</span>
<span style="font-family:Microsoft YaHei; font-size:18px">package com.tigaoban.domain;

import java.util.Map;

public class ActionMapping {
	private String path;
	
	private String type;
	
	private Map<String, String> forwardMap;

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Map<String, String> getForwardMap() {
		return forwardMap;
	}

	public void setForwardMap(Map<String, String> forwardMap) {
		this.forwardMap = forwardMap;
	}
}
</span>
<span style="font-family:Microsoft YaHei; font-size:18px">package com.tigaoban.util;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.tigaoban.domain.ActionMapping;



/**
 * 采用单例模式解析ActionConfig.xml文件
 * @author Haitao
 *
 */
public class XmlConfigReader {
	
	
	//懒汉式
	private static XmlConfigReader instance = null;//XmlConfigReader 实例
	
	
	
	//key=名称,value = 具体的动作实体类
	private Map<String, ActionMapping> actionMap = new HashMap<String, ActionMapping>();
	
	/**
	 * 构造方法
	 */
	private XmlConfigReader(){
		SAXReader reader = new SAXReader();
		InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("ActionConfig.xml");
		
		try {
			Document doc = reader.read(in);
			
			//取得Action相关的配置信息,并存入ActionMapping实体中
			List<Element> actionList = doc.selectNodes("//action");
			for (Iterator<Element> iter = actionList.iterator(); iter.hasNext();){
				Element element = iter.next();
				
				String path = element.attributeValue("path");
				String type = element.attributeValue("type");
				//System.out.println(path);
				
				List<Element> forwardList = element.elements("forward");
				//存放forward中信息的map
				Map<String,String> forwardMap = new HashMap<String, String>();
				
				for (Iterator<Element> iterator = forwardList.iterator();iterator.hasNext();){
					Element forward = iterator.next();
					String name = forward.attributeValue("name");
					String value = (String)forward.getData();
					forwardMap.put(name, value);
				}
				
				//新建ActionMapping实体
				ActionMapping actionMapping = new ActionMapping();
				actionMapping.setPath(path);
				actionMapping.setType(type);
				actionMapping.setForwardMap(forwardMap);
				
				//将actionMapping放入map中
				actionMap.put(path, actionMapping);
				
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}	
	}
	
	public static synchronized XmlConfigReader getInstance() {
		if(instance == null){
			instance = new XmlConfigReader();
		}
		return instance;
	}
	
	/**
	 * 取得action相关配置
	 * @return
	 */
	public Map<String, ActionMapping>  getMap() {
		return actionMap;
	}
	
	
}	</span>

 

Tag:
网友评论

<