JSP 未检测到 javascript 文件

发布于 2024-09-25 05:56:55 字数 1589 浏览 7 评论 0原文

我从 servlet 将请求转发到呈现 FusionChart 的 JSP 页面。 但我在加载图表时遇到问题。 JSP 文件未检测到 JavaScript 文件。文件夹结构为:

axis
 |
 WebContent
      |
     WEB-INF
        |
       classes
         |_ com 
         |_FusionCharts.js
         |_MyChartJsp.jsp
         |_Line.swf

JSP 代码:

<html>
<head>
<script language="text/javascript" src="/WEB-INF/classes/FusionCharts.js"></script>
</head>
<body bgcolor="#ffffff">
<div id="chartdiv" align="left">The chart will appear within
this DIV. This text will be replaced by the chart.</div>
<script type="text/javascript">

var foo = //value fetched from DAO
var myChart = new FusionCharts("/WEB-INF/classes/Line.swf",
            "myChartId", "1000", "500");

    myChart
            .setDataXML("<graph caption='aCaption' xAxisName='xAxis' yAxisName='yAxis' showNames='1' decimalPrecision='0' formatNumberScale='0'>"+foo+"</graph>");
    myChart.render("chartdiv");
</script>

</body>
</html>

转发请求的 Servlet 代码:

final RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/classes/MyChartJsp.jsp");
requestDispatcher.forward(request, response);

请求被转发到 JSP。但图表没有显示,因为它无法弄清楚

var myChart = new FusionCharts("/WEB-INF/classes/Line.swf",
                "myChartId", "1000", "500");

我尝试过

src="/FusionCharts.js"

src="FusionCharts.js"

但没有运气的行中的 FusionCharts 是什么。

这与转发的请求有关吗?

From a servlet, I'm forwarding the request to a JSP page which renders a FusionChart.
But I've a problem in loading the chart. The JSP file is not detecting the JavaScript file. The folder structure is:

axis
 |
 WebContent
      |
     WEB-INF
        |
       classes
         |_ com 
         |_FusionCharts.js
         |_MyChartJsp.jsp
         |_Line.swf

And the JSP code:

<html>
<head>
<script language="text/javascript" src="/WEB-INF/classes/FusionCharts.js"></script>
</head>
<body bgcolor="#ffffff">
<div id="chartdiv" align="left">The chart will appear within
this DIV. This text will be replaced by the chart.</div>
<script type="text/javascript">

var foo = //value fetched from DAO
var myChart = new FusionCharts("/WEB-INF/classes/Line.swf",
            "myChartId", "1000", "500");

    myChart
            .setDataXML("<graph caption='aCaption' xAxisName='xAxis' yAxisName='yAxis' showNames='1' decimalPrecision='0' formatNumberScale='0'>"+foo+"</graph>");
    myChart.render("chartdiv");
</script>

</body>
</html>

The Servlet code to forward the request:

final RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/classes/MyChartJsp.jsp");
requestDispatcher.forward(request, response);

The request is getting forwarded to the JSP. But the chart is not getting displayed because it is unable to figure out what FusionCharts is in the line

var myChart = new FusionCharts("/WEB-INF/classes/Line.swf",
                "myChartId", "1000", "500");

I tried

src="/FusionCharts.js"

src="FusionCharts.js"

but no luck.

Has it something to do with the request being forwarded??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

握住你手 2024-10-02 05:56:55

WEB-INF 中不能有 .js(或 .swf、.jpg 等)文件 - 它们不可公开访问。
将其移至 /js/

没有理由在 WEB-INF 中隐藏静态资源(如脚本和 css)。如果您坚持这样做,您应该创建一个 servlet,根据 js/css 的名称,从其位置读取它并将其作为响应。这就是访问静态资源时默认 servlet 所做的事情。

页面加载的流程如下:浏览器向servlet发送请求; Servlet 在内部转发到 JSP,JSP 被呈现为响应;然后浏览器解析

然后,要使脚本 url 固定到 servlet 上下文根,请使用

src="<c:url value="/js/script.js" />"

无论当前 url 是什么,这都会起作用

You cannot have .js (or .swf, .jpg, etc.) files in WEB-INF - they are not publically accessible.
Move it to /js/

There is no reason to hide static resources (like scripts and css) in WEB-INF. If you insist on that, you should make a servlet that, given the name of the js/css, would read it from its location and will serve it as a response. This is what the default servlet does when you access static resources.

The flow of the page loading is as follows: the browser sends a request to the servlet; the servlet forwards internally to the JSP, and the JSP is rendered as a response; then the browser parses the <script> tag and fires another request to the script. If the script is not accessible via URL, it's not loaded.

Then, to make the script url fixed to the servlet context root, use

src="<c:url value="/js/script.js" />"

This will work regardless of what is the current url

时光病人 2024-10-02 05:56:55

不是问题的原因,但还要注意您的

(我试图将此作为评论发布,但由于某种原因它不允许我这样做。)

Not the cause of your problem, but also note that your <script> element is incorrect. It should be <script type="text/javascript"....

(I tried to post this as a comment, but for some reason it wouldn't let me.)

年华零落成诗 2024-10-02 05:56:55

我面临着同样的问题。就我而言,当我直接调用 myFile.jsp 时,它会读取 myFile.js;

但是当通过登录调用时-> myFile.jsp,它不读取 myFile.js;

通过开发者工具分析路径后:=>控制台,我发现它插入了uri,所以最终路径不正确。

最终解决方案:

我对所有 .js 和 .css 使用了绝对路径。现在它从四面八方打来。

我的项目结构是:

在我的 servlet-context.xml 中

i) <context:component-scan base-package="com.SBP.SHWeb" />

ii) <resources mapping="/resources/**" location="/resources/" />

我的旧路径包含 .js 是:/resources/MyJs/myfile.js ===>有时它不会被调用。

我的绝对路径,从所有地方调用都是这样的:

/SHweb/resources/MyJs/myfile.js ==>它从各处被调用。

希望对您有帮助。

I was facing same issue. In my case when I calling the myFile.jsp directly its reading the myFile.js;

But when calling through login-> myFile.jsp, its not reading the myFile.js;

After analyzing the path through the Developer tools :=> console, I found that its inserting the uri, so final path was incorrect.

Final Solution:

I'd used the absolute path for all .js and .css. Now its called from everywhere.

My Project Structure is:

In my servlet-context.xml

i) <context:component-scan base-package="com.SBP.SHWeb" />

ii) <resources mapping="/resources/**" location="/resources/" />

My old path for including .js was: /resources/MyJs/myfile.js ===> Its not get called sometimes.

My Absolute path, which get called from all places is like this:

/SHweb/resources/MyJs/myfile.js ==> Its get called from everywhere.

Hope it help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文