A simple question about URL params

73 views
Skip to first unread message

Shark.Z

unread,
Feb 20, 2011, 4:50:27 AM2/20/11
to lif...@googlegroups.com
I wanna to achieve an effect like this:
When the user clicks an edit link in a post, the page redirects to a URL like: context/edit/postid,
context/edit is an edit page.
I defined it in Sitemap like this : Menu.i("Edit Page") / "pages" / "edit"

But I don't know which part of Lift controls the URL params, when I visit context/edit/1, I got a Not Found error.
Actually I wanna it calls a snippet and then I can pull out the post with the id from DB, and inject to template and return to user.
So what shall I do? I know Simply Lift has a similar example, didn't get it work.








Timothy Perrett

unread,
Feb 20, 2011, 5:03:56 AM2/20/11
to lif...@googlegroups.com
You need to use rewriting for this. Lift has good support for this type of thing, pretty sure there are examples on the wiki. Additionally, there are decent examples in Lift in Action.

On my mobile at the moment so can't paste a code example.

Hope that helps, Tim

Diego Medina

unread,
Feb 20, 2011, 7:21:25 AM2/20/11
to lif...@googlegroups.com

This is a running example where you visit a page like
http://127.0.0.1:8080/browser-details/2.4.0.1090

And it uses the template browser-details and it passes the 2.4.0.10 9 0 to the template.

Let me know if you have any questions.

Sent from my cell

https://github.com/fmpwizard/comet_rest_example

Diego Medina

On Feb 20, 2011 5:04 AM, "Timothy Perrett" <tim...@getintheloop.eu> wrote:

Timothy Perrett

unread,
Feb 20, 2011, 8:07:36 AM2/20/11
to lif...@googlegroups.com
Thats probably pretty confusing for someone who does not know what they are looking at. To the OP, please see here:


Cheers, Tim

Shark.Z

unread,
Feb 20, 2011, 9:37:02 AM2/20/11
to lif...@googlegroups.com
 I just read that article.
Seems URL rewriting doesn't work for me, here's my code:

    LiftRules.statelessRewrite.prepend(NamedPF("URLRewrite") {
      case RewriteRequest(
          ParsePath("Pages" :: "edit" :: postid :: Nil, _, _,_), GetRequest, _) =>
        RewriteResponse(
          "Pages/edit/postid" :: Nil, Map("postid" -> postid)
      )
    })


For example if I wanna visit: /context/editor/100, I got a NOT_FOUND page.


1. Should I  make that URL pattern accessible first? 
Like doing :
Menu.i("Edit Page") / "pages" / "edit" / ** ?
2. Should I define the statelessRewrite before or after the Sitemap definition?



Naftoli Gugenheim

unread,
Feb 20, 2011, 10:57:09 AM2/20/11
to liftweb, Shark.Z
If you want to visit /context/... then your pattern match has to be case ... ParsePath("context" ..., not "Pages".
Re #2, I highly doubt the order matters.





--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.

Shark.Z

unread,
Feb 20, 2011, 9:36:08 PM2/20/11
to lif...@googlegroups.com, Shark.Z
Regarding to the code posted, I don't have a exact context path, I use the Maven archetype construct the prototype of my project, so when I visit localhost:8080 it display the index page, I am sorry for the context mentioned earlier.
I still didn't get it work.
Is there any problem with my code?

    LiftRules.statelessRewrite.prepend(NamedPF("URL Rewrite") {

      case RewriteRequest(
          ParsePath("Pages" :: "edit" :: postid :: Nil, _, _,_), GetRequest, _) =>
        RewriteResponse(
          "Pages" :: "edit" :: postid :: Nil, Map("postid" -> postid) 
      )
    })


Naftoli Gugenheim

unread,
Feb 20, 2011, 11:11:04 PM2/20/11
to liftweb, Shark.Z
Sorry, I don't understand. You need to rewrite several kinds of urls?


Shark.Z

unread,
Feb 20, 2011, 11:38:43 PM2/20/11
to lif...@googlegroups.com, Shark.Z
Let me clarify it this way (Bad English Speaker :) ),
no context path is required any longer. I need to rewrite the url:
http://localhost:8080/Pages/edit/(postid)
using the following code:

LiftRules.statelessRewrite.
prepend(NamedPF("URL Rewrite") {
      case RewriteRequest(
          ParsePath("Pages" :: "edit" :: postid :: Nil, _, _,_), GetRequest, _) =>
        RewriteResponse(
          "Pages" :: "edit" :: postid :: Nil, Map("postid" -> postid) 
      )
    })

When the URL http://localhost:8080/Pages/edit/10 is visited, I wanna get the postid in my snippet.
but now I got a NOT_FOUND error.
I guess Lift trying to locate a 10.html file which means the URL rewrite doesn't work.







Jeppe Nejsum Madsen

unread,
Feb 21, 2011, 3:33:25 AM2/21/11
to lif...@googlegroups.com, Shark.Z
"Shark.Z" <evo...@gmail.com> writes:

> Let me clarify it this way (Bad English Speaker :) ),
> no context path is required any longer. I need to rewrite the url:

> *http://localhost:8080/Pages/edit/(postid)*using the following code:


>
> LiftRules.statelessRewrite.
> prepend(NamedPF("URL Rewrite") {
> case RewriteRequest(
> ParsePath("Pages" :: "edit" :: postid :: Nil, _, _,_), GetRequest,
> _) =>
> RewriteResponse(
> "Pages" :: "edit" :: postid :: Nil, Map("postid" -> postid)
> )
> })
>

> When the URL *http://localhost:8080/Pages/edit/10 is *visited, I wanna get

> the postid in my snippet.
> but now I got a NOT_FOUND error.
> I guess Lift trying to locate a 10.html file which means the URL rewrite
> doesn't work.

Have a look here: http://simply.liftweb.net/index-3.2.html (Parameters)

Also, for a custom Loc you can look here:

https://github.com/lift/examples/blob/master/combo/example/src/main/scala/net/liftweb/example/lib/WikiStuff.scala

/Jeppe

Naftoli Gugenheim

unread,
Feb 21, 2011, 2:40:03 PM2/21/11
to liftweb, Shark.Z

That's one possibility. Another is that somehow it's redirecting to an invalid page. To eliminate one possibility, add a println to the rewrite:

LiftRules.statelessRewrite.prepend(NamedPF("URL Rewrite") {

      case RewriteRequest(
          ParsePath("Pages" :: "edit" :: postid :: Nil, _, _,_), GetRequest, _) =>
        println("I am here")

        RewriteResponse(
          "Pages" :: "edit" :: postid :: Nil, Map("postid" -> postid) 
      )
    })

If it never gets printed then your problem is with the pattern match; otherwise, it's with the RewriteResponse.

Why are you using statelessRewrite?

Mike

unread,
Feb 21, 2011, 6:45:39 PM2/21/11
to Lift
The problem is your RewriteResponse should be Pages :: edit :: Nil.
Otherwise, you're just using the same URL, but adding ?postid=postid
to the end of it.

HTH

Shark.Z

unread,
Feb 22, 2011, 3:41:13 AM2/22/11
to lif...@googlegroups.com, Shark.Z
Why are you using statelessRewrite?

I am following the Lift WIKI 's example, perhaps not suitable for my case.

I am wondering if there're at least two ways to achieve this.
one is use Menu.param and a customized Loc,  like David showed in his Lift Basic Video.
The other  is as Tim mentioned, use a URL Rewrite.
Am I right? What's the difference between them?
 

Diego Medina

unread,
Feb 23, 2011, 11:36:50 PM2/23/11
to lif...@googlegroups.com
So far I have only used Menu.Param and it works for what I needed. If
you'd like, I can help you using it to fit your app, just create a
sample project following this steps:

http://www.assembla.com/wiki/show/liftweb/Posting_example_code

and I'll work something out.

Regards

Diego

> --
> You received this message because you are subscribed to the Google Groups
> "Lift" group.
> To post to this group, send email to lif...@googlegroups.com.
> To unsubscribe from this group, send email to
> liftweb+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/liftweb?hl=en.
>

--
Diego Medina
Web Developer
http://www.fmpwizard.com

David Pollak

unread,
Feb 27, 2011, 8:05:35 PM2/27/11
to lif...@googlegroups.com
On Sun, Feb 20, 2011 at 2:03 AM, Timothy Perrett <tim...@getintheloop.eu> wrote:
You need to use rewriting for this. Lift has good support for this type of thing, pretty sure there are examples on the wiki. Additionally, there are decent examples in Lift in Action.

No, you don't need to use rewriting for this.  Menu.param and Menu.params mean that there's no need for explicit rewriting.
 

On my mobile at the moment so can't paste a code example.

Hope that helps, Tim
--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.




--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Blog: http://goodstuff.im
Surf the harmonics

David Pollak

unread,
Feb 27, 2011, 8:10:27 PM2/27/11
to lif...@googlegroups.com
On Mon, Feb 21, 2011 at 12:33 AM, Jeppe Nejsum Madsen <je...@ingolfs.dk> wrote:
"Shark.Z" <evo...@gmail.com> writes:

> Let me clarify it this way (Bad English Speaker :) ),
> no context path is required any longer. I need to rewrite the url:
> *http://localhost:8080/Pages/edit/(postid)*using the following code:
>
> LiftRules.statelessRewrite.
> prepend(NamedPF("URL Rewrite") {
>       case RewriteRequest(
>           ParsePath("Pages" :: "edit" :: postid :: Nil, _, _,_), GetRequest,
> _) =>
>         RewriteResponse(
>           "Pages" :: "edit" :: postid :: Nil, Map("postid" -> postid)
>       )
>     })
>
> When the URL *http://localhost:8080/Pages/edit/10 is *visited, I wanna get
> the postid in my snippet.
> but now I got a NOT_FOUND error.
> I guess Lift trying to locate a 10.html file which means the URL rewrite
> doesn't work.

Have a look here: http://simply.liftweb.net/index-3.2.html (Parameters)

This is the right answer:

Assuming you've got a mapper singleton called Table:

Menu.param[Table]("Table", "Table", s => Table.find(s), _.id.toString) / "table"

And voila:

/table/1 will give you the page for the Table element with id 1.

If you've got snippets like:

class MySnippet(t: Table) {
  def render = "#name" #> t.name
}

Then you can do:

<div class="lift"MySnippet"><span id="name">Name goes here</span></div>

There are very few cases where you need rewrites, and this simple case is not one of them.


 
--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.

Reply all
Reply to author
Forward
0 new messages