You can use LiftRules.statelessRewrite which is processed very early
before the SiteMap comes into the picture. Hence you can rewrite at
this level. LiftRules.statefulRewrite is processed a little later on
(but you have full S context) but still before SiteMap is processed.

If this doesn't fit your needs, using your approach is not bad either.
Not sure where the boilerplate is as RESTfulLoc  looks like a generic
one.

Br's,
Marius

On Jan 10, 11:39 am, Brian Hsu <brianhsu....@gmail.com> wrote:
> Thanks for your reply,
>
> But I would like them combine with Menu tightly, so I could setup them
> only in my Model, just like CRUDify trait.
>
> In the end, I created a Loc subclass myself, and use it to handle all
> the rewrite stuff, it seems works pretty
> well and make thing much simpler (at least for me), so I post the code
> here.
>
> Feel free to use it if anybody need this.
>
> /**
>  *  A RESTful-like URL handling Loc
>  *
>  *  If you have the following templates:
>  *
>  *    * webapps/item/edit.html
>  *    * webapps/item/view.html
>  *
>  *  You want the following URL map to corresponding template with
>  *  last path component as a S parameter.
>  *
>  *    http://localhost/item/edit/1 to  http://localhost/item/edit
>  *    http://localhost/item/view/1 to  http://localhost/item/view
>  *
>  *  You could create a Menu with this Loc class in your Model object.
>  *
>  *  <code>
>  *  object Item extends Item with LongKeyedMetaMapper[Item]
>  *  {
>  *      // Other methods here...
>  *
>  *      def menu () {
>  *
>  *          // What methods do we have?
>  *          val methods = List ("view", "edit")
>  *
>  *          val parameterName = "itemID"
>  *          val itemLoc = new RESTfulLoc("Item", List("item"),
> "Item",
>  *                                       methods, parameterName)
>  *
>  *          Menu (itemLoc)
>  *      }
>  *  }
>  *  </code>
>  *
>  *  Now add the menu to SiteMap in Boot.boot
>  *
>  *  <code>
>  *  class Boot {
>  *      def boot () {
>  *
>  *          val entries = Item.menu ::  Nil
>  *
>  *          LiftRules.setSiteMap(SiteMap(entries:_*))
>  *      }
>  *  }
>  *  </code>
>  *
>  *
>  *  Finally, You could access the parameter in your snippet with
>  *  S.param("itemID")
>  *
>  */
> class RESTfulLoc (val name: String, val path: List[String],
>                   val text: LinkText[Unit], val methods: List[String],
>                   val parameterName: String,
>                   val locParams: LocParam[Unit]*) extends Loc[Unit]
> {
>     override val defaultValue = Full(())
>     override val params = locParams.toList
>     override val link: Link[Unit] = (List(path.first), true)
>
>     def this (name: String, path: List[String], text: LinkText[Unit],
>               methods: List[String], locParams: LocParam[Unit]*) =
>     {
>         this (name, path, text, methods, "id", locParams:_*)
>     }
>
>     private def isTarget (path: ParsePath) =
>     {
>         path.partPath -- this.path match {
>             case List (action, id) => {
>                 (methods contains action) && id != "index"
>             }
>             case _ => false
>         }
>     }
>
>     override def rewrite = Full (NamedPF("RESTfulLoc")
>     {
>         case RewriteRequest (path, _, _) if isTarget(path) => {
>              val parameter = path.partPath.last
>              val action    = path.partPath.init
>              val data      = Map (parameterName -> parameter)
>
>              RewriteResponse(action, data) -> ()
>         }
>     })
>
> }
>
> On 1月10日, 下午4時55分, Marius <marius.dan...@gmail.com> wrote:
>
> > Or you could use something like if you'r building REST API's:
>
> > LiftRules.dispatch.append {
>
> >    case Req("test" :: _, suffix, reqType) => // return a () =>
> > LiftResponse
>
> > }
>
> > or for non REST API you could use
>
> > LiftRules.statefulRewrite.append {
>
> > case RewriteRequest(ParsePath("test" :: _, _, _, _), reqType, request)
> > => ...
>
> > }
>
> > Br's,
> > Marius
>
> > On Jan 10, 7:32 am, Brian Hsu <brianhsu....@gmail.com> wrote:
-- 
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.


Reply via email to