mvc模式中mystruts.xml的解析案例

3 篇文章 0 订阅

1.mystrust.xml的编写

<?xml version="1.0" encoding="UTF-8"?>
<mystruts>
	<actions>
		<action name="register" class="com.is102.RegisterAction">
			<result name="success">page/success.jsp</result>
			<result name="input">page/register.jsp</result>
			<result name="error">page/register.jsp</result>
		</action>
		<action name="login" class="com.is102.LoginAction">
			<result name="success" redirect="true">page/register.jsp</result>
			<result name="input">page/result.jsp</result>
			<result name="error">page/login.jsp</result>
		</action>
	</actions>
</mystruts>


2.在java新建一AactionMappingManage类进行测试


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

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

/**
 * 读取mystruts的actions 进行管理
 * @author Wang
 *
 */
public class ActionMappingManager {
	
	private static Map<String , ActionMapping>  actionMappings=new HashMap<String, ActionMapping>();
	
	public ActionMappingManager(){};
	public ActionMappingManager(String [] configureFileName){
		 init(configureFileName[0]);
	}
	/**
	 * 用init方法加载Action配置文件
	 * @param configureFileName为文件名称
	 */
	public void init(String configureFileName){
		try{
			if(configureFileName==null||configureFileName.isEmpty()){
				throw new Exception("文件配置不能为空");
			}		
			InputStream is =this.getClass().getResourceAsStream("/"+configureFileName);
			Document doc = new SAXReader().read(is);
			Element root =doc.getRootElement();	
			//便利所用的Action
			Iterator<Element> actionsIt =root.elements("actions").iterator();
			 Element actions =actionsIt.next();
			for (Iterator<Element> actionIt =actions.elementIterator("action");actionIt.hasNext();) {
				Element action = actionIt.next();
				/*
				 * 把Actionmapping元素封装到actionMapping对象中
				 */
				ActionMapping mapping = new ActionMapping();
				mapping.setName(action.attributeValue("name"));
				mapping.setClassName(action.attributeValue("class"));
				for (Iterator<Element> resultIt =action.elementIterator("result");resultIt.hasNext();) {
					Element resultElement =resultIt.next();
					String name =resultElement.attributeValue("name");
					String redirect=resultElement.attributeValue("redirect");
					String result=resultElement.getText();
					if(name==null||"".equals(name)){
						name="success";
					}
					//把result添加到 actionMapping 中
					mapping.addResult(name, result);
				}
				actionMappings.put(mapping.getName(),mapping );
			}
		
			System.out.println(actionMappings);
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 根据actionName 查询对应的actionmapping实列
	 * @param actionName
	 * @return
	 * @throws Exception
	 */
	public ActionMapping getActionMappingByName(String actionName) throws Exception{
		if(actionName==null||actionName.isEmpty()){
			return null;
		}
		ActionMapping mapping =this.actionMappings.get(actionName);
		if(mapping==null){
			throw new Exception("系统中找到不到:{"+actionName+"]请检查配置文件");
		}
		return mapping;
		
	}
	
	public static void main(String[] args) {
		ActionMappingManager  manager = new ActionMappingManager();
		manager.init("mystruts.xml");
	try {
		ActionMapping a =manager.getActionMappingByName("login");
		System.out.println(a.getName()+"==="+a.getClassName());
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	}
}

 
               更多分享:http://s.click.taobao.com/t?e=zGU34CA7K%2BPkqB07S4%2FK0CFcRfH0GoT805sipKvKrikfcaS4TTyN2G3x7NI3EXSGarCgKH1chUbWuNlKXrgQVWexYON45Y0xznSqHHCn2lNZlw%3D%3D

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值