gpt4 book ai didi

java - 如何从客户端 GWT 应用程序读取 JNDI 属性

转载 作者:行者123 更新时间:2023-11-30 10:53:58 26 4
gpt4 key购买 nike

在服务器端,有一个 JNDI 资源,我需要从客户端 GWT 应用程序读取它。
我知道,我可以进行 GWT RPC 调用以动态获取 JNDI 资源,但 JNDI 资源是一个静态 URL,一旦页面加载,它就不会改变。所以 - 我的想法是在加载页面时加载 JNDI 资源。
我在 2011 年找到了关于如何做到这一点的过时描述
https://webtide.com/gwt-and-jndi/
但是,我想知道这是否适用于更新版本的 GWT(我使用的是 GWT 2.7.0)

最佳答案

我遇到了同样的问题。将 JNDI 参数和一些其他配置值传递给 GWT 应用程序。

诀窍是动态生成 GWT 主页(在我的例子中是使用 JSP)。

对我的 GWT 应用程序的每个初始调用都会转到前端 Controller (一个 Servlet)以进行授权和其他一些初始化操作。

然后我获取所有 JNDI 参数和其他值,将它们放入请求上下文并调用主机页面 JSP。

在我的 JSP 页面中,我定义了一个 JavaScript 哈希并使用参数对其进行了初始化。

<script type="text/javascript">
var my_params = {
jndiParam1: '<%= request.getAttribute("jndiParam1") %>',
exampleUrl: '<%= request.getAttribute("exampleUrl") %>',
jndiParam2: '<%= request.getAttribute("jndiParam2") %>'
};
</script>

在我的 GWT 应用程序中,我有一个 HostPageParameter 类,它使用 com.google.gwt.i18n.client.Dictionary 来访问 JavaScript 哈希 my_params

public class HostPageParameter {
private static final String DICTNAME = "my_params";
private static HostPageParameter instance = null;

public static HostPageParameter getInstance() {
if(instance == null) {
instance = new HostPageParameter();
}
return instance;
}

private Dictionary myParams;

private HostPageParameter() {
try {
myParams = Dictionary.getDictionary(DICTNAME);
} catch(MissingResourceException e) {
// If not defined
myParams = null;
}
}

public String getParameter(String paramName) {
return getParameter(paramName, null);
}

public String getParameter(String paramName, String defaultValue) {
String paramValue = null;

if(myParams != null && paramName != null) {
try {
paramValue = myParams.get(paramName);
} catch (MissingResourceException e) {
// If not defined
paramValue = defaultValue;
}
}
return paramValue;
}
}

要读取您可以使用的值:

// Without a default value, If not defined, null is returned.
final String jndiParam1 = HostPageParameter.getInstance().getParameter("jndiParam1");

// With default value.
final String exampleUrl = HostPageParameter.getInstance().getParameter("exampleUrl",
"http://example.com");

关于java - 如何从客户端 GWT 应用程序读取 JNDI 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33808567/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com