javascript 无法在 Liferay Portlet JSP 页面中包含 css 和 JS 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10253495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:11:04  来源:igfitidea点击:

Unable to include css and JS files in Liferay Portlet JSP Page

javascriptliferayportlet

提问by user1253847

I have download a Jquery Image Slider source and want to integarte with my JSP File

我已经下载了一个 Jquery Image Slider 源代码并想与我的 JSP 文件集成

This is my Folder Struture

这是我的文件夹结构

enter image description here

在此处输入图片说明

This is the way i am including them

这就是我包括他们的方式

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<link type="text/css" href="/css/jquery.ui.theme.css" rel="stylesheet" />
<link type="text/css" href="/css/jquery.ui.core.css" rel="stylesheet" />
<link type="text/css" href="/css/jquery.ui.slider.css" rel="stylesheet" />
<link rel="stylesheet" href="/css/style.css" type="text/css" media="screen"/>

I get this 404 in server console

我在服务器控制台中得到这个 404

20:50:04,625 WARN [404_jsp:109] /css/jquery.ui.theme.css
20:50:04,640 WARN [404_jsp:109] /css/jquery.ui.core.css
20:50:04,640 WARN [404_jsp:109] /css/jquery.ui.slider.css
20:50:04,656 WARN [404_jsp:109] /css/style.css
20:50:04,671 WARN [404_jsp:109] /js/cufon-yui.js
20:50:04,671 WARN [404_jsp:109] /js/GreyscaleBasic.font.js
20:50:04,687 WARN [404_jsp:109] /js/jquery.easing.1.3.js

回答by Mark

Put the links in liferay-portlet.xmland let Liferay load the css and javascript. That is the better way (e.g. if user wants put two portlets to one page).

把链接liferay-portlet.xml放进去,让 Liferay 加载 css 和 javascript。这是更好的方法(例如,如果用户想将两个 portlet 放在一个页面上)。

liferay-portlet.xml:

liferay-portlet.xml:

<header-portlet-css>/css/main.css</header-portlet-css>
<footer-portlet-javascript>/js/main.js</footer-portlet-javascript>

And Olaf is right, you must move the css- and js-folders (and images too) from WEB-INFto docrootfolder

Olaf 是对的,您必须将 css- 和 js- 文件夹(以及图像)从WEB-INFtodocroot文件夹中移动

回答by Olaf Kock

Several issues:

几个问题:

  • If I see this correctly in that thumbnail image that you posted, you have your css and js folder in WEB-INF: Nothing that's in WEB-INF can be requested by any browser, so you'll never be able to get it.
  • If you create docroot/css and docroot/js, you will still get 404, because you can't find the files on /css/style.css, but in /your-portlet/css/style.css - the generic way to address it in jsps is

    < link rel="stylesheet" href="<%=request.getContextPath()%>/css/style.css"/>
    
  • 如果我在您发布的缩略图中正确地看到了这一点,则您的 css 和 js 文件夹位于 WEB-INF 中:任何浏览器都无法请求 WEB-INF 中的任何内容,因此您将永远无法获取它。
  • 如果您创建 docroot/css 和 docroot/js,您仍然会得到 404,因为您无法在 /css/style.css 中找到这些文件,但在 /your-portlet/css/style.css 中 - 通用方式在jsps中解决它是

    < link rel="stylesheet" href="<%=request.getContextPath()%>/css/style.css"/>
    

Late Edit: requeston a portlet's JSP doesn't always work, because according to the JSP specification it's a HttpServletRequest. In the portal world, this is not often relative to the current path of your portlet. Thus, I'd recommend to go with Mark's answer rather than mine: liferay-portlet.xml contains files relative to the current portlet. Since Liferay 7.0, you may use an OSGi-Component's equivalent to liferay-portlet.xml.

后期编辑:requestportlet 的 JSP 并不总是有效,因为根据 JSP 规范,它是一个 HttpServletRequest。在门户世界中,这通常与您的 portlet 的当前路径无关。因此,我建议使用 Mark 的答案而不是我的答案:liferay-portlet.xml 包含与当前 portlet 相关的文件。从 Liferay 7.0 开始,您可以使用与 liferay-portlet.xml 等效的 OSGi 组件。