如何在 JAVA STRUTS2 中从 java 类访问另一个类的静态变量?
我正在尝试从一个 java 类访问另一个 java 类的静态变量,但没有获取该值!
下面是我的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();
}
}
I am trying to access STATIC Variables from one java class to another but am not getting the value!
Below are my java classes.
Static Contents, class 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;
}
}
Here i want to acess those static variables 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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题解决了,我正在分配
VARIABLE =""
(声明时静态变量的空值)它是:
它应该是这样的:
现在它工作正常......
谢谢你.....
Issue solved, i was assigning
VARIABLE =""
(empty value for static variable in declaration time)It was :
Where it was suppose to be like this :
Now its working fine......
Thank you.....