歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Struts2的一些常量和含義的應用

Struts2的一些常量和含義的應用

日期:2017/3/1 10:34:56   编辑:Linux編程

首先,當我們學習一個框架的時,一般可以參考框架自帶的示例項目。例如struts2,剛入門的時候,可以參考其自帶的blank項目。當部署完這個項目後,開啟tomcat服務,在地址欄裡輸入請求的action路徑時,可以發現,雖然沒有給該action定義擴展名,但是地址裡輸入的action路徑沒有擴展名或者擴展名為action時都可以通過。為什麼呢?這是由struts2的默認配置屬性決定的。

常量的默認配置在struts2-core-2.1.8.1/org/apache/struts2/default.properties文件裡,像定義訪問action的默認擴展名、開發模式設置、i18n以及默認字符編碼等等,例如struts.action.extension=action,,。

應當注意的是,一般情況不要修改框架系統的原始文件,如果要更改默認的配置項,可以采用覆蓋的方式。struts.xml是struts2的基本配置文件之一。該文件內可以配置多個常量,以覆蓋默認的常量配置,例如<constant name="struts.action.extension" value="do,," />表示action的可用擴展名為do或者不用擴展名。

常量可以在下面多個文件中進行定義,struts2加載常量的搜索順序如下,後面的設置可以覆蓋前面的設置:
default.properties文件
struts-default.xml
struts-plugin.xml
struts.xml
struts.properties(為了與webwork向後兼容而提供)
web.xml

下面列舉幾個常用的常量

[java]

  1. ### This can be used to set your default locale and encoding scheme
  2. # struts.locale=en_US
  3. struts.i18n.encoding=UTF-8//指定默認編碼

[java]

  1. # uses javax.servlet.context.tempdir by default
  2. struts.multipart.saveDir=
  3. struts.multipart.maxSize=2097152//上傳文件大小限制

[java]

  1. ### Used by the DefaultActionMapper
  2. ### You may provide a comma separated list, e.g. struts.action.extension=action,jnlp,do
  3. ### The blank extension allows you to match directory listings as well as pure action names
  4. ### without interfering with static resources.
  5. struts.action.extension=action,,//指定處理請求的後綴
[java]
  1. ### Used by FilterDispatcher
  2. ### This is good for development where one wants changes to the static content be
  3. ### fetch on each request.
  4. ### NOTE: This will only have effect if struts.serve.static=true
  5. ### If true -> Struts will write out header for static contents such that they will
  6. ### be cached by web browsers (using Date, Cache-Content, Pragma, Expires)
  7. ### headers).
  8. ### If false -> Struts will write out header for static contents such that they are
  9. ### NOT to be cached by web browser (using Cache-Content, Pragma, Expires
  10. ### headers)
  11. struts.serve.static.browserCache=true//指定是否緩存靜態資源,開發的時候最好關閉
[java]
  1. ### when set to true, Struts will act much more friendly for developers. This
  2. ### includes:
  3. ### - struts.i18n.reload = true
  4. ### - struts.configuration.xml.reload = true
  5. ### - raising various debug or ignorable problems to errors
  6. ### For example: normally a request to foo.action?someUnknownField=true should
  7. ### be ignored (given that any value can come from the web and it
  8. ### should not be trusted). However, during development, it may be
  9. ### useful to know when these errors are happening and be told of
  10. ### them right away.
  11. struts.devMode = false//開發時候設置為trus可以開發出更加詳細的信息
[java]
  1. ### Standard UI theme
  2. ### Change this to reflect which path should be used for JSP control tag templates by default
  3. struts.ui.theme=xhtml//設置默認視圖,最好為simple
[java]
  1. ### Configuration reloading
  2. ### This will cause the configuration to reload struts.xml when it is changed
  3. struts.configuration.xml.reload=false//設置為True一旦Struts.xml文件修改就自動加載
Copyright © Linux教程網 All Rights Reserved