我想从worklight适配器调用java类
我在此url“”中遵循了此文档模块5.5,或 我的代码结构也是如此我想从worklight适配器调用java类,java,javascript,ibm-mobilefirst,Java,Javascript,Ibm Mobilefirst,我在此url“”中遵循了此文档模块5.5,或 我的代码结构也是如此 server/java folder name com.worklight.custonmode package name loginfunction.java java file inside com.worklight.custonmode package login j
server/java folder name
com.worklight.custonmode package name
loginfunction.java java file inside com.worklight.custonmode package
login java method in class loginfunction
我已经从worklight适配器调用了
function loginmodules(username, passwd) {
return {
result : com.worklight.custonmode.loginfunction.login()
};
}
当我打电话时,我得到的错误如下
response [/apps/services/api/Light/common/query] success: /*-secure- {"responseID":"2","errors":["Ecma Error: TypeError: Cannot call property login in object [JavaPackage com.worklight.custonmode.loginfunction]. It is not a function, it is \"object\".
(C%3A%5用户%5管理员%5工作空间11%5MobileClient%5Adapters%5Adapter/Adapter impl.js#103)”,“isSuccessful”:false,“警告”:[],“信息”:[]}*/
loginfunction.java中的我的登录函数
public class loginfunction {
public static void login() {
//============== Code to adapt to your own configuration =============//
String server = "https://ibm-f4acjqe8c6p:9443/dfd"; // Set the Public URI of your RRC server
String JTS_Server = "https://ibm-f4acjqe8c6p:9443/jts"; //Set the public URI of your JTS server
String login = "Admin"; // Set the user login
String password = "Admin"; // Set the associated password
//============== -------------------------------------- =============//
String rootServices = server + "/rootservices";
String catalogXPath = "/rdf:Description/oslc_we:rweServiceProviders/@rdf:resource";
String serviceProviderTitleXPath = "//oslc:ServiceProvider/dcterms:title";
System.out.println(">> Example03: Print out the content of the Service Providers catalog");
System.out.println(" - Root Services URI: "+rootServices);
System.out.println(" - Service Providers catalog XPath expression: "+catalogXPath);
System.out.println(" - Service Provider title XPath expression: "+serviceProviderTitleXPath);
System.out.println(" - Login: "+login);
System.out.println(" - Password: "+password);
// Setup the HttClient
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpUtils.setupLazySSLSupport(httpclient);
// Setup the rootServices request
HttpGet rootServiceDoc = new HttpGet(rootServices);
rootServiceDoc.addHeader("Accept", "application/rdf+xml");
rootServiceDoc.addHeader("OSLC-Core-Version", "2.0");
try {
// Request the Root Services document
HttpResponse rootServicesResponse = HttpUtils.sendGetForSecureDocument(
server, rootServiceDoc, login, password, httpclient,JTS_Server);
if (rootServicesResponse.getStatusLine().getStatusCode() == 200) {
// Define the XPath evaluation environment
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
xpath.setNamespaceContext(
new NamespaceContextMap(new String[]
{ "rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"oslc_we","http://open-services.net/xmlns/we/1.0/"}));
// Parse the response body to retrieve the catalog URI
InputSource source = new InputSource(rootServicesResponse.getEntity().getContent());
Node attribute = (Node) (xpath.evaluate(catalogXPath, source, XPathConstants.NODE));
String serviceProvidersCatalog = attribute.getTextContent();
rootServicesResponse.getEntity().consumeContent();
// Setup the catalog request
HttpGet catalogDoc = new HttpGet(serviceProvidersCatalog);
catalogDoc.addHeader("Accept", "application/xml");
catalogDoc.addHeader("OSLC-Core-Version", "2.0");
// Access to the Service Providers catalog
HttpResponse catalogResponse = HttpUtils.sendGetForSecureDocument(
server, catalogDoc, login, password, httpclient,JTS_Server);
if (catalogResponse.getStatusLine().getStatusCode() == 200) {
// Define the XPath evaluation environment
XPath xpath2 = factory.newXPath();
xpath2.setNamespaceContext(
new NamespaceContextMap(new String[]
{ "oslc", "http://open-services.net/ns/core#",
"dcterms","http://purl.org/dc/terms/"}));
// Parse the response body to retrieve the Service Provider
source = new InputSource(catalogResponse.getEntity().getContent());
NodeList titleNodes = (NodeList) (xpath2.evaluate(serviceProviderTitleXPath, source, XPathConstants.NODESET));
// Print out the title of each Service Provider
int length = titleNodes.getLength();
System.out.println(">> Project Areas:");
for (int i = 0; i < length; i++) {
System.out.println(">> \t - "+ titleNodes.item(i).getTextContent());
}
}
}
rootServicesResponse.getEntity().consumeContent();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XPathExpressionException e) {
e.printStackTrace();
} catch (InvalidCredentialsException e) {
e.printStackTrace();
} finally {
// Shutdown the HTTP connection
httpclient.getConnectionManager().shutdown();
}
}
公共类登录函数{
公共静态void登录(){
//===============代码以适应您自己的配置=============//
字符串服务器=”https://ibm-f4acjqe8c6p:9443/dfd“;//设置RRC服务器的公共URI
字符串JTS_服务器=”https://ibm-f4acjqe8c6p:9443/jts“;//设置JTS服务器的公共URI
String login=“Admin”//设置用户登录
String password=“Admin”//设置关联的密码
//============== -------------------------------------- =============//
字符串rootServices=server+“/rootServices”;
字符串catalogXPath=“/rdf:Description/oslc_we:rweServiceProviders/@rdf:resource”;
字符串serviceProviderTitleXPath=“//oslc:ServiceProvider/dcterms:title”;
System.out.println(“>>示例03:打印出服务提供商目录的内容”);
System.out.println(“-Root服务URI:+Root服务”);
System.out.println(“-服务提供者目录XPath表达式:“+catalogXPath”);
System.out.println(“-服务提供者标题XPath表达式:“+serviceProviderTitleXPath”);
System.out.println(“-Login:”+Login);
System.out.println(“-Password:+Password”);
//设置HttClient
DefaultHttpClient httpclient=新的DefaultHttpClient();
HttpUtils.setupLazySSLSupport(httpclient);
//设置rootServices请求
HttpGet rootServiceDoc=新的HttpGet(rootServices);
rootServiceDoc.addHeader(“接受”、“应用程序/rdf+xml”);
rootServiceDoc.addHeader(“OSLC核心版本”,“2.0”);
试一试{
//请求根服务文档
HttpResponse rootServicesResponse=HttpUtils.sendGetForSecureDocument(
服务器、rootServiceDoc、登录、密码、httpclient、JTS_服务器);
if(rootServicesResponse.getStatusLine().getStatusCode()==200){
//定义XPath计算环境
XPathFactory=XPathFactory.newInstance();
XPath=factory.newXPath();
xpath.setNamespaceContext(
新建NamespaceContextMap(新字符串[])
{“rdf”http://www.w3.org/1999/02/22-rdf-syntax-ns#",
“奥斯陆大学我们”http://open-services.net/xmlns/we/1.0/"}));
//解析响应主体以检索目录URI
InputSource source=新的InputSource(rootServicesResponse.getEntity().getContent());
Node属性=(Node)(xpath.evaluate(catalogXPath,source,XPathConstants.Node));
字符串serviceProvidersCatalog=attribute.getTextContent();
rootServicesResponse.getEntity().consumeContent();
//设置目录请求
HttpGet catalogDoc=新的HttpGet(serviceProvidersCatalog);
catalogDoc.addHeader(“接受”、“应用程序/xml”);
catalogDoc.addHeader(“OSLC核心版本”,“2.0”);
//访问服务提供商目录
HttpResponse catalogResponse=HttpUtils.sendGetForSecureDocument(
服务器、catalogDoc、登录、密码、httpclient、JTS_服务器);
如果(catalogResponse.getStatusLine().getStatusCode()==200){
//定义XPath计算环境
XPath xpath2=factory.newXPath();
xpath2.setNamespaceContext(
新建NamespaceContextMap(新字符串[])
{“奥斯陆”http://open-services.net/ns/core#",
“dcterms”http://purl.org/dc/terms/"}));
//解析响应主体以检索服务提供者
source=新的InputSource(catalogResponse.getEntity().getContent());
节点列表标题节点=(节点列表)(xpath2.evaluate(serviceProviderTitleXPath,source,XPathConstants.NODESET));
//打印出每个服务提供商的标题
int length=titleNodes.getLength();
System.out.println(“>>项目区域:”);
for(int i=0;i>\t-”+titleNodes.item(i).getTextContent());
}
}
}
rootServicesResponse.getEntity().consumeContent();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}捕获(XPathExpressionException e){
e、 printStackTrace();
}捕获(无效凭证例外情况e){
e、 printStackTrace();
}最后{
//关闭HTTP连接
httpclient.getConnectionManager().shutdown();
}
}
}确保登录()函数是公共的和静态的。请查看您正在使用的培训模块的示例代码。我刚刚发现了这一点。有趣的是,每个人在这个问题上沉默了两个月。对我来说,这一点并不明显,因为它在某些项目中有效,而在其他项目中根本不起作用。机器也是如此。它在一个地方有效,但是不是另一个(如果你不知道发生了什么) 检查.project文件,确保其中包含正确的buildCommand标记
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.worklight.studio.plugin.WorklightProjectBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
org.eclipse.jdt.core.javabuilder
不是答案,但请查看以下内容是否会给您任何提示(请参阅问题的答案:)登录是否为静态登录
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.worklight.studio.plugin.WorklightProjectBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>