如何在 JAVA STRUTS2 中从 java 类访问另一个类的静态变量?

发布于 2024-11-10 10:20:24 字数 3275 浏览 4 评论 0原文

我正在尝试从一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单调的奢华 2024-11-17 10:20:24

问题解决了,我正在分配 VARIABLE ="" (声明时静态变量的空值)

它是:

 private static String SMTP_SERVER = "";
    private static String SMTP_USERNAME = "";
    private static String SMTP_PASSWORD = "";

它应该是这样的:

 private static String SMTP_SERVER;
    private static String SMTP_USERNAME;
    private static String SMTP_PASSWORD;

现在它工作正常......

谢谢你.....

Issue solved, i was assigning VARIABLE ="" (empty value for static variable in declaration time)

It was :

 private static String SMTP_SERVER = "";
    private static String SMTP_USERNAME = "";
    private static String SMTP_PASSWORD = "";

Where it was suppose to be like this :

 private static String SMTP_SERVER;
    private static String SMTP_USERNAME;
    private static String SMTP_PASSWORD;

Now its working fine......

Thank you.....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文