用過struts1.x的人都知道,標簽庫有html、bean、logic、tiles,
而struts2.0里的標簽卻沒有分類,只用在jsp頭文件加上
<%@ taglib prefix="s" uri="/struts-tags" %>
就能使用struts2.0的標簽庫
下面就介紹下每個標簽的用法(有錯請指正):
A:
<s:a href=""></s:a>-----超鏈接,類似于html里的<a></a>
<s:action name=""></s:action>-----執行一個view里面的一個action
<s:actionerror/>-----如果action的errors有值那么顯示出來
<s:actionmessage/>-----如果action的message有值那么顯示出來
<s:append></s:append>-----添加一個值到list,類似于list.add();
<s:autocompleter></s:autocompleter>-----自動完成<s:combobox>標簽的內容,這個是ajax
B:
<s:bean name=""></s:bean>-----類似于struts1.x中的,JavaBean的值
C:
<s:checkbox></s:checkbox>-----復選框
<s:checkboxlist list=""></s:checkboxlist>-----多選框
<s:combobox list=""></s:combobox>-----下拉框
<s:component></s:component>-----圖像符號
D:
<s:date/>-----獲取日期格式
<s:datetimepicker></s:datetimepicker>-----日期輸入框
<s:debug></s:debug>-----顯示錯誤信息
<s:div></s:div>-----表示一個塊,類似于html的<div></div>
<s:doubleselect list="" doubleName="" doubleList=""></s:doubleselect>-----雙下拉框
E:
<s:if test=""></s:if>
<s:elseif test=""></s:elseif>
<s:else></s:else>-----這3個標簽一起使用,表示條件判斷
F:
<s:fielderror></s:fielderror>-----顯示文件錯誤信息
<s:file></s:file>-----文件上傳
<s:form action=""></s:form>-----獲取相應form的值
G:
<s:generator separator="" val=""></s:generator>----和<s:iterator>標簽一起使用
H:
<s:head/>-----在<head></head>里使用,表示頭文件結束
<s:hidden></s:hidden>-----隱藏值
I:
<s:i18n name=""></s:i18n>-----加載資源包到值堆棧
<s:include value=""></s:include>-----包含一個輸出,servlet或jsp頁面
<s:inputtransferselect list=""></s:inputtransferselect>-----獲取form的一個輸入
<s:iterator></s:iterator>-----用于遍歷集合
L:
<s:label></s:label>-----只讀的標簽
M:
<s:merge></s:merge>-----合并遍歷集合出來的值
O:
<s:optgroup></s:optgroup>-----獲取標簽組
<s:optiontransferselect doubleList="" list="" doubleName=""></s:optiontransferselect>-----左右選擇框
P:
<s:param></s:param>-----為其他標簽提供參數
<s:password></s:password>-----密碼輸入框
<s:property/>-----得到'value'的屬性
<s:push value=""></s:push>-----value的值push到棧中,從而使property標簽的能夠獲取value的屬性
R:
<s:radio list=""></s:radio>-----單選按鈕
<s:reset></s:reset>-----重置按鈕
S:
<s:select list=""></s:select>-----單選框
<s:set name=""></s:set>-----賦予變量一個特定范圍內的值
<s:sort comparator=""></s:sort>-----通過屬性給list分類
<s:submit></s:submit>-----提交按鈕
<s:subset></s:subset>-----為遍歷集合輸出子集
T:
<s:tabbedPanel id=""></s:tabbedPanel>-----表格框
<s:table></s:table>-----表格
<s:text name=""></s:text>-----I18n文本信息
<s:textarea></s:textarea>-----文本域輸入框
<s:textfield></s:textfield>-----文本輸入框
<s:token></s:token>-----攔截器
<s:tree></s:tree>-----樹
<s:treenode label=""></s:treenode>-----樹的結構
U:
<s:updownselect list=""></s:updownselect>-----多選擇框
<s:url></s:url>-----創建url
一. .properties 文件的形式
# 以下為服務器、數據庫信息
dbPort = localhost
databaseName = mydb
dbUserName = root
dbPassword = root
# 以下為數據庫表信息
dbTable = mytable
# 以下為服務器信息
ip = 192.168.0.9
在上面的文件中我們假設該文件名為: test.properties 文件。其中 # 開始的一行為注釋信息;在等號“ = ”左邊的我們稱之為 key ;等號“ = ”右邊的我們稱之為 value 。(其實就是我們常說的鍵 - 值對) key 應該是我們程序中的變量。而 value 是我們根據實際情況配置的。
二. JDK 中的 Properties 類 Properties 類存在于胞 Java.util 中,該類繼承自 Hashtable ,它提供了幾個主要的方法:
1. getProperty ( String key) , 用指定的鍵在此屬性列表中搜索屬性。也就是通過參數 key ,得到 key 所對應的 value。
2. load ( InputStream inStream) ,從輸入流中讀取屬性列表(鍵和元素對)。通過對指定的文件(比如說上面的 test.properties 文件)進行裝載來獲取該文件中的所有鍵 - 值對。以供 getProperty ( String key) 來搜索。
3. setProperty ( String key, String value) ,調用 Hashtable 的方法 put 。他通過調用基類的put方法來設置 鍵 - 值對。
4. store ( OutputStream out, String comments) , 以適合使用 load 方法加載到 Properties 表中的格式,將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流。與 load 方法相反,該方法將鍵 - 值對寫入到指定的文件中去。
5. clear () ,清除所有裝載的 鍵 - 值對。該方法在基類中提供。
有了以上幾個方法我們就可以對 .properties 文件進行操作了!
三.代碼實例
package configuration;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/** *//**
* 讀取properties文件
* @author Qutr
*
*/
public class Configuration
...{
private Properties propertie;
private FileInputStream inputFile;
private FileOutputStream outputFile;
/** *//**
* 初始化Configuration類
*/
public Configuration()
...{
propertie = new Properties();
}
/** *//**
* 初始化Configuration類
* @param filePath 要讀取的配置文件的路徑+名稱
*/
public Configuration(String filePath)
...{
propertie = new Properties();
try ...{
inputFile = new FileInputStream(filePath);
propertie.load(inputFile);
inputFile.close();
} catch (FileNotFoundException ex) ...{
System.out.println("讀取屬性文件--->失敗!- 原因:文件路徑錯誤或者文件不存在");
ex.printStackTrace();
} catch (IOException ex) ...{
System.out.println("裝載文件--->失敗!");
ex.printStackTrace();
}
}//end ReadConfigInfo(...)
/** *//**
* 重載函數,得到key的值
* @param key 取得其值的鍵
* @return key的值
*/
public String getValue(String key)
...{
if(propertie.containsKey(key))...{
String value = propertie.getProperty(key);//得到某一屬性的值
return value;
}
else
return "";
}//end getValue(...)
/** *//**
* 重載函數,得到key的值
* @param fileName properties文件的路徑+文件名
* @param key 取得其值的鍵
* @return key的值
*/
public String getValue(String fileName, String key)
...{
try ...{
String value = "";
inputFile = new FileInputStream(fileName);
propertie.load(inputFile);
inputFile.close();
if(propertie.containsKey(key))...{
value = propertie.getProperty(key);
return value;
}else
return value;
} catch (FileNotFoundException e) ...{
e.printStackTrace();
return "";
} catch (IOException e) ...{
e.printStackTrace();
return "";
} catch (Exception ex) ...{
ex.printStackTrace();
return "";
}
}//end getValue(...)
/** *//**
* 清除properties文件中所有的key和其值
*/
public void clear()
...{
propertie.clear();
}//end clear();
/** *//**
* 改變或添加一個key的值,當key存在于properties文件中時該key的值被value所代替,
* 當key不存在時,該key的值是value
* @param key 要存入的鍵
* @param value 要存入的值
*/
public void setValue(String key, String value)
...{
propertie.setProperty(key, value);
}//end setValue(...)
/** *//**
* 將更改后的文件數據存入指定的文件中,該文件可以事先不存在。
* @param fileName 文件路徑+文件名稱
* @param description 對該文件的描述
*/
public void saveFile(String fileName, String description)
...{
try ...{
outputFile = new FileOutputStream(fileName);
propertie.store(outputFile, description);
outputFile.close();
} catch (FileNotFoundException e) ...{
e.printStackTrace();
} catch (IOException ioe)...{
ioe.printStackTrace();
}
}//end saveFile(...)
public static void main(String[] args)
...{
Configuration rc = new Configuration("."config"test.properties");//相對路徑
String ip = rc.getValue("ipp");//以下讀取properties文件的值
String host = rc.getValue("host");
String tab = rc.getValue("tab");
System.out.println("ip = " + ip + "ip-test leng = " + "ip-test".length());//以下輸出properties讀出的值
System.out.println("ip's length = " + ip.length());
System.out.println("host = " + host);
System.out.println("tab = " + tab);
Configuration cf = new Configuration();
String ipp = cf.getValue("."config"test.properties", "ip");
System.out.println("ipp = " + ipp);
// cf.clear();
cf.setValue("min", "999");
cf.setValue("max", "1000");
cf.saveFile("."config"save.perperties", "test");
// Configuration saveCf = new Configuration();
// saveCf.setValue("min", "10");
// saveCf.setValue("max", "1000");
// saveCf.saveFile("."config"save.perperties");
}//end main()
}//end class ReadConfigInfo
四.小結 通過上面的例子不難看出,在Java中操作配置文件是非常簡單的。在一個需要用到大量配置信息的模塊或系統里,我們有必要封裝一個專門的類來共使用。通過最后的main函數調用,相信大家可以看出該類的用法。不足指出希望大家多多指點。
Java properties文件的操作
----------------------------------------------------
java中的properties文件是一種配置文件,主要用于表達配置信息,文件類型為*.properties,格式為文本文件,文件的內容是格式是"鍵=值"的格式,在properties文件中,可以用"#"來作注釋,properties文件在Java編程中用到的地方很多,操作很方便。下面是一個操作java properties文件的例子,給出了操作方法和properties文件。從中可以看到如何讀取properties文件,并應用讀取出來的值,是學習操作properties文件的好例子。
一、properties文件
IcisReport.properties
------------------------------------------------------
#################################
# 工商報表應用IcisReport的配置文件 #
# 作者:雷智民 #
# 日期:2006年11月21日 #
#################################
#
# 說明:業務系統TopIcis和報表系統IcisReport是分離的
# 可分開部署到不同的服務器上,也可以部署到同一個服務
# 器上;IcisReprot作為獨立的web應用程序可以使用任何
# 的Servlet容器或者J2EE服務器部署并單獨運行,也可以
# 通過業務系統的接口調用作為業務系統的一個庫來應用.
#
# IcisReport的ip
IcisReport.server.ip=192.168.3.143
# IcisReport的端口
IcisReport.server.port=8080
# IcisReport的上下文路徑
IcisReport.contextPath=/IcisReport
------------------------------------------------------
二、操作properties文件的java方法
下面是一個操作properties文件的方法
/** *//**
* @return 獲取IcisReport報表應用的URL
*/
private String getIcisReportURL() ...{
String icisReportURL = ""; // IcisReport報表應用的URL
String icisReportServerIP = ""; // IcisReport服務器的IP
String icisReportServerPort = ""; // IcisReport服務器的服務端口
String icisReportContextPath = ""; // IcisReport應用的ContextPath
Properties prop = new Properties();
InputStream in;
try ...{
in = getClass().getResourceAsStream("/IcisReport.properties");
prop.load(in);
Set keyValue = prop.keySet();
for (Iterator it = keyValue.iterator(); it.hasNext();) ...{
String key = (String) it.next();
if (key.equals("IcisReport.server.ip")) ...{
icisReportServerIP = (String) prop.get(key);
} else if (key.equals("IcisReport.server.port")) ...{
icisReportServerPort = (String) prop.get(key);
} else if (key.equals("IcisReport.contextPath")) ...{
icisReportContextPath = (String) prop.get(key);
}
}
} catch (Exception e) ...{
log.error("IO讀取出錯,找不到IcisReport.properties!");
}
if (icisReportServerIP.trim().equals("")) ...{
log
.error("請檢查配置文件IcisReport.properties中的IcisReport.server.ip項的值是否正確!");
}
if (icisReportServerPort.trim().equals("")) ...{
log
.error("請檢查配置文件IcisReport.properties中的IcisReport.server.port項的值是否正確!");
}
if (icisReportServerPort.trim().equals("")) ...{
log
.error("請檢查配置文件IcisReport.properties中的IcisReport.server.port項的值是否正確!");
}
icisReportURL = "http://" + icisReportServerIP.trim() + ":"
+ icisReportServerPort.trim() + icisReportContextPath.trim();
log.info("獲取的icisReportURL=" + icisReportURL);
return icisReportURL;
}
總結:java的properties文件需要放到classpath下面,這樣程序才能讀取到,有關classpath實際上就是java類或者庫的存放路徑,在java工程中,properties放到class文件一塊。在web應用中,最簡單的方法是放到web應用的WEB-INF"classes目錄下即可,也可以放在其他文件夾下面,這時候需要在設置classpath環境變量的時候,將這個文件夾路徑加到classpath變量中,這樣也也可以讀取到。在此,你需要對classpath有個深刻理解,classpath絕非系統中刻意設定的那個系統環境變量,WEB-INF"classes其實也是,java工程的class文件目錄也是。
該文章轉載自網絡大本營:http://www.xrss.cn/Dev/JAVA/200761514149.Html
一:介紹:
properties文件在java開發中使用的比較多,主要是一些配置不希望在程序中寫死,而采用
properties文件這樣在不同的地方使用只需要修改properties文件而不用修改程序,最平常的
是使用在數據庫配置中或信息配置中,在開發多語言版本的時候也很有用處,你不同的語言版本
使用不同的配置文件,這樣你就可以不修改程序也不用在程序中在判斷,只需要把文件放在
不同的地方就可以使用。
二:準備
使用properties文件你需要使用java.util.ResourceBundle充分了解,同時你需要把properties
文件放在classpath中,這樣系統啟動是才能加載文件。
三:加載properties文件
ResourceBundle msgBundle=ResourceBundle.getBundle(msgResource,Locale.CHINA);
使用上面的語句你就可以加載properties文件文件了,但你必須保證properties 文件放
在classpath中。
同時請參考Java API java.util.ResourceBundle;
四:使用properties
現在你需要取到properties文件中的內容,使用ResourceBundle里面的getString() 方法就可以了。
但需要注意的是getString取到的是ISO字符串,你可能根據需要轉換為不同的字符串。
五:具體實現
msg.properties
=============================================
dafualt.path=e:/dbocw/
error_0=password error
error_1=user not found
MessageBundle.java
=============================================
public class MessageBundle{
private static ResourceBundle msgBundle=null;
public MessageBundle(String msgResource){
msgBundle=ResourceBundle.getBundle(msgResource,Locale.CHINA);
}
public static String getMessage(String _key) {
String message=null;
try{
message=new String(msgBundle.getString(_key).getBytes("ISO8859_1"),"gb2312");
}catch(MissingResourceException ex){
ex.printStackTrace();
}catch(UnsupportedEncodingException ex){
ex.printStackTrace();
}
return message;
}
}
MsgInfo.java
=================================================================
public class MsgInfo{
private static MessageBundle msg=new MessageBundle("database");
public MsgInfo(){
}
public static String ERROR_0=msg.getMessage("error_0");
...........
}
六:具體運用
1:)連接數據庫
在jsp開發中通常連接數據庫都是由JavaBean去實現,但你由不希望下次使用這個javabean
去修改.這時候properties文件就很有作用了。你可以把數據庫配置放在properties文件中。
這樣就可以只修改properties而繼續使用JavaBean了。
2網頁風格
建設一個網站通常是需要統一的風格,也就以為著需要統一的背景色等等,這個時候你把
網頁風格涉及的要素放在peoperties文件中,需要修改一次性修改幾可以了,或者下次還
有大概相同的網站是不是可以省修改頁面的時間啊。
3:)信息提示
在開發一個Appaction中出錯提示或者信息提示是必須的,而很多時候你的提示信息,用戶
未必能理解,一開始你又不知道如何用戶可以理解,這個時候把所有的提示信息放在
properties文件中是一個不錯的提示。
4:)和系統有關的屬性
因為java是可以在不同的平臺上運行的,而很多時候開發和實際運行是在不同的平臺,這個
時候你就可以使用properties文件保存系統屬性,移植也可以省一些時間。
....
properties文件大概的的用處我先說這么多了,實際運用中其實有很多地方會用到properties
文件你實際運用到中會有體會的。