无法在jsf中获取servletcontext路径 [英] Unable to get servletcontext path in jsf
问题描述
我写了一个liferay richfaces portlet.但是Iam无法在web-inf文件夹中获取css路径或图像. 我的portlet配置是
I have written a liferay richfaces portlet. But iam not able to get the css path or images in the web-inf folder. My portlet config is
<portlet>
<portlet-name>testLR6_PB3_RF4</portlet-name>
<instanceable>true</instanceable>
<render-weight>1</render-weight>
<ajaxable>true</ajaxable>
<header-portlet-css>/resources/css/style.css</header-portlet-css>
<header-portlet-javascript>/js/eims.js</header-portlet-javascript>
<footer-portlet-javascript>/js/fileupload.js</footer-portlet-javascript>
</portlet>
并在我的jsf页面
<link href="${facesContext.externalContext.requestContextPath}/resources/css/style.css" rel="stylesheet" type="text/css" />
<div class="floatleft1"><img src="${facesContext.externalContext.requestContextPath}/resources/images/eims_logo.jpg" /></div>
如何获取资源路径.
这是我的Java servlet上下文代码
this my java servletcontext code
FacesContext context = FacesContext.getCurrentInstance();
ServletContext servletContext= (ServletContext) context.getCurrentInstance().getExternalContext().getContext();
String path=servletContext.getRealPath("/");
MainBean mainBean = new MainBean();
mainBean.getUserBean().setUserPath(path);
推荐答案
看看如何访问我的文件放在WEB-INF文件夹中.这可能会有所帮助.此外,如果您使用的是JSF 2.0,Facelets,则可以简单地在jsf页面中将上下文根获取为
Have a look at how can i have access to my files that placed in WEB-INF folder. It might be of help. Besides, if you are using JSF 2.0, Facelets, you can simply get your context root in your jsf pages as
<ui:param name="root" value="#{request.contextPath}" />
以及您需要访问上下文根目录内的文件的任何地方,都可以作为一个假设的示例来访问它们
and wherever you need to access files inside your context root, you may access them as(hypothetical example)
<img src="#{root}/resources/images/sample.jpg" />
在Java端,您可以将上下文路径获取为
At the java side, you can get the context path as
String contextPath = FacesContext.getCurrentInstance().getExternalContext().getContextName();
希望这会有所帮助.
这篇关于无法在jsf中获取servletcontext路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!