留言板

How to get real path to my deployed exploded war in Liferay 7?

Pernes Panta,修改在6 年前。

How to get real path to my deployed exploded war in Liferay 7?

New Member 帖子: 6 加入日期: 17-11-7 最近的帖子
I comme from liferay 6 and i want to migrate my portlets to liferay 7. I have alreaty some portlet migrate but i have a problem:
I can't more get access to my deployed exploed war folder.

Before i used this code below to access my deployed war folder:

FacesContext context = FacesContext.getCurrentInstance();
String realPathPrefix = context.getExternalContext().getRealPath(".") + "/WEB-INF/classes/";
File file = realPathPrefix + "/templates/myfiles.xml";


Now this code does not work anymore.
FacesContext.getCurrentInstance().getExternalContext().getRealPath(".") return NULL

can someone helhp me to fix that.

Note: The XML files contain email templates.
thumbnail
Olaf Kock,修改在6 年前。

RE: How to get real path to my deployed exploded war in Liferay 7?

Liferay Legend 帖子: 6403 加入日期: 08-9-23 最近的帖子
Pernes Panta:
I comme from liferay 6 and i want to migrate my portlets to liferay 7. I have alreaty some portlet migrate but i have a problem:
I can't more get access to my deployed exploed war folder.


Somebody from the faces team might give you a more appropriate and helpful answer - I can only go so far to let you know that you can never assume you get an exploded war file in the first place. The application server is free to not explode the war file, but serve it from a zip file or any non-file-storage it prefers. You can't assume that files are available as physical files on disk - but in a web application you can typicall get the resources through the classloader through the class' getResourceAsStream method

However, I'm aware that any other answer might be more helpful, especially with regards to the faces implementation. To me it feels like you're finally finding an issue with your old implementation that happened to work in the circumstances that you found, but not within the specification options that were otherwise possible.

When Liferay 7 deploys a WAR file, it transforms it transparently into an OSGi bundle and no longer deploys it as a web application to the application server.
thumbnail
Kyle Joseph Stiemann,修改在6 年前。

RE: How to get real path to my deployed exploded war in Liferay 7? (答复)

Liferay Master 帖子: 760 加入日期: 13-1-14 最近的帖子
Hi Pernes,
Olaf's answer is excellent and exactly right. Rather than using ExternalContext.getRealPath(), you should be able to call ExternalContext.getResourceAsStream() to obtain the contents of your file:

InputStream inputStream = externalContext
.getResourceAsStream("/WEB-INF/classes/templates/myfiles.xml");


Then you can read the contents of the inputStream in whatever way you prefer.

- Kyle
thumbnail
Olaf Kock,修改在6 年前。

RE: How to get real path to my deployed exploded war in Liferay 7?

Liferay Legend 帖子: 6403 加入日期: 08-9-23 最近的帖子
great - this also clarifies which class (and with it, which classloader) to choose. I'd have feared to grab the wrong one, thus I stayed very abstract... emoticon