如何从Java类到Java STRUTS2中的另一个类访问STATIC变量?
|
我试图从一个Java类到另一个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;
}
}
在这里我想访问那些静态变量Class 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();
}
}
没有找到相关结果
已邀请:
1 个回复
郸身
(声明时静态变量的空值) 它是 :
假设是这样的:
现在工作正常…… 谢谢.....