Struts2 的Action 命名重复检测

本文介绍了一段用于检测Struts2配置文件中Action名称重复的代码实现,通过遍历XML配置文件,统计并判断是否存在重复的Action名称,以避免运行时的潜在错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原因:在实际项目中发现 <action /> 的 name 重复时候,Struts2 并不会报错而是随意找一个去执行!

为了避免重复的情况发生,特地写了一个检测的程序:

package barcode;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *创建日期	:2012-10-22
 *创建用户	:GongQiang
 *变更情况	:
 *文档位置 	$Archive:NTT-DATA//ActionNameRepeatCheck.java$
 *最后变更	$Author:  $
 *变更日期	$Date: $
 *当前版本 	$Revision: $
 *
 *Copyright (c) 2004 Sino-Japanese Engineering Corp, Inc. All Rights Reserved.
 */

public class ActionNameRepeatCheck {

	/**
	 * @param args
	 *
	 * Date	  :2012-10-22
	 * Author :GongQiang
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		Strut2Xml strut2Xml = new Strut2Xml();
		strut2Xml.parserXml( "struts.xml" );
	}

}

interface XmlDocument { 
	/** * 建立XML文档 
	 *@param fileName 文件全路径名称 
	 */ 
	public void createXml(String fileName);
	
	/** * 解析XML文档 
	 * @param fileName 文件全路径名称 
	 */ 
	public void parserXml(String fileName); 
}  

class Strut2Xml implements XmlDocument{

	Document document = null;
	
	private Map<String,Integer> map = new HashMap<String,Integer>();
	
	/**
	 * 初始化
	 * @param fileName
	 *
	 * Date	  :2012-10-22
	 * Author :GongQiang
	 */
	private void init( String fileName )
	{
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
		DocumentBuilder db;
		try {
			db = dbf.newDocumentBuilder();
			InputStream in = getClass().getClassLoader().getResourceAsStream(fileName);
			document = db.parse(in); 
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		
	}
	
	public void createXml(String fileName) {
		throw new UnsupportedOperationException( "不支持此操作" );
		
	}

	public void parserXml(String fileName) 
	{
		init( fileName );
		// 如果包含<package/> 和 <action/>元素则统计
		statistics( fileName );
		
		// 如果包含<include/> 则统计外部文件的
		parseInclude(fileName);
	}
	
	/**
	 * 对<package/> 下的 <action/>进行统计
	 * @param fileName
	 *
	 * Date	  :2012-10-22
	 * Author :GongQiang
	 */
	private void statistics( String fileName )
	{
		Element root = document.getDocumentElement();
		NodeList list = root.getChildNodes();
		for( int i=0 ; i<list.getLength() ; i++ )
		{
			Node node = list.item(i);
			if( "package".equals(node.getNodeName()) )
			{
				String namespace = node.getAttributes().getNamedItem("namespace").getNodeValue();
				NodeList actions = node.getChildNodes();
				for( int j=0 ; j<actions.getLength() ; j++ )
				{
					Node action = actions.item(j);
					if( "action".equals( action.getNodeName() ) )
					{
						String actionName = action.getAttributes().getNamedItem("name").getNodeValue();
						Integer count = map.get( namespace + "/" + actionName );
						if( count == null )
						{
							map.put(namespace + "/" + actionName, 1);
						}
						else
						{
							map.put(namespace + "/" + actionName, ++count);
						}
					}
				}
				
				// 打印<package/> 下的统计信息
				boolean hasRepeat = false;
				for( String key : map.keySet() )
				{
					Integer count = map.get( key );
					if( count != 1 )
					{
						hasRepeat = true;
					}
					System.out.println( key + "-->" + count);
				}
				if( hasRepeat )
				{
					System.out.println( "<package namespace=\""+namespace+"\"/> 包含重复Action!错误!!!\n" );
				}
				else
				{
					System.out.println( "<package namespace=\""+namespace+"\"/> 不包含重复Action!正确!!!\n" );
				}
			}
		}

	}
	
	/**
	 * 统计外部文件
	 * @param fileName
	 *
	 * Date	  :2012-10-22
	 * Author :GongQiang
	 */
	private void parseInclude( String fileName )
	{
		Element root = document.getDocumentElement();
		NodeList list = root.getChildNodes();
		for( int i=0 ; i<list.getLength() ; i++ )
		{
			Node node = list.item(i);
			if( "include".equals(node.getNodeName()) )
			{
				String file = node.getAttributes().getNamedItem("file").getNodeValue();
				new Strut2Xml().parserXml( file );
			}
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值