将外部 Java 库添加到 Adobe/Day CRX (2.2) 以在 JSP 文件中使用
我的公司正在考虑使用 Adobe CQ5 作为新的内容管理系统,我的任务是弄清楚如何用它来做某些事情。
我们想做的一件事是在 JSP 页面的 scriptlet 中使用我们为旧 Web 应用程序创建的一些 JAR,可能会调用服务或其他内容。
我考虑过使用 OSGi 包来做到这一点,但我认为这不是我们想要做的。 现在,我无法确定将要在 JSP 文件中使用的外部库放在哪里。
我创建了一个 JAR,它只有一个通用类,
package org.company.test;
import java.lang.String;
public class TestService{
private String myString;
public TestService(String input){myString = input;}
public String getMyString(){return myString;}
}
这是 jar 中唯一的东西。
我尝试将其放入 /crx-quickstart/server/lib/common 中
,该文件夹的自述文件显示
“将库放入此文件夹中,该文件夹应在所有 Web 应用程序和服务器之间共享。”
但是当我尝试访问我的 JSP 文件时,它出现了一些问题。这是完整的 JSP 文件:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ TR/html4/loose.dtd">
<%%>
<%@page session="false"%>
<%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0"%>
<sling:defineObjects/>
<%@ page import="javax.jcr.Repository, javax.jcr.Session, javax.jcr.SimpleCredentials, javax.jcr.Node, javax.jcr.NodeIterator,
java.net.URLEncoder, java.util.List, java.util.Iterator, javax.jcr.Value, javax.jcr.RepositoryException,org.company.test.TestService;"%>
<html>
<head>
<style type="text/css"><jsp:include page="/content/myBlog/style.css"/></style>
<!--<link rel="stylesheet" href="/content/myBlog/style.css" type="text/css">-->
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<%
TestService jServ = new TestService("TigerBlood");
String returnValue = "Failed";
if(jServ!=null){
returnValue = jServ.getMyString();
}
%>
<title><%= returnValue %></title>
</head>
<body>
<a href="/content/myBlog.html" class="imgcontainer"><img src="/apps/myBlog/myBlog.png" width="80px" height="60px" border="0" alt="myBlog" /></a>
<h1><%= returnValue %></h1>
<div class="body">
<br>
<a href="/apps/myBlog/comment.html">Comment</a>
</div>
</body>
</html>
当我在浏览器中导航到它时,出现以下错误:
Unable to compile class for JSP: An error occurred at line: 16 in the generated java file Only a type can be imported. org.company.test.TestService resolves to a package
An error occurred at line: 17 in the jsp file: /apps/myBlog/test.jsp TestService cannot be resolved to a type
14: <!--<link rel="stylesheet" href="/content/myBlog/style.css" type="text/css">-->
15: <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
16: <%
17: TestService jServ = new TestService("TigerBlood");
18: String returnValue = "Failed";
19: if(jServ!=null){ 20: returnValue = jServ.getMyString();
An error occurred at line: 17 in the jsp file: /apps/myBlog/test.jsp TestService cannot be resolved to a type
14: <!--<link rel="stylesheet" href="/content/myBlog/style.css" type="text/css">-->
15: <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
16: <%
17: TestService jServ = new TestService("TigerBlood");
18: String returnValue = "Failed";
19: if(jServ!=null){
20: returnValue = jServ.getMyString();
我可以通过将导入更改为 com.company.test.* 来修复有关包的第一个错误,但它不能修复其他错误两个错误。
我知道这不是使用服务的最佳“实践”,但我希望得到一个概念证明,表明我们可以根据需要使用这些库。任何帮助将不胜感激。
my company is looking into using Adobe CQ5 as a new content management system and I've been given the task of figuring out how to do certain things with it.
One thing we would like to do is use some JARs we created for our old web-apps in scriptlets within our JSP pages, possibly to call a service or something.
I looked into doing this with OSGi bundles but I don't think it's what we want to do.
Right now I'm having trouble figuring out where to put external libraries to be used in our JSP files.
I created a JAR that just has a generic class
package org.company.test;
import java.lang.String;
public class TestService{
private String myString;
public TestService(String input){myString = input;}
public String getMyString(){return myString;}
}
That is the only thing in the jar.
I tried putting it in /crx-quickstart/server/lib/common
which the README for that folder says
"Put libraries in this folder that should be shared among all web applications and the server."
but my JSP file has a few issues when I try to access it. This is the JSP file in its entirety:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ TR/html4/loose.dtd">
<%%>
<%@page session="false"%>
<%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0"%>
<sling:defineObjects/>
<%@ page import="javax.jcr.Repository, javax.jcr.Session, javax.jcr.SimpleCredentials, javax.jcr.Node, javax.jcr.NodeIterator,
java.net.URLEncoder, java.util.List, java.util.Iterator, javax.jcr.Value, javax.jcr.RepositoryException,org.company.test.TestService;"%>
<html>
<head>
<style type="text/css"><jsp:include page="/content/myBlog/style.css"/></style>
<!--<link rel="stylesheet" href="/content/myBlog/style.css" type="text/css">-->
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<%
TestService jServ = new TestService("TigerBlood");
String returnValue = "Failed";
if(jServ!=null){
returnValue = jServ.getMyString();
}
%>
<title><%= returnValue %></title>
</head>
<body>
<a href="/content/myBlog.html" class="imgcontainer"><img src="/apps/myBlog/myBlog.png" width="80px" height="60px" border="0" alt="myBlog" /></a>
<h1><%= returnValue %></h1>
<div class="body">
<br>
<a href="/apps/myBlog/comment.html">Comment</a>
</div>
</body>
</html>
When I navigate to it in a browser I get these errors:
Unable to compile class for JSP: An error occurred at line: 16 in the generated java file Only a type can be imported. org.company.test.TestService resolves to a package
An error occurred at line: 17 in the jsp file: /apps/myBlog/test.jsp TestService cannot be resolved to a type
14: <!--<link rel="stylesheet" href="/content/myBlog/style.css" type="text/css">-->
15: <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
16: <%
17: TestService jServ = new TestService("TigerBlood");
18: String returnValue = "Failed";
19: if(jServ!=null){ 20: returnValue = jServ.getMyString();
An error occurred at line: 17 in the jsp file: /apps/myBlog/test.jsp TestService cannot be resolved to a type
14: <!--<link rel="stylesheet" href="/content/myBlog/style.css" type="text/css">-->
15: <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
16: <%
17: TestService jServ = new TestService("TigerBlood");
18: String returnValue = "Failed";
19: if(jServ!=null){
20: returnValue = jServ.getMyString();
I can fix the first error about the package by changing the import to com.company.test.* but it does not fix the other two errors.
I know this is not the best 'practice' for using services but I was hoping to just get a Proof of Concept going that shows we could use those libraries if we wanted. Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了答案,您必须将外部库捆绑为 OSGi 捆绑包。
该过程可以在此处找到,
但令人惊讶的是很难找到虽然这比仅仅添加 JAR 更耗时,但它确实有效。
I've found the answer, you have to bundle external libraries as an OSGi bundle.
The process can be found here
It was surprisingly hard to find that answer and although it's more time consuming than just adding a JAR, it works.
我们使用“maven-bundle-plugin”使用 Maven 构建 cq 包。您可以使用它来创建您自己的具有正确导出说明的包。
在“Export-Package”标签中,您应该包含您想要在 jsp 中使用的所有包。
另外,如果你在bundle中使用任何外部jar,你应该在相应的maven pom中指向依赖项。
例如对于 gson:
We build our cq bundles with Maven, using "maven-bundle-plugin". You can use it to create your own bundle with correct export instructions.
In "Export-Package" tag you should include all packages you want using in your jsp.
Also you should point dependencies in corresponding maven pom if you use any external jars in bundle.
e.g. for gson: