struts.xml配置文件(一)
1,bean元素的配置
struts2框架是一個具有高度可擴展性的java web框架,其主要核心組件不是以之間編寫代碼的方式下載代碼里,而是通過配置文件以IoC控制反轉(zhuǎn) 容器來管理這些組件。struts2可以通過配置方式管理其核心組件,從而允許開發(fā)者很方便的擴展這些核心組件。
Bean在struts.xml中的配置格式如下:
<!-- Bean配置 --><bean name="Bean的名字" class="自定義的組件類"></bean><bean/>元素的常用屬性如下:
1,name:指定Bean實例化對象的名字,對于相同類型的多個Bean實例,其對應(yīng)的name屬性值不能相同。
2,class :指定Bean實例的實現(xiàn)類
3,type:指定Bean實例實現(xiàn)的struts2規(guī)范,該規(guī)范通常是通過某個接口體現(xiàn)的,因此該屬性的值通常是一個struts2接口。
4,scope:指定Bean的作用域。
5,optional:指定bean是否是一個可選項的bean。
6,static:指定允許不創(chuàng)建bean實例,而是讓Bean接受框架常量,這時屬性設(shè)為true。但是當(dāng)指定type屬性時,該屬性不能設(shè)為true。
2,常量constant的配置
常量的配置格式如下:
<!-- 常量配置 --><constant name="資源文件" value="資源文件名字"></constant><constant/>元素常用屬性如下:
1,name:指定常量屬性的名字
2,value:指定常量的值
例如在struts.xml文件中配置國際化文件名和字符集的編碼方式為gb2312,代碼如下:
<!-- 常量配置 ,指定struts2國際化資源文件的名字是messageResource--><constant name="struts.custom.i18n.resources" value="messageResource"></constant><!-- 常量配置 ,指定國際化編碼方式--><constant name="struts.custom.i18n.encoding" value="gb2312"></constant>
在struts.properties文件中相應(yīng)配置代碼如下:
struts.custom.i18n.resources=messageResourcestruts.custom.i18n.encoding=gb2312
在web.xml中相應(yīng)的配置代碼如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"><display-name></display-name> <welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class><init-param><!-- 指定國際化資源文件常量 --><param-name>struts.custom.i18n.resources</param-name><param-value>messageResource</param-value><!-- 指定編碼方式常量 --><param-name>struts.custom.i18n.encoding</param-name><param-value>gb2312</param-value> </init-param></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>*.action</url-pattern></filter-mapping></web-app>
3,包含配置
在開發(fā)一個項目時,一般采用模板開發(fā)的方式,即一個項目由多個模塊組成,每個模塊由某個項目或者某些程序員來開發(fā),每個程序員都可以創(chuàng)建使用自己的配置文件,然后把模塊集成在一起。
在struts.xml中文件包含的格式如下:
<span style="font-size:24px;"> <!-- 倒入一個配置文件,通過這種方式可以將struts2的Action按模板配置到多個配置文件中 --><include file="別的配置文件"></include></span><include>的屬性是file:指定文件名
例如:
<include file="struts-view.xml"></include><include file="struts-edit.xml"></include><include file="struts-help.xml"></include><include file="struts-eventing.xml"></include>
總結(jié)
以上是生活随笔為你收集整理的struts.xml配置文件(一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: struts.xml配置文件结构
- 下一篇: 字符转换属性text-tranform改