struts之XML配置文件歸納

1.首先是src目錄下的總配置文件struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

	<!-- 二、總配置文件:引入其他所有配置文件 -->
	
	<include file="constant.xml"></include>
	<include file="cn/sp/a_config/struts.xml"></include>
	<include file="cn/sp/b_config2/config.xml"></include>
	<include file="cn/sp/c_data/data.xml"></include>
	<include file="cn/sp/d_type/type.xml"></include>
	<include file="cn/sp/e_fileupload/upload.xml"></include>
	
</struts>

這種一般是引入其他包裏的配置文件。

2.然後是配置文件的通用模板

<span style="font-size:14px;"><struts>
 <package name="config2" namespace="/" extends="struts-default">
  <!-- 配置全局跳轉視圖 -->
  <global-results>
   <result name="success">/index.jsp</result>
  </global-results>
  <!-- 通配符: </span><a target=_blank href="http://locahost:8080/struts02/user_login"><span style="font-size:14px;">http://locahost:8080/struts02/user_login</span></a><span style="font-size:14px;"> -->
  <action name="user_*" class="cn.itcast.b_config2.UserAction" method="{1}">
   <result name="loginSuccess" >list.jsp</result>
  </action></span>
<span style="font-size:14px;">  <action name="test2">
    <result name="success" type="redirect">/WEB-INF/index.jsp</result>
   </action></span>
<span style="font-size:14px;"> </package> 
 
</struts></span>

type默認爲dispatcher即轉發,如要改爲重定向需寫出來,同時還可以重定向到Action,type="redirectAction".

3.接着是使用struts框架自帶的一些功能時涉及到的一些配置文件。

3.1常量配置

<struts>

	<!-- 一、全局配置 -->
	<!-- 0. 請求數據編碼 -->
	 <constant name="struts.i18n.encoding" value="UTF-8"/>
	<!-- 1. 修改Struts默認的訪問後綴 -->
	<constant name="struts.action.extension" value="action,do,"></constant>
	<!-- 2. 修改xml自動重新加載 -->
	<constant name="struts.configuration.xml.reload" value="true"/>
	<!-- 3. 開啓動態方法調用 (默認不開啓)-->
	<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
	<!-- 4. 修改上傳文件的最大大小爲30M -->
	<constant name="struts.multipart.maxSize" value="31457280"/>
	<!-- 更改主題,使用標籤時用到 -->
 	<constant name="struts.ui.theme" value="simple"></constant>
</struts>

<!-- 通過常量加載資源文件 -->
 <constant name="struts.custom.i18n.resources" value="cn.sp.config.msg"></constant>

這裏是加載自己寫的國際化配置文件,格式爲msg_en_US.properties,en是英語的簡寫,US是美國簡寫,還有默認格式msg.properties.

3.2文件上傳與下載


<span style="font-size:12px;color:#33ccff;background-color: rgb(51, 102, 255);"><struts>
	<!-- 0. 請求數據編碼 -->
	 <constant name="struts.i18n.encoding" value="UTF-8"/>
	 <!-- 2. 修改xml自動重新加載 -->
	<constant name="struts.configuration.xml.reload" value="true"/>
	<!-- 3. 開啓動態方法調用 (默認不開啓)-->
	<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
	
	<package name="upload_" extends="struts-default">
		<!-- 注意: action 的名稱不能用關鍵字"fileUpload" -->
		<action name="fileUploadAction" class="cn.itcast.e_fileupload.FileUpload">
		
			<!-- 限制運行上傳的文件的類型 -->
			<interceptor-ref name="defaultStack">
				
				<!-- 限制運行的文件的擴展名 -->
				<param name="fileUpload.allowedExtensions">txt,jpg,jar</param>
				
				<!-- 限制運行的類型   【與上面同時使用,取交集】
				<param name="fileUpload.allowedTypes">text/plain</param>
				-->
				
			</interceptor-ref>
			
			<result name="success">/e/success.jsp</result>
			
			<!-- 配置錯誤視圖 -->
			<result name="input">/e/error.jsp</result>
		</action>
		
		<action name="down_*" class="cn.itcast.e_fileupload.DownAction" method="{1}">
			<!-- 列表展示 -->
			<result name="list">/e/list.jsp</result>
			<!-- 下載操作 -->
			<result name="download" type="stream">
			
				<!-- 運行下載的文件的類型:指定爲所有的二進制文件類型 -->
			   <param name="contentType">application/octet-stream</param>
			   
			   <!-- 對應的是Action中屬性: 返回流的屬性【其實就是getAttrInputStream()】 -->
			   <param name="inputName">attrInputStream</param>
			   
			   <!-- 下載頭,包括:瀏覽器顯示的文件名 -->
			   <param name="contentDisposition">attachment;filename=${downFileName}</param>
			 
			 	<!-- 緩衝區大小設置 -->
			   <param name="bufferSize">1024</param>
			</result>
		</action>
	</package>	
</struts></span>



3.3攔截器


