0
我想從一個Java類訪問靜態變量到另一個,但沒有得到價值!如何在JAVA STRUTS2中從java類訪問另一個STATIC變量?
下面是我的java類。
靜態內容,類1:
package com.globalProperties;
public class GlobalVariables {
static{
someFunctionName();
}
private static String SMTP_SERVER = "";
private static String SMTP_USERNAME = "";
private static String SMTP_PASSWORD = "";
private static void someFunctionName(){
Connection con = null;
Statement statement = null;
ResultSet resultset = null;
LinkedHashMap<String,String> sysParmHashMap = new LinkedHashMap<String, String>();
con = DatabaseConnection.getDbConnection();
statement = con.createStatement();
String queryStr = "SELECT * FROM SMTPCONNECTION";
resultset = statement.executeQuery(queryStr);
while(resultset.next()){
sysParmHashMap.put(resultset.getString("KEY"), resultset.getString("KEY_VALUE"));
}
// First i tried this to set value
SMTP_SERVER = sysParmHashMap.get("SMTP_SERVER").trim();
SMTP_USERNAME = sysParmHashMap.get("SMTP_USERNAME").trim();
SMTP_PASSWORD = sysParmHashMap.get("SMTP_PASSWORD").trim();
// Second time, i tried this to set value
setSMTP_SERVER(sysParmHashMap.get("SMTP_SERVER").trim());
setSMTP_USERNAME(sysParmHashMap.get("SMTP_USERNAME").trim());
setSMTP_PASSWORD(sysParmHashMap.get("SMTP_PASSWORD").trim());
System.out.println("SMTP_SERVER :" +SMTP_SERVER);
System.out.println("SMTP_USERNAME :" +SMTP_USERNAME);
System.out.println("SMTP_PASSWORD :" +SMTP_PASSWORD);
}
public static String getSMTP_SERVER() {
return SMTP_SERVER;
}
public static void setSMTP_SERVER(String sMTP_SERVER) {
SMTP_SERVER = sMTP_SERVER;
}
public static String getSMTP_USERNAME() {
return SMTP_USERNAME;
}
public static void setSMTP_USERNAME(String sMTP_USERNAME) {
SMTP_USERNAME = sMTP_USERNAME;
}
public static String getSMTP_PASSWORD() {
return SMTP_PASSWORD;
}
public static void setSMTP_PASSWORD(String sMTP_PASSWORD) {
SMTP_PASSWORD = sMTP_PASSWORD;
}
public static String getGATEWAY_SERVICE_URL() {
return GATEWAY_SERVICE_URL;
}
}
在這裏,我想接取那些靜態變量2類:
package com.unilog.sendMails;
import com.globalProperties.GlobalVariables;
improt .... some imports ....;
public class sendEmailTo extends ActionSupport implements ServletResponseAware {
public String MailContact(){
String result = "Fail";
if(Some condition){
result = sendingEmail( -- Parameters --);
}
}
public String sendingEmail(String toAddress,String fromAddress,String mailSubject,String mailBody){
String result="FAIL";
// For Below variable am not getting any value they are Empty but not null
String smtpHost = GlobalVariables.getSMTP_SERVER();
final String smtpUserName = GlobalVariables.getSMTP_USERNAME();
final String smtpPassword = GlobalVariables.getSMTP_PASSWORD();
}
}
任何異常/錯誤信息? – lschin 2011-05-30 08:20:53