在liferay portlet中使用jsp作为img src

时间:2015-04-28 21:34:18

标签: java jsp liferay portlet

我有一些代码检查xml节点值,如果值为true则显示图像:

<x:if select="$person/pictureprivate != 'false'">
    <c:set var="pidm">
        <x:out select="$person/@pidm" />
    </c:set>
    <img src="<%=renderRequest.getContextPath()%>/getPicture.jsp&amp;pidm=${pidm}"></img>
</x:if>

作为源的jsp是webservice的帖子:

int len;
int size = 1024;
byte[] buf;
OutputStream o = response.getOutputStream();
String pidm=request.getParameter("pidm");

//out.print("URL: " + urlProp + pidm);
URL url = new URL("myURL" + pidm);
URLConnection connection = url.openConnection();

connection.setDoOutput(true);

connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);

InputStream is = connection.getInputStream();


 if (is instanceof ByteArrayInputStream) {
  size = is.available();
  buf = new byte[size];
  len = is.read(buf, 0, size);
} 
else {
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  buf = new byte[size];
  while ((len = is.read(buf, 0, size)) != -1)
    bos.write(buf, 0, len);
  buf = bos.toByteArray();
}

 o.write(buf);  
 o.flush();
 o.close();

我知道webservice jsp有效,因为我在另一个应用程序中使用它。但是,图像不会加载到我的liferay portlet中,并在apache错误right click -> view image results中尝试the requested resource is not available。我以为我可能会错误地定义我的src路径,但我不确定。

1 个答案:

答案 0 :(得分:1)

renderRequest的{​​{1}}最有可能解析为门户网站的上下文,而您的插件有自己的上下文。事实上,即使getContextPath最有可能解析到门户网站(但请仔细检查):当提供portlet时,请求由门户网站处理。当您想要从portlet的webcontext中寻址资源时,您需要明确地解决它,并且要么对您的portlet的Web上下文进行硬编码(不优雅),要么找到如何解决它。

关注@ ramp的评论,检查request.getContextPathgetContextPathrenderRequest)实际解析的内容,并且您更接近解决方案。如果有疑问,现在只需用硬编码的上下文路径替换它,然后继续搜索动态路径。

另请注意,如果以您希望的方式请求,您将无权访问JSP中已登录的用户帐户。你没有在JSP中使用它,所以它应该没问题 - 但我只是想确保这一点也被理解