Struts2 的Action 命名重复检测
生活随笔
收集整理的這篇文章主要介紹了
Struts2 的Action 命名重复检测
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原因:在實際項目中發現 <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 );}}} }總結
以上是生活随笔為你收集整理的Struts2 的Action 命名重复检测的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SessionHelper
- 下一篇: 轩辕传奇服务器合并信息,轩辕传奇9月1日