使用jquery和play framework 2.0

Mil*_*Inc 4 java jquery playframework-2.0

我想使用jQuery打开一个灯箱,但它导致了问题.这是我的Application.java代码:

    @(products: List[Products])

    @import helper._

        <script type="text/javascript" src="../public/javascripts/jquery-1.7.2.min.js"></script>
        <!-- Add mousewheel plugin (this is optional) -->
        <script type="text/javascript" src="../public/javascripts/jquery.mousewheel-3.0.6.pack.js"></script>

        <!-- Add fancyBox main JS and CSS files -->
        <script type="text/javascript" src="../public/javascripts/jquery.fancybox.js?v=2.0.6"></script>
        <link rel="stylesheet" type="text/css" href="../public/stylesheets/jquery.fancybox.css?v=2.0.6" media="screen" />


        <script type="text/javascript">
        $(document).ready(function() {

$('.fancybox').fancybox();

// my code
           });
// rest of the code
Run Code Online (Sandbox Code Playgroud)

它给了我错误

ReferenceError:$未在firebug中显示.我甚至尝试用jQuery更改$但仍然无法正常工作.我也看到jQuery被加载到head部分.帮帮我这个.

conf/routes文件:

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

# Home page
#GET     /                           controllers.Application.index()

# Map static resources from the /public folder to the /assets URL path
GET     /assets/*file               controllers.Assets.at(path="/public", file)

# Products list (to fetch the list of all the products)
GET     /products                controllers.Application.list

GET     /products/:id           controllers.Application.findAll1(id:Integer)
Run Code Online (Sandbox Code Playgroud)

nde*_*rge 8

我认为你的JS路径是错误的.

如果您使用默认的Play配置,您的Javascript路径应如下所示:

<script type="text/javascript" src="/assets/javascripts/jquery-1.7.2.min.js"></script>
...
Run Code Online (Sandbox Code Playgroud)

更好的是,使用反向路由,你应该使用:

<script type="text/javascript" src="@routes.Assets.at("/javascripts/jquery-1.7.2.min.js")"></script>
...
Run Code Online (Sandbox Code Playgroud)

静态资产通过conf/routes文件中定义的特殊路由提供:

# Map static resources from the /public folder to the /assets URL path
GET     /assets/*file               controllers.Assets.at(path="/public", file)
Run Code Online (Sandbox Code Playgroud)

此路由只是在本地public文件夹和/assetsUrl 之间建立绑定.