简体   繁体   English

使用jquery和play framework 2.0

[英]Using jquery with play framework 2.0

I want to use jQuery to open a lightbox but it is causing the problem. 我想使用jQuery打开一个灯箱,但它导致了问题。 Here is my Application.java code: 这是我的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

It gives me the error 它给了我错误

ReferenceError: $ is not defined showed in firebug. ReferenceError:$未在firebug中显示。 i even tried changing $ with jQuery but still it doesnt work at all. 我甚至尝试用jQuery更改$但仍然无法正常工作。 Also i saw that the jQuery gets loaded in the head section. 我也看到jQuery被加载到head部分。 Do help me with this. 帮帮我这个。

conf/routes file: 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)

I think that your JS paths are wrong. 我认为你的JS路径是错误的。

If you use the default Play config, your Javascript paths should look like: 如果您使用默认的Play配置,您的Javascript路径应如下所示:

<script type="text/javascript" src="/assets/javascripts/jquery-1.7.2.min.js"></script>
...

And ever better, using the reverse routing, you should use: 更好的是,使用反向路由,你应该使用:

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

The static assets are provided through a special route defined in your conf/routes file: 静态资产是通过conf/routes文件中定义的特殊路由提供的:

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

This route simply makes a binding between your local public folder and the /assets Url. 这条路线只是使您的本地之间的结合public文件夹和/assets的URL。

This is the new way to reference assets: 这是引用资产的新方法:

@routes.Assets.versioned("javascripts/jquery-3.1.1.js")

More info here: https://www.playframework.com/documentation/2.5.x/Assets 更多信息: https//www.playframework.com/documentation/2.5.x/Assets

here is the simple example of Jquery using ajax in play framework... Configure the Route 1st 这是Jquery在play框架中使用ajax的简单示例...配置Route 1st

Routes 路线

This file defines all application routes (Higher priority routes first) 此文件定义所有应用程序路由(优先级较高的路由优先

~~~~ ~~~~

Home page GET / controllers.Application.index GET /newUser controllers.Application.newUserSign GET /login 主页GET / controllers.Application.index GET / newUser controllers.Application.newUserSign GET / login

controllers.Application.signInUser GET /newUserSignUP controllers.Application.signInUser GET / newUserSignUP
controllers.Application.newUserSignUP1 POST /newUserSignUP controllers.Application.newUserSignUP1 POST / newUserSignUP
controllers.Application.newUserSignUP POST /signIn controllers.Application.newUserSignUP POST / signIn
controllers.Application.signIn POST /userDetail controllers.Application.signIn POST / userDetail
controllers.Application.userDetail GET /logout controllers.Application.userDetail GET / logout
controllers.Application.logout POST /sendRequest controllers.Application.logout POST / sendRequest
controllers.Application.sendRequest POST /friendDetail controllers.Application.sendRequest POST / friendDetail
controllers.Application.friendList POST /requestList controllers.Application.friendList POST / requestList
controllers.Application.requestList POST /acceptRequest controllers.Application.requestList POST / acceptRequest
controllers.Application.acceptRequest GET /shwUserDetail controllers.Application.shwUserDetail controllers.Application.acceptRequest GET / shwUserDetail controllers.Application.shwUserDetail

And the application is as 应用程序如下

package controllers 包控制器

import models.User import play.api._ import play.api.mvc._ import net.liftweb.json._ import net.liftweb.json.JsonDSL import net.liftweb.json.Serialization.write import 导入models.User import play.api._ import play.api.mvc._ import net.liftweb.json._ import net.liftweb.json.JsonDSL import net.liftweb.json.Serialization.write import

scala.collection.mutable.ListBuffer scala.collection.mutable.ListBuffer

object Application extends Controller { implicit val format = DefaultFormats def index = Action { Ok(views.html.index()) } def newUserSign = Action { Ok(views.html.newUserSignUpForm()) } def newUserSignUP = Action { implicit request => val a = request.body.asFormUrlEncoded val id = a.get("id").head val fnm = a.get("fnm").head val lnm = a.get("lnm").head val email = a.get("email").head val res = a.get("res").head val num = a.get("num").head val pwd = a.get("pwd").head object Application extends Controller {implicit val format = DefaultFormats def index = Action {Ok(views.html.index())} def newUserSign = Action {Ok(views.html.newUserSignUpForm())} def newUserSignUP = Action {implicit request = > val a = request.body.asFormUrlEncoded val id = a.get(“id”)。head val fnm = a.get(“fnm”)。head val lnm = a.get(“lnm”)。head val email = a.get(“email”)。head val res = a.get(“res”)。head val num = a.get(“num”)。head val pwd = a.get(“pwd”)。head

 val obj = User(id, fnm, lnm, email, res, num, pwd); if (models.UserModel.create(obj) > 0) { Ok(views.html.newUserWlcmPage()) } else { Ok(views.html.Error()) } //Ok(" congratulation ur finally registered.. your id is " + id + " & your name is => " + fnm + " And you lives in ::" + res) } def newUserSignUP1 = Action { Ok(views.html.newUserWlcmPage()) } def signIn = Action { implicit request => val a = request.body.asFormUrlEncoded val id = a.get("id").head val pwd = a.get("pwd").head val data = models.UserModel.userDetail(id, pwd) 

Redirect(routes.Application.shwUserDetail).withSession("id" -> id) 重定向(routes.Application.shwUserDetail).withSession(“id” - > id)

// Ok(views.html.userInfoPage(data)).withSession("id" -> id) } // Ok(views.html.userInfoPage(data))。withSession(“id” - > id)}

def signInUser = Action { Ok(views.html.login()) } def shwUserDetail = Action { implicit request => val id = request.session.get("id").get val usrDetail = models.UserModel.userDetail(id) Ok(views.html.userInfoPage(usrDetail)) } def userDetail = Action { implicit request => val b = request.body.asFormUrlEncoded if (!session.get("id").isEmpty) { val id = session.get("id").get //val password = b.get("pwd").head def signInUser = Action {Ok(views.html.login())} def shwUserDetail = Action {implicit request => val id = request.session.get(“id”)。get val usrDetail = models.UserModel.userDetail(id )Ok(views.html.userInfoPage(usrDetail))} def userDetail = Action {implicit request => val b = request.body.asFormUrlEncoded if(!session.get(“id”)。isEmpty){val id = session。 get(“id”)。get // val password = b.get(“pwd”)。head

  val data = models.UserModel.getAllUserDetail(id) Ok(write(data)) } else { Ok("") } } def sendRequest = Action { implicit request => val b = request.body.asFormUrlEncoded.get val receiver_id = b.get("receiver_id").get(0) val sender_id = request.session.get("id").get val data = models.UserModel.friendRequest(sender_id, receiver_id) Ok(write(Map("sucess"->true))) } def logout() = Action { println("You are successfully logout") Ok(views.html.logout()).withNewSession } def friendList = Action { implicit request => val b = request.body.asFormUrlEncoded 

val sender_id = request.session.get("id").get val data = models.UserModel.friendList(sender_id) val sender_id = request.session.get(“id”)。get val data = models.UserModel.friendList(sender_id)

 Ok(views.html.requestConfirmation()) 

} def requestList = Action { implicit request => val sender_id = request.session.get("id").get val data = models.UserModel.requestList(sender_id) Ok(write(data)) def requestList = Action {implicit request => val sender_id = request.session.get(“id”)。get val data = models.UserModel.requestList(sender_id)Ok(write(data))

  } def acceptRequest = Action { implicit request => val b = request.body.asFormUrlEncoded.get val friend_id = b.get("friend_id").get(0) val user_id = request.session.get("id").get val data = models.UserModel.acceptRequest(friend_id, user_id) Ok(write(Map("sucess"->true))) } } 

And the mole is as 痣就像

package models import play.api.db._ import play.api.Play.current 包模型导入play.api.db._ import play.api.Play.current

import anorm._ import anorm.SqlParser._ import anorm._ import anorm.SqlParser._

case class User(id: String, fnm: String,lnm: String, email: String, res: String,num: String, pwd: String) case class User1(sender_id:String, receiver_id:String) object UserModel { case class User(id:String,fnm:String,lnm:String,email:String,res:String,num:String,pwd:String)case class User1(sender_id:String,receiver_id:String)object UserModel {

def create(obj: User) = { def create(obj:User)= {

 DB.withConnection { implicit Connection => val data = SQL("insert into user_detail values({id},{fnm},{lnm},{email},{res},{num},{pwd})").on("id" -> 

obj.id, "fnm" -> obj.fnm, "lnm" -> obj.lnm,"email" -> obj.email,"res" -> obj.res, "num" -> obj.num, "pwd" -> obj.pwd).executeUpdate() obj.id,“fnm” - > obj.fnm,“lnm” - > obj.lnm,“email” - > obj.email,“res” - > obj.res,“num” - > obj.num,“ pwd“ - > obj.pwd).executeUpdate()

  data } } def userDetail(id: String, pwd: String): List[User] = { DB.withConnection { implicit Connection => val dat = SQL("select * from user_detail where id='" + id + "' and pwd='" + pwd +"'") var data = dat().map(row => User(row[String]("id"),row[String]("fnm"), row[String]("lnm"), row[String]("email"),row[String]("res"),row[String]("num"),row[String]("pwd"))).toList data } } def userDetail(id: String): List[User] = { DB.withConnection { implicit Connection => val dat = SQL("select * from user_detail where id='" + id + "'") var data = dat().map(row => User(row[String]("id"),row[String]("fnm"), row[String]("lnm"), row[String]("email"),row[String]("res"),row[String]("num"),row[String]("pwd"))).toList data } } def friendRequest(sender_id:String,receiver_id:String)={ DB.withConnection{ implicit c=> val result=SQL("insert into request_table values ({sender_id},{receiver_id})").on("sender_id"->sender_id,"receiver_id"->receiver_id).executeInsert() println("------------------------------------\\n"+result); result } } def getAllUserDetail(id: String) = { DB.withConnection { implicit Connection => val dat = SQL("select * from user_detail where id!='" + id +"'") var data = dat().map(row => User(row[String]("id"),row[String]("fnm"), row[String]("lnm"), row[String]("email"),row[String]("res"),row[String]("num"),row[String]("pwd"))).toList data } } def friendList(user_id: String) = { DB.withConnection { implicit Connection => val dat = SQL("select * from friend_tbl where id!='" + user_id +"'") var data = dat().map(row => User(row[String]("id"),row[String]("fnm"), row[String]("lnm"), row[String]("email"),row[String]("res"),row[String]("num"),row[String]("pwd"))).toList data } } def requestList(sender_id: String) = { DB.withConnection { implicit Connection => val dat = SQL("select receiver_id from request_table where sender_id='" + sender_id +"'") var data = dat().map(row => row[String]("receiver_id")).toList data } } def acceptRequest(friend_id:String, user_id:String)={ DB.withConnection{ implicit c= 

Blockquote 大段引用

  val result=SQL("insert into friend_tbl values ({friend_id},{user_id})").on("friend_id"->friend_id,"user_id"->user_id).executeInsert() println("------------------------------------\\n"+result); result } } } 

and after that configure the view page enter code here 然后配置视图页面enter code here

Map static resources from the /public folder to the /assets URL path GET /assets/*file controllers.Assets.at(path="/public", file) 将静态资源从/ public文件夹映射到/ assets URL路径GET / assets / *文件controllers.Assets.at(path =“/ public”,file)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM