zoukankan      html  css  js  c++  java
  • Struts – Wildcards example

    Struts wildcards can helps to reduce the repetition in your struts-config.xml file, as long as your Struts project is following some regular file structure. For example, in User module, to implement the CRUD function, your struts-config.xml may look like following

    1. No Wildcards

    You need to create four action mappings for each list, add, delete and update function, and a lot of repetition.

    struts-config.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts-config PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
    "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
     
    <struts-config>
    	
    	<action-mappings>
    
    	 	<action
    			path="/ListUserAction"
    			type="com.mkyong.common.action.UserAction"
    			parameter="ListUser"
    			>
     
    			<forward name="success" path="/pages/ListUser.jsp"/>
     
    		</action>
    		
    		<action
    			path="/AddUserAction"
    			type="com.mkyong.common.action.UserAction"
    			parameter="AddUser"
    			>
     
    			<forward name="success" path="/pages/AddUser.jsp"/>
     
    		</action>
    		
    		<action
    			path="/EditUserAction"
    			type="com.mkyong.common.action.UserAction"
    			parameter="EditUser"
    			>
     
    			<forward name="success" path="/pages/EditUser.jsp"/>
     
    		</action>
    		
    		<action
    			path="/DeleteUserAction"
    			type="com.mkyong.common.action.UserAction"
    			parameter="DeleteUser"
    			>
     
    			<forward name="success" path="/pages/DeleteUser.jsp"/>
     
    		</action>
    		
    		
    	</action-mappings>
    	
    </struts-config>
    

    2. With Wildcards

    With Struts wildcards feature, your struts-config.xml can cut down into one action mapping.

    struts-config.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts-config PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
    "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
     
    <struts-config>
    	
    	<action-mappings>
    
    	 	<action
    			path="/*UserAction"
    			type="com.mkyong.common.action.UserAction"
    			parameter="{1}User"
    			>
     
    			<forward name="success" path="/pages/{1}User.jsp"/>
     
    		</action>
    
    	</action-mappings>
    	
    </struts-config>
    

    Let’s see an use case, try access via http://localhost:8080/StrutsExample/EditUserAction.do. The “EditUserAction.do” will match the “/*UserAction” pattern, and the * matched string “Edit” is represent by {1} for later use.

    In above case, the wildcards action mapping will change from

           <action
    		path="/*UserAction"
    		type="com.mkyong.common.action.UserAction"
    		parameter="{1}User"
    	>
     
    	<forward name="success" path="/pages/{1}User.jsp"/>
     
    	</action>
    

    to

           <action
    		path="/EditUserAction"
    		type="com.mkyong.common.action.UserAction"
    		parameter="EditUser"
    	>
     
    	<forward name="success" path="/pages/EditUser.jsp"/>
     
    	</action>
    

    Conclusion

    Both struts-config.xml samples have the same functionality, but with less repetition in wildcards support. However, DO NOT overuse this wildcards feature in your project, it’s less manageable than the normal declaration.

  • 相关阅读:
    375D.Tree and Queries(树上启发式合并+离线)
    600E.Lomsat gelral (树上启发式合并)
    741D.Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(树上启发式合并+状压)
    Nowcoder13249.黑白树(树形DP)
    Nowcoder51179.选课(树形背包)
    Nowcoder20811.蓝魔法师(树形背包)
    Nowcoder19782.Tree(树形DP+逆元)
    Gym102292M.Monster Hunter(树形背包+滚动数组)
    大数据运维(61)Linux环境安装PostgreSQL-10.1
    大数据运维(60)Hive on Spark配置
  • 原文地址:https://www.cnblogs.com/ghgyj/p/4766276.html
Copyright © 2011-2022 走看看