<struts>
	<package name="hello" extends="struts-default">
	
		<!-- 【攔截器配置】 -->
		<interceptors>
		
			<!-- 配置用戶自定義的攔截器 -->
			<interceptor name="helloInterceptor" class="cn.itcast.a_interceptor.HelloInterceptor"></interceptor>
			
			<!-- 自定義一個棧: 要引用默認棧、自定義的攔截器 -->
			<interceptor-stack name="helloStack">
				<!-- 引用默認棧 (一定要放到第一行)-->
				<interceptor-ref name="defaultStack"></interceptor-ref>
				<!-- 引用自定義攔截器 -->
				<interceptor-ref name="helloInterceptor"></interceptor-ref>
			</interceptor-stack>
			
		</interceptors>
		
		<!-- 【執行攔截器】 -->
		<default-interceptor-ref name="helloStack"></default-interceptor-ref>
		
	
		<!-- Action配置 -->
		<action name="hello" class="cn.itcast.a_interceptor.HelloAction">
			<result name="success">/index.jsp</result>
		</action>
	
	
	</package>
</struts>
這裏需要注意幾點:
         1.默認棧一定要放在第一行,也就是所有自定義攔截器的前面
          2.執行攔截器時根據作用範圍大小不同可以分爲三種寫法,具體依實際需求而定。
<package name="user" extends="struts-default">
	
		<!-- 【攔截器配置】 -->
		<interceptors>
			<interceptor name="loginCheck" class="cn.itcast.interceptor.UserCheckInterceptor"></interceptor>
			<interceptor-stack name="myStack">
				<interceptor-ref name="defaultStack"></interceptor-ref>
				<interceptor-ref name="loginCheck"></interceptor-ref>
			</interceptor-stack>
		</interceptors>
		<!-- 【執行攔截器:第一種寫法: 當前包下所有的acntion都執行myStack棧】
		<default-interceptor-ref name="myStack"></default-interceptor-ref>
		 -->
	
		<!-- 全局配置 -->
		<global-results>
			<result name="error">/error.jsp</result>
		</global-results>
		
		<action name="user_*" class="cn.itcast.action.UserAction" method="{1}">
			
			<!--第二種寫法: 只是在這一個Action中執行myStack棧 
			<interceptor-ref name="defaultStackt"></interceptor-ref>
			<interceptor-ref name="loginCheck"></interceptor-ref>
			-->
			
			<!-- 第三種寫法:執行用戶棧(與第二種寫法一樣, 只在當前aciton中執行自定義棧) -->
			<interceptor-ref name="myStack"></interceptor-ref>
			
			
			
			<!-- 1. 登陸失敗 -->
			<result name="input">/login.jsp</result>
			
			<!-- 2. 登陸成功 -->
			<result name="loginSuccess" type="redirectAction">user_list</result>
			
			<!-- 3. 列表展示 -->
			<result name="list">/WEB-INF/list.jsp</result>
			
		</action>
	</package>

補上防止表單重複提交攔截器

	<!-- 防止表單重複提交,第二步: 配置" 防止表單重複提交攔截器" -->
			<interceptor-ref name="defaultStack"></interceptor-ref>
			<interceptor-ref name="token">
				<!-- 指定攔截哪些方法需要防止表單重複提交(save) -->
				<param name="includeMethods">save</param>
			</interceptor-ref>
		
			<!-- 防止表單重複提交,第三步: 如果用戶重複提交了跳轉到指定的錯誤頁面  -->
			<result name="invalid.token" type="redirectAction">emp_list</result>

第一步就是在對應的jsp頁面寫入<s:token></s:token>


3.4數據校驗

UserAction-user_register-validation.xml這種取名方式是對特定的action中的特定方法進行數據效驗,

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
 		"-//Apache Struts//XWork Validator 1.0.3//EN"
 		"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">

<validators>
	
	<!-- 驗證的每一個字段用field表示 -->
	<field name="user.userName">
		<!-- 指定使用的驗證器 -->
		<field-validator type="requiredstring">
			<!-- 驗證失敗的錯誤提示信息 -->
			<message>用戶名不能爲空!</message>
		</field-validator>
	</field>
	
	<!-- 驗證pwd -->
	<field name="user.pwd">
		
		<!-- 非空 -->
		<field-validator type="requiredstring">
			<message>密碼不能爲空!</message>
		</field-validator>
		
		<!-- 長度 -->
		<field-validator type="stringlength">
			<param name="minLength">6</param>
			<param name="maxLength">8</param>
			<message>密碼必須爲6-8位!</message>
		</field-validator>
	</field>
	
	<!-- 驗證日期 -->
	<field name="user.birth">
		<field-validator type="date">
			<message>日期格式不對!</message>
		</field-validator>
	</field>
	
	<!-- 驗證Email -->
	<field name="user.email">
		<field-validator type="email">
			<message>郵箱格式錯誤!</message>
		</field-validator>
	</field>
	
</validators> 		
 		


命名方式ActionClassName-validation.xml 放置在同Action的包下,默認Action中的所有方法進行校驗,當然還有代碼校驗此處省略。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章