简体   繁体   English

从一个类到另一个Java使用变量

[英]Using variables from one class into another java

I have a java we application that is working with velocity. 我有一个正在处理速度的Java we应用程序。 I get two variables in the first page via url that I extract using ureq.getParameter() method. 我在第一页中通过使用ureq.getParameter()方法提取的URL获得了两个变量。 One of the other classes that has a velocity container and I need to send one of the variables from the url to this velocity container. 其他具有速度容器的类之一,我需要将url中的变量之一发送到该速度容器。 I tried creating an instance of the first class in the second class and using getVariable name method to do that but it did not work. 我尝试在第二个类中创建第一个类的实例,并使用getVariable name方法来执行此操作,但是它不起作用。 Can someone tell me how I can do this? 有人可以告诉我该怎么做吗?

Class 1: 第1类:

package org.olat.dispatcher;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.olat.core.gui.UserRequest;

public class RemoteLoginformDispatcher implements Dispatcher {

    private static final String PARAM_newUrl = "ret";
    private static String newURL;

    @Override
    public void execute(
            final HttpServletRequest request, 
            final HttpServletResponse response, 
            final String uriPrefix) {

        UserRequest ureq = null;

        try {
            ureq = new UserRequest(uriPrefix, request, response);
            newURL = ureq.getParameter(PARAM_newUrl);
        } catch () {
        }

    }

    public String getURL(){
        return newURL;
    }

}

Class 2: 第2类:

public class BaseChiefController extends DefaultChiefController implements ContentableChiefController {
    //Velocity container mainvc created here. It interacts with a html file. Removed the code that would not really matter

    //mainvc.contextPut("newURL", "something");
    //The below statement works. When I try with something, the something appears in the html file.
    mainvc.contextPut("newURL", myLogin.getURL());      

}

To create an instance of another class, simply create a "public CLASSNAME" method, and inside define all class variables with the "this" modfier. 要创建另一个类的实例,只需创建一个“ public CLASSNAME”方法,并在内部使用“ this”修饰符定义所有类变量。 Then, call out the function you wish to use from that method, and when you want to use the class, just do "new CLASSNAME(args);" 然后,从该方法中调出要使用的函数,然后在要使用该类时,只需执行“ new CLASSNAME(args);”即可。

Although, I am not really sure I am understanding your question. 虽然,我不确定我是否理解您的问题。

Maybe this is your answer. 也许这是您的答案。 You can use variables from one class to another class by making the variable static, then doing "CLASSNAME.VARIABLENAME = WHATEVER". 通过将变量设为静态,然后执行“ CLASSNAME.VARIABLENAME = WHATEVER”,可以使用一个类到另一个类的变量。


EDITED: 编辑:

Okay, so as far as I can tell, you are using a method to return a static value from the class, which is much slower than just doing "newURL", RemoteLoginformDispatcher.newURL);. 好的,据我所知,您正在使用一种从类中返回静态值的方法,这比仅执行“ newURL”,RemoteLoginformDispatcher.newURL);要慢得多。 Why not try this, as it is probably faster, and it should always work if newURL is defined. 为什么不尝试此方法,因为它可能更快,并且如果定义了newURL,它应该总是可以工作。 Otherwise, you have a different problem, and newURL is not being defined. 否则,您会有其他问题,并且不会定义newURL。 If this is the case, try printing the caught Exception. 如果是这种情况,请尝试打印捕获的异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM