Can't get correct URL parameters,

25 views
Skip to first unread message

Chenguang He

unread,
Sep 30, 2012, 6:46:58 PM9/30/12
to lif...@googlegroups.com
Hey guys,
I got a very weird problem.
i use S.param("jobid"). it return Full("1")

S.param("jobid") also return Full("1")

I do not know why..

Chenguang He




Chenguang He

unread,
Sep 30, 2012, 7:36:01 PM9/30/12
to lif...@googlegroups.com
Sorry, I forgot to attach the code.

bject jobDetail {
  var id = S.param("jobid")
  var theJob = Jobs.find(By(Jobs.id, id.open_!.toLong)).open_!

  def render = "#title" #>  <p><strong>{theJob.title}</strong></p> &
               "#location" #> <p>{theJob.location}</p> &
               "#duration" #> <p>{theJob.duration}</p> &
               "#req" #> <p>{theJob.req}</p> &
               "#cha" #> <p>{theJob.chas}</p> &
               "#resp" #> <p>{theJob.respons}</p> &
               "#apply [onClick]" #> SHtml.ajaxInvoke(()=>JsCmds.RedirectTo("/jobapply.html?jobid="+theJob.id))

}

i use http://localhost:8080/jobDetail.html?jobid=2 to try to get the id=2

But the page also return the job with  id=1



Chenguang He



Diego Medina

unread,
Sep 30, 2012, 10:29:04 PM9/30/12
to lif...@googlegroups.com
On Sun, Sep 30, 2012 at 7:36 PM, Chenguang He <gao...@gmail.com> wrote:
> Sorry, I forgot to attach the code.
>
> object jobDetail {

Don;t use an object, you a class, by using an object, you are having
just one instance of this snippet across your complete application,
that means that visiting this url with one parameter value, will show
up on another browser window with the same value. You use object only
when there is no value stored in the snippet (in practice, I hardly
ever use an object)

> var id = S.param("jobid")

and once you move to a class, you also don;t need to use a var here,
if yo uare not changing the value of id, you should use a val

> var theJob = Jobs.find(By(Jobs.id, id.open_!.toLong)).open_!

Same here, no need for a var.
And then there is the open_!, when you see in Lift method that ends
with _!, it means "don;t use unless you really know what you are
doing". It is not safe to use open_!, because if the S.param(jobid)
does not have a value, then you will see an ugly Null pointer
exception on the screen (not what most users want to see)

but then you may ak, what do I di? I want to get the value from the
Box, yo ucan do something like:

val theJob = id.map{ jobId => Jobs.find(By(Jobs.id, jobId.toLong) }

but I think that will give you a Box[Box[Jobs]], and you really just
want Box[Jobs], so you can use:

val theJob = id.flatMap{ jobId => Jobs.find(By(Jobs.id, jobId.toLong) }

The use of flatMap there basically gets rid of the outer Box.

>
> def render = "#title" #> <p><strong>{theJob.title}</strong></p> &

then you do things like:

def render = "#title" #> <p><strong>{theJob.map(_.title.is)}</strong></p> &


theJob.map(_.title.is) is a Box[String] but there are implicit
conversions in Lift that convert from a Box[String] to something that
can be use in css selectors.

Note how I added then .is after title, calling .is will give you the
raw type of the column, meaning, a String, an Int, etc

> "#location" #> <p>{theJob.location}</p> &
> "#duration" #> <p>{theJob.duration}</p> &
> "#req" #> <p>{theJob.req}</p> &
> "#cha" #> <p>{theJob.chas}</p> &
> "#resp" #> <p>{theJob.respons}</p> &
> "#apply [onClick]" #>
> SHtml.ajaxInvoke(()=>JsCmds.RedirectTo("/jobapply.html?jobid="+theJob.id))
>
> }
>
> i use http://localhost:8080/jobDetail.html?jobid=2 to try to get the id=2
>
> But the page also return the job with id=1
>

If you are still having issues, please put together a sample app
https://www.assembla.com/wiki/show/liftweb/Posting_example_code
and I'll walk you through the steps to get it working as you want.

Regards,

Diego

>
>
> Chenguang He
>
>
>
>
> On Sep 30, 2012, at 5:46 PM, Chenguang He wrote:
>
> Hey guys,
> I got a very weird problem.
> I have a url http://localhost:8080/jobDetail.html?jobid=1
> i use S.param("jobid"). it return Full("1")
>
> But when i pass http://localhost:8080/jobDetail.html?jobid=2
> S.param("jobid") also return Full("1")
>
> I do not know why..
>
> Chenguang He
>
>
>
>
>
> --
> --
> Lift, the simply functional web framework: http://liftweb.net
> Code: http://github.com/lift
> Discussion: http://groups.google.com/group/liftweb
> Stuck? Help us help you:
> https://www.assembla.com/wiki/show/liftweb/Posting_example_code
>
>
>



--
Diego Medina
Lift/Scala Developer
di...@fmpwizard.com
http://www.fmpwizard.com

Chenguang He

unread,
Oct 1, 2012, 2:49:22 AM10/1/12
to lif...@googlegroups.com
Thank you for giving such a detail response !

Finally, i got it works!

Chenguang He



Aditya Vishwakarma

unread,
Oct 1, 2012, 5:50:49 AM10/1/12
to lif...@googlegroups.com
Hi

To add to what Diego said, You can use an Object here. Generally  using an object as a snippet is more efficient, since its only initiated once. While for a class snippet, an object is created for every page load. 

However, you captured the S.param("") values in object jobDetail's constructor, which is only called once. So the vars ids, theJob are only assigned values once. 

If you shift the assignment to the render function, they would work correctly, as render function is called for every page load. Like this.

object jobDetail {

  def render = 
  {
    val id = S.param("jobid")
    val theJob = Jobs.find(By(Jobs.id, id.open_!.toLong)).open_!
    
    "#title" #> <p><strong>{theJob.title}</strong></p> &
               "#location" #> <p>{theJob.location}</p> &
               "#duration" #> <p>{theJob.duration}</p> &
               "#req" #> <p>{theJob.req}</p> &
               "#cha" #> <p>{theJob.chas}</p> &
               "#resp" #> <p>{theJob.respons}</p> &
               "#apply [onClick]" #> SHtml.ajaxInvoke(()=>JsCmds.RedirectTo("/jobapply.html?jobid="+theJob.id))
}}


I hope this clears up how Snippet classes and objects are used by lift.

Aditya Vishwakarma

Chenguang He

unread,
Oct 1, 2012, 11:02:37 AM10/1/12
to lif...@googlegroups.com
Yea, i got it works. Thank you !

Chenguang He



Reply all
Reply to author
Forward
0 new messages