Re: [Lift] About the url rewrite (more thant one params and get 404 not found error)

2010-03-01 Thread David Pollak
On Sun, Feb 28, 2010 at 8:28 PM, Neil.Lv  wrote:

> Hi all,
>
>  I have a silly question about the url rewrite in the lift.
>  It has another two params in the url after the img.
>
>  http://localhost:8080/show/img/version/id
>
>  The url works fine.
>  => http://localhost:8080/show/img
>  => http://localhost:8080/show/img/v1/
>  => http://localhost:8080/show/img/v1/1
>
>  Get 404 error if the "/" string is not added at the end of the url
> (the id param isn't supplied)
>  => http://localhost:8080/show/img/v1
>
>  So, how can i rewrite the url and set the sitemap can make this link
> (  http://localhost:8080/show/img/v1  )
>  works fine as (  http://localhost:8080/show/img/v1/  ) when the id
> is not supplied.
>
>  Here is the code:
>
> ###
>  case RewriteRequest(
>  ParsePath(List("show", "img", version, id), _, _,_), _, _) =>
>RewriteResponse(List("show", "img"),
>  Map("version" -> version, "id" -> id)
>  )
> ###
>


This is just basic pattern matching stuff.  So,

case RewriteRequest(
 ParsePath("show" :: "img" :: version :: rest, _, _,_), _, _) =>
RewriteResponse(List("show", "img"),
 Map("version" -> version, "id" -> (rest.firstOption getOrElse "N/A"))
 )

Please pick up a copy of one of the Scala books and look at the pattern
matching section related to Lists.  There's a lot of flexibility that's no
Lift-specific, but that Lift leverages.

val entries = Menu(Loc("Home", List("index"), "Home")) ::
> Menu(Loc("show", List("show"), "show", Hidden)) :: User.sitemap
>
> ###
>
>  BTW, I use the LiftView to handle the request.
>
> ###
> class show extends LiftView {
>  def dispatch = {
>case "img" => imgDispatch _
>case _ => allDispatch _
>  }
>
>  def imgDispatch(): Box[NodeSeq] = {
>...
>  }
>
>  def allDispatch(): Box[NodeSeq] = {
>...
>  }
> }
> ###
>
>  Thanks very much!
>
> Cheers,
>  Neil
>
> --
> You received this message because you are subscribed to the Google Groups
> "Lift" group.
> To post to this group, send email to lift...@googlegroups.com.
> To unsubscribe from this group, send email to
> liftweb+unsubscr...@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
Surf the harmonics

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



Re: [Lift] About the url rewrite (more thant one params and get 404 not found error)

2010-02-28 Thread Jeppe Nejsum Madsen
"Neil.Lv"  writes:

> Hi all,
>
>   I have a silly question about the url rewrite in the lift.
>   It has another two params in the url after the img.
>
>   http://localhost:8080/show/img/version/id
>
>   The url works fine.
>   => http://localhost:8080/show/img
>   => http://localhost:8080/show/img/v1/
>   => http://localhost:8080/show/img/v1/1
>
>   Get 404 error if the "/" string is not added at the end of the url
> (the id param isn't supplied)
>   => http://localhost:8080/show/img/v1
>
>   So, how can i rewrite the url and set the sitemap can make this link
> (  http://localhost:8080/show/img/v1  )
>   works fine as (  http://localhost:8080/show/img/v1/  ) when the id
> is not supplied.
>
>   Here is the code:
>
> ###
>   case RewriteRequest(
>   ParsePath(List("show", "img", version, id), _, _,_), _, _) =>
> RewriteResponse(List("show", "img"),
>   Map("version" -> version, "id" -> id)
>   )
> ###


I'm not sure if this can be handled with the standard ParsePath, but it
is fairly easy to write your own extractor that can handle more complex
scenarios. Here's an example:

object ParamsExtractor {
  def unapply(pp:ParsePath): Option[(Account, OrgUnit)] = {
val result:Box[(Account, OrgUnit)] = if (pp.wholePath.startsWith(path) 
&& pp.wholePath.length == (path.length + 2)) {
  val res = Full((XX,YY))
  debug("Decoded URL: %s=%s".format(pp,res))
  res
  }
else
  None
result
  }
}
used like this:

case RewriteRequest(ParamsExtractor(account,orgUnit) , _, _) =>
(RewriteResponse(path), Full(account, orgUnit))

/Jeppe

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



[Lift] About the url rewrite (more thant one params and get 404 not found error)

2010-02-28 Thread Neil.Lv
Hi all,

  I have a silly question about the url rewrite in the lift.
  It has another two params in the url after the img.

  http://localhost:8080/show/img/version/id

  The url works fine.
  => http://localhost:8080/show/img
  => http://localhost:8080/show/img/v1/
  => http://localhost:8080/show/img/v1/1

  Get 404 error if the "/" string is not added at the end of the url
(the id param isn't supplied)
  => http://localhost:8080/show/img/v1

  So, how can i rewrite the url and set the sitemap can make this link
(  http://localhost:8080/show/img/v1  )
  works fine as (  http://localhost:8080/show/img/v1/  ) when the id
is not supplied.

  Here is the code:

###
  case RewriteRequest(
  ParsePath(List("show", "img", version, id), _, _,_), _, _) =>
RewriteResponse(List("show", "img"),
  Map("version" -> version, "id" -> id)
  )
###
val entries = Menu(Loc("Home", List("index"), "Home")) ::
Menu(Loc("show", List("show"), "show", Hidden)) :: User.sitemap

###

  BTW, I use the LiftView to handle the request.

###
class show extends LiftView {
  def dispatch = {
case "img" => imgDispatch _
case _ => allDispatch _
  }

  def imgDispatch(): Box[NodeSeq] = {
...
  }

  def allDispatch(): Box[NodeSeq] = {
...
  }
}
###

  Thanks very much!

Cheers,
  Neil

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