Unable to get servletcontext path in jsf

一个人想着一个人 提交于 2019-12-20 03:29:09

问题


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>

and in my jsf page

<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>

how to get the resources path.

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);

回答1:


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" />

At the java side, you can get the context path as

String contextPath = FacesContext.getCurrentInstance().getExternalContext().getContextName();

Hope this helps.



来源:https://stackoverflow.com/questions/19043569/unable-to-get-servletcontext-path-in-jsf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!