首先
將設定檔的位置放至在/src下,接著在想要使用設定檔的java class內加上程式碼
private Properties props;
private void loadProperties() throws FileNotFoundException, IOException {
props = new Properties();
try {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config.properties");
props.load(inputStream);
//System.out.println("Get Config Properties Successful");
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
}
private String getConfig(String key) {
return props.getProperty(key);
}
public ConfigTest() throws NamingException, SQLException, FileNotFoundException, IOException {
init();
}
private void init() throws FileNotFoundException, IOException {
loadProperties();
}
假設你的設定檔的內容可能為
test.Property = justTest (注意test後面有個點,其實也可以合在一起,只是特別註明此法可以用不同的符號區隔)
那麼接下來再你需要讀取設定檔的method裡(與剛才程式碼同個class)
String test = getConfig("test.Property");
則此時 test ="justTest" 了
此法的缺點是當別支class呼叫到些class時,每次都會重新讀取設定檔
但優點也可以說,我不需重新佈署程式,只單純修改程式設定檔參數即可執行新的參數。
2014年4月21日 星期一
2014年4月8日 星期二
初探 Struts 2.x
初探 Struts 2
Structs 2 與 structs 1.x 大大不同
基本上必須認知一點是 Structs 2.x 並不是 Structs 1.x 的延伸
而是xWork(WebWork的核心) 的架構,可謂是集WebWork與Struct 1.x於大成,而且依然採用 MVC
所以從技術上來說 Structs 2.x 算是全新的框架
Structs 2所需採用的 jar檔為,通常需放在 WEB-INF的lib目錄下(版號會因版本不同,但前面大致相同)
struct2-core-2.x.jar
xwork-core-2.x.jar
commons-lang-2.x.jar
commons-fileupload-1.x.jar
freemarker-2.x.jar
ognl-3.x.jar
javassist-3.x.jar
commons-io-2.x.jar
commons-lang3-3.x.jar
通常網路上會提供懶人包,一次把所有的jar包成一個jar檔,但很容易讓整個專案變得太胖,而且
開發者理論上也應該要了解哪些是真的會用的,哪些不會用到
Structs 2.x 與 1.x 基本上配置有點不一樣
在 1.x 的時候,會使用一個叫做 structs-config.xml
而在 2.x 的時候 則是在 src目錄下 建立一個叫 structs.xml
並且在 1.x 時 對於 structs 在web.xml是配置一個 servlet
但在 2.x 時,則是視為一個 filter
如下:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
假設有使用過 Structs 1.x的人應該知道 structs-config 就是配置 action的地方
而 Structs 2.x 則是在 structs.xml下面有個基本範例
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name="package" extends="struts-default">
<action name="hello" class="com.company.MyStructs2" method="test">
<result name="success">index.jsp</result>
</action>
</package>
</struts>
稍微解釋一下
package name 可隨意取
extends="structs-default" 代表直接使用 struts2.x.jar 中提供的標準設定
action name 就是 可以視為操作行為的名稱
class 就代表 class name,所以是 在 com.company.MyStructs2
method 指的是 在剛才那個 class中有個 method 叫作 test
<result name="success" index.jsp </result>
這是代表 假設 test這個method傳回的結果為 success 則將導向 index.jsp這個頁面
接著如上所示 建立一個 package 名為 com.company,接著 new一個 class名為 MyStructs2
import com.opensymphony.xwork2.ActionSupport;
public class MyStructs2 extends ActionSupport{
public String test(){
return "success";
}
}
如上所示,注意 Struts 2.x 是 extends ActionSupport 而 1.x通常是 Action
接著 請隨意建一個 index.jsp 頁面,當我們要測試 structs 2.x是否運作成功時
則在瀏覽器上輸入 http://serverIP:port/projectName/hello.action
如上所示,測試時 URL 的結尾是採用 .action ,而 structs 1.x 則使用 .do
Structs 2 與 structs 1.x 大大不同
基本上必須認知一點是 Structs 2.x 並不是 Structs 1.x 的延伸
而是xWork(WebWork的核心) 的架構,可謂是集WebWork與Struct 1.x於大成,而且依然採用 MVC
所以從技術上來說 Structs 2.x 算是全新的框架
Structs 2所需採用的 jar檔為,通常需放在 WEB-INF的lib目錄下(版號會因版本不同,但前面大致相同)
struct2-core-2.x.jar
xwork-core-2.x.jar
commons-lang-2.x.jar
commons-fileupload-1.x.jar
freemarker-2.x.jar
ognl-3.x.jar
javassist-3.x.jar
commons-io-2.x.jar
commons-lang3-3.x.jar
通常網路上會提供懶人包,一次把所有的jar包成一個jar檔,但很容易讓整個專案變得太胖,而且
開發者理論上也應該要了解哪些是真的會用的,哪些不會用到
Structs 2.x 與 1.x 基本上配置有點不一樣
在 1.x 的時候,會使用一個叫做 structs-config.xml
而在 2.x 的時候 則是在 src目錄下 建立一個叫 structs.xml
並且在 1.x 時 對於 structs 在web.xml是配置一個 servlet
但在 2.x 時,則是視為一個 filter
如下:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
假設有使用過 Structs 1.x的人應該知道 structs-config 就是配置 action的地方
而 Structs 2.x 則是在 structs.xml下面有個基本範例
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name="package" extends="struts-default">
<action name="hello" class="com.company.MyStructs2" method="test">
<result name="success">index.jsp</result>
</action>
</package>
</struts>
稍微解釋一下
package name 可隨意取
extends="structs-default" 代表直接使用 struts2.x.jar 中提供的標準設定
action name 就是 可以視為操作行為的名稱
class 就代表 class name,所以是 在 com.company.MyStructs2
method 指的是 在剛才那個 class中有個 method 叫作 test
<result name="success" index.jsp </result>
這是代表 假設 test這個method傳回的結果為 success 則將導向 index.jsp這個頁面
接著如上所示 建立一個 package 名為 com.company,接著 new一個 class名為 MyStructs2
import com.opensymphony.xwork2.ActionSupport;
public class MyStructs2 extends ActionSupport{
public String test(){
return "success";
}
}
如上所示,注意 Struts 2.x 是 extends ActionSupport 而 1.x通常是 Action
接著 請隨意建一個 index.jsp 頁面,當我們要測試 structs 2.x是否運作成功時
則在瀏覽器上輸入 http://serverIP:port/projectName/hello.action
如上所示,測試時 URL 的結尾是採用 .action ,而 structs 1.x 則使用 .do
2014年4月7日 星期一
Invoking 'Spring Project Builder' on '/project' 花費時間太久
最近遇到在Ecslipse使用Spring開發程式時,常常Build WorkSpace時很慢
訊息僅顯示Spring Project Builder on /projectName
看了好幾篇文章,看來這是Spring的小Bug,我不想要撰寫幾行程式,一儲存就要等個
3-5分鐘甚至更久才能測試程式,這裡連續提供幾個我試圖解決的方法
1.在 Eclipse上方Windows-->Preference-->Spring-->Bean Support-->Timeout[sec]改成了600秒
本來還未改此設定之前,default是300秒,但由於專案太大,總是會花超過300秒,改成了600秒之後專案就沒有Error了<忘了Error是啥,意思好像是Build的時候timeout>
2.修改(1)剛開始沒問題,後來出現標題的問題,所以修改設定在
Windows-->Preference-->Spring-->Project Builders
把 Spring Bean Meta Data Builder 不要打勾,如圖
訊息僅顯示Spring Project Builder on /projectName
看了好幾篇文章,看來這是Spring的小Bug,我不想要撰寫幾行程式,一儲存就要等個
3-5分鐘甚至更久才能測試程式,這裡連續提供幾個我試圖解決的方法
1.在 Eclipse上方Windows-->Preference-->Spring-->Bean Support-->Timeout[sec]改成了600秒
本來還未改此設定之前,default是300秒,但由於專案太大,總是會花超過300秒,改成了600秒之後專案就沒有Error了<忘了Error是啥,意思好像是Build的時候timeout>
2.修改(1)剛開始沒問題,後來出現標題的問題,所以修改設定在
Windows-->Preference-->Spring-->Project Builders
把 Spring Bean Meta Data Builder 不要打勾,如圖
3.如果仍然有問題,因為Spring把所有Libris中所使用的jar檔中 import的class都搜尋了一遍
可以參考設定在 你的專案名字按「右鍵」-->Properties-->Spring-->Beans Support下面的Options把 Enable support <import/> elements in configuration files 試著勾或不勾看看
補充可加速 Spring Tool Initialize
- Disable validators: Preferences -> Validation: check Suspend all validators
- Disables Spring validation: Preferences -> Spring: uncheck unwanted validation rules
- Disable Build Automatically: Preferences -> General -> workspace: uncheck build automatically
參考資料
http://www.bmchild.com/2013/01/speeding-up-springsource-tool-suite-sts.html
2014年1月6日 星期一
解決 SQL Plus 在 Linux上無法使用 SQL History(按上) 使用 rlwrap 功能
在工作中常常遇到連線 Linux Oracle DB時,下SQL指令,打錯後要重新打無法使用向上鍵而必須重打的冏境,rlwrap是一個好用的工具,且安裝方便
首先至rlwrap官網download最新版本
http://utopia.knoware.nl/~hlub/uck/rlwrap/
首先至rlwrap官網download最新版本
http://utopia.knoware.nl/~hlub/uck/rlwrap/
之後將此檔案放至您想放的目錄下,我是放在/home/oracle下(我使用WinSCP工具)
記得使用 root下指令喔!解壓縮吧!
接著輸入如下指令(記得要到rlwrap-0.37目錄下喔!)
中間的訊息我就省略了
最後的訊息大概像這樣
接著準備 su - oracle 吧!
在.bash_profile加入
alias sqlplus=”rlwrap sqlplus”
alias rman=”rlwrap rman”
之後離開session(putty),重新登入os後試試sqlplus及上一步功能有沒有顯示上一個SQL Commad嘍!
2013年12月25日 星期三
Oracle user密碼過期解決方式
最近使用很久沒使用的Oracle DB發現出現密碼過期的提示訊息
解決方式如下:
首先使用有 DBA 擁限的使用者登入 通常會用 sys as sysdba 這邊我採用sqlplus連線
接著查詢一下user的status看一下是否真的過期了(Expired)
接著修改密碼使用期限後,再修改使用者輸入新的密碼,如圖
解決方式如下:
首先使用有 DBA 擁限的使用者登入 通常會用 sys as sysdba 這邊我採用sqlplus連線
接著查詢一下user的status看一下是否真的過期了(Expired)
接著修改密碼使用期限後,再修改使用者輸入新的密碼,如圖
恭喜搞定!
2013年12月24日 星期二
2013年12月17日 星期二
JQuery Validate Password Format
好久沒寫文章,事實上幾乎都把很多技術文章都轉成文件了,最近比較有空就寫個最近用到的東西,JQuery Plugin Validate
首先當然你可能必須要了解什麼是 JQuery,網路上的文章很多,這部分我就不贅述了!
而且我今天所要用的是在前端頁面就可以做一些判斷,例如必填欄位,至少需幾位字元等等
話不多說,Let do it !!!
首先至 JQuery官網 Download JQuery js,再到http://jqueryvalidation.org/ download JQuery validate js
如下 我寫了一個範例程式有2個欄位,請使用者輸入第一次與第二次密碼確認
<form method="GET" id="myform">
<table align="center" style="width:100%; height: 100px; text-align:center;" class="queryTable">
<tr>
<th align="center" colspan="3" class="detailTitle" >
個人更新密碼
</th>
</tr>
<tr>
<td class="queryHeader">更改密碼<span style="color:red;">*</span></td>
<td><input type="password" id="ps" size="30" name="ps"/></td>
</tr>
<tr>
<td class="queryHeader">確認密碼<span style="color:red;">*</span></td>
<td><input type="password" id="ps2" size="30" name="ps2"/></td>
</tr>
<tr>
<td colspan="2" align="right">
<input type=button value="更新" onClick="send()" class="radiusBtn"/>
</td>
</tr>
</table>
</form>
這邊我就不附上我的css class了,首先完成了你的 password form 後,接著記得 在你的jsp頁面上把剛才download的檔案(解壓縮在dist目錄)把js放入你的Web Project(Content)下的目錄
<script src="js/jquery-1.10.2.js" type="text/javascript"></script>
<script src="js/jquery.validate.js" type="text/javascript"></script>
<script src="js/additional-methods.js" type="text/javascript"></script>
開始主要驗證程式 如下
<script>
// just for the demos, avoids form submit
jQuery.validator.setDefaults({
debug: true,
success: "valid"
});
$( "#myform" ).validate({
rules: {
ps: {
required: true,
minlength:8
},
ps2: {
equalTo: "#ps"
}
}
});
</script>
重點提示 ps,ps2 這邊是指的是name而不是id,所以沒設定name是不行的
基本上有許多規則可以設定使用可參考其它網址
http://www.coolsun.idv.tw/modules/xhnewbb/viewtopic.php?topic_id=1244
http://jqueryvalidation.org/equalTo-method/
http://iphp.wesped.es/wp/blog/2012/09/page/3/
如上面圖示你會發現我的訊息提示是中文的,基本上套件預設的是英文的
我使用的方式是建立一個message_cn.js 接著同樣在剛在import jquery的位置 import進來
<script src="js/messages_cn.js" type="text/javascript"></script>
其中js的內文就如下
jQuery.extend(jQuery.validator.messages, {
required: "必要欄位",
remote: "請修正該字段",
email: "請輸入正確格式的電子郵件",
url: "請輸入合法的網址",
date: "請輸入合法的日期",
dateISO: "請輸入合法的日期 (ISO).",
number: "請輸入合法的數字",
digits: "只能輸入整數",
creditcard: "請輸入合法的信用卡號",
equalTo: "請再次輸入相同的值",
accept: "請輸入有效的字符串",
maxlength: jQuery.validator.format("請輸入一個長度最多是 {0} 的字元"),
minlength: jQuery.validator.format("請輸入一個長度最少是 {0} 的字元"),
rangelength: jQuery.validator.format("請輸入一個長度介於 {0} 和 {1} 之間的字元"),
range: jQuery.validator.format("請輸入一個介於 {0} 和 {1} 之間的值"),
max: jQuery.validator.format("請輸入一個最大為 {0} 的值"),
min: jQuery.validator.format("請輸入一個最小為 {0} 的值")
});
結果訊息就會是中文的嘍!^^"
===============================================================
接著我想加入我密碼的規則,除了套件預設的規則外,我們還可以自訂規則
如下我的規則是必須符合至少8位至多10位,含!@#$%特殊字元與數字,及英文字母大小寫各1位
$.validator.addMethod("pwcheck", function(value) {
return /^[A-Za-z0-9\d=!\-@._*]*$/.test(value) // consists of only these
&& /[a-z]/.test(value) // has a lowercase letter
&& /\d/.test(value);
});
然後加上對於此規則的訊息提示使用原本的js變成
$( "#myform" ).validate({
rules: {
ps: {
required: true,
rangelength:[8,10],
pwcheck: true
},
ps2: {
equalTo: "#ps"
}
},
messages: {
ps: {
required: "必填欄位",
rangelength: jQuery.validator.format("請輸入長度介於 {0} 至 {1} 之間的字元"),
pwcheck: "請輸入正確的密碼格式"
}
}
});
很神奇吧!,事實上訊息會優先讀取這邊的設定喔!
首先當然你可能必須要了解什麼是 JQuery,網路上的文章很多,這部分我就不贅述了!
而且我今天所要用的是在前端頁面就可以做一些判斷,例如必填欄位,至少需幾位字元等等
話不多說,Let do it !!!
首先至 JQuery官網 Download JQuery js,再到http://jqueryvalidation.org/ download JQuery validate js
如下 我寫了一個範例程式有2個欄位,請使用者輸入第一次與第二次密碼確認
<form method="GET" id="myform">
<table align="center" style="width:100%; height: 100px; text-align:center;" class="queryTable">
<tr>
<th align="center" colspan="3" class="detailTitle" >
個人更新密碼
</th>
</tr>
<tr>
<td class="queryHeader">更改密碼<span style="color:red;">*</span></td>
<td><input type="password" id="ps" size="30" name="ps"/></td>
</tr>
<tr>
<td class="queryHeader">確認密碼<span style="color:red;">*</span></td>
<td><input type="password" id="ps2" size="30" name="ps2"/></td>
</tr>
<tr>
<td colspan="2" align="right">
<input type=button value="更新" onClick="send()" class="radiusBtn"/>
</td>
</tr>
</table>
</form>
這邊我就不附上我的css class了,首先完成了你的 password form 後,接著記得 在你的jsp頁面上把剛才download的檔案(解壓縮在dist目錄)把js放入你的Web Project(Content)下的目錄
<script src="js/jquery-1.10.2.js" type="text/javascript"></script>
<script src="js/jquery.validate.js" type="text/javascript"></script>
<script src="js/additional-methods.js" type="text/javascript"></script>
開始主要驗證程式 如下
<script>
// just for the demos, avoids form submit
jQuery.validator.setDefaults({
debug: true,
success: "valid"
});
$( "#myform" ).validate({
rules: {
ps: {
required: true,
minlength:8
},
ps2: {
equalTo: "#ps"
}
}
});
</script>
重點提示 ps,ps2 這邊是指的是name而不是id,所以沒設定name是不行的
基本上有許多規則可以設定使用可參考其它網址
http://www.coolsun.idv.tw/modules/xhnewbb/viewtopic.php?topic_id=1244
http://jqueryvalidation.org/equalTo-method/
http://iphp.wesped.es/wp/blog/2012/09/page/3/
如上面圖示你會發現我的訊息提示是中文的,基本上套件預設的是英文的
我使用的方式是建立一個message_cn.js 接著同樣在剛在import jquery的位置 import進來
<script src="js/messages_cn.js" type="text/javascript"></script>
其中js的內文就如下
jQuery.extend(jQuery.validator.messages, {
required: "必要欄位",
remote: "請修正該字段",
email: "請輸入正確格式的電子郵件",
url: "請輸入合法的網址",
date: "請輸入合法的日期",
dateISO: "請輸入合法的日期 (ISO).",
number: "請輸入合法的數字",
digits: "只能輸入整數",
creditcard: "請輸入合法的信用卡號",
equalTo: "請再次輸入相同的值",
accept: "請輸入有效的字符串",
maxlength: jQuery.validator.format("請輸入一個長度最多是 {0} 的字元"),
minlength: jQuery.validator.format("請輸入一個長度最少是 {0} 的字元"),
rangelength: jQuery.validator.format("請輸入一個長度介於 {0} 和 {1} 之間的字元"),
range: jQuery.validator.format("請輸入一個介於 {0} 和 {1} 之間的值"),
max: jQuery.validator.format("請輸入一個最大為 {0} 的值"),
min: jQuery.validator.format("請輸入一個最小為 {0} 的值")
});
結果訊息就會是中文的嘍!^^"
===============================================================
接著我想加入我密碼的規則,除了套件預設的規則外,我們還可以自訂規則
如下我的規則是必須符合至少8位至多10位,含!@#$%特殊字元與數字,及英文字母大小寫各1位
$.validator.addMethod("pwcheck", function(value) {
return /^[A-Za-z0-9\d=!\-@._*]*$/.test(value) // consists of only these
&& /[a-z]/.test(value) // has a lowercase letter
&& /\d/.test(value);
});
然後加上對於此規則的訊息提示使用原本的js變成
$( "#myform" ).validate({
rules: {
ps: {
required: true,
rangelength:[8,10],
pwcheck: true
},
ps2: {
equalTo: "#ps"
}
},
messages: {
ps: {
required: "必填欄位",
rangelength: jQuery.validator.format("請輸入長度介於 {0} 至 {1} 之間的字元"),
pwcheck: "請輸入正確的密碼格式"
}
}
});
很神奇吧!,事實上訊息會優先讀取這邊的設定喔!
訂閱:
文章 (Atom)












