原因:在实际项目中发现 <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 );
}
}
}
}