为什么 Apache Tomcat 看不到 mp3 文件?

Why Apache Tomcat does not see mp3 file?

我开发的网站必须播放音乐。但是 tomcat 没有看到我想听的 mp3 文件。

这是我的一部分 internalAccountTemplate.xhtml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:p="http://primefaces.org/ui"
    xmlns:ui="http://java.sun.com/jsf/facelets">
<f:view contentType="text/html">
    <h:head>
        <title><ui:insert name="title" /></title>
    </h:head>
    <h:body styleClass="defaultStyle" id="body">
        <audio id="customAudio" preload='none'> 
            <source src='templates/KnifeParty.mp3' type='audio/mpeg' /> 
        </audio>
    </h:body>
</f:view>
</html>

当我按下 播放 按钮时,我收到下一条错误消息
/WEB-INF/templates/KnifeParty.xhtml Not Found in ExternalContext as a Resource

我的文件夹结构:

我还有 NewFile1.xhtml,效果很好。但是我在 firefox 中打开的这个文件 (NewFile1.xhtml) 不是在 Tomcat Server

上打开的
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Xhtml work</title>
</head>
<body>
    <audio id="yourAudio" preload='none'> <source
        src='templates/KnifeParty.mp3' type='audio/mpeg' /> </audio>
    <a href="#" id="audioControl">play!</a>
    <script type="text/javascript">
        var yourAudio = document.getElementById('yourAudio'), ctrl = document
                .getElementById('audioControl');

        ctrl.onclick = function() {

            // Update the Button
            var pause = ctrl.innerHTML === 'pause!';
            ctrl.innerHTML = pause ? 'play!' : 'pause!';

            // Update the Audio
            var method = pause ? 'pause' : 'play';
            yourAudio[method]();

            // Prevent Default Action
            return false;
        };
    </script>
</body>
</html>

在网上我看到了类似的问题 (like this),有人说 在 Tomcat 服务器上注册扩展

我写了 KnifeParty.mp3 文件的不同路径并更改了他的位置,但这没有帮助。

/WEB-INF 中的资源不可公开访问。您需要将可公开访问的资源放在 /WEB-INF 之外。 /WEB-INF 应该只用于配置文件、模板文件、包含文件、标记文件等,这些文件应该 而不是 可以公开访问的。

像这样将其移动到 /WEB-INF 的父文件夹后,

WebContent
 |-- META-INF
 |-- WEB-INF
 `-- some.mp3

那么您可以参考如下:

<audio ...> 
    <source src="#{request.contextPath}/some.mp3" type="audio/mpeg" />
</audio>

您还可以通过将 JSF 资源管理工具放在 JSF 专用 /resources 文件夹中(对于 JS/CSS/image 文件,您应该已经拥有该文件夹)来使用它,

WebContent
 |-- META-INF
 |-- WEB-INF
 `-- resources
      `-- audio
           `-- some.mp3

然后你可以在EL中使用#{resource}映射来引用它,如下所示:

<audio ...>
    <source src="#{resource['audio/some.mp3']}" type="audio/mpeg" />
</audio>

另请参阅:

  • Which XHTML files do I need to put in /WEB-INF and which not?
  • How to reference CSS / JS / image resource in Facelets template?