官术网_书友最值得收藏!

Defining a SiteMap

Lift offers several ways for programmers to define which pages their applications will have. SiteMap is one of these ways. Besides defining the pages of your application, you can also control the access of each URL. This means that you can, for example, control whether a URL will have access restrictions or not and even control the restriction level of any given URL.

What does this mean? This means that you can think of SiteMap as the defined structure of your application containing every URL that will be accessible and the policies that are applied for each one of them.

Another thing that SiteMap does is automatic mapping between URLs and XHTML templates.

In this recipe, we will see how to configure SiteMap for a simple application. For this, let's suppose we want to create an application to store and manage contacts.

We will need one URL for each of the following actions:

  • List contacts
  • Create contacts
  • Edit contacts
  • Delete contacts
  • View contacts' details

Getting ready

Copy the contents of the lift_blank folder from the scala_29 folder to a new directory called contacts-app.

How to do it...

Carry out the following steps:

  1. Edit the Boot.scala file by adding a function, LocParam, just before the declaration of the entries value:
    val canManage_? = If(() => {
      true
    }, () => RedirectResponse("/"))

    This is to verify whether the user accessing the URL has management permission and is allowed to access it.

  2. Then, add another LocParam to check whether the user has administrative privileges:
    val isAdmin_? = If(() => {
      false
    }, () => RedirectWithState("/contacts/list",MessageState("Authorized personnel only" -> NoticeType.Warning)))
  3. After creating the functions, we'll define SiteMap by adding new menu items between the Menu.i("Home") / "index" and the Menu(Loc("Static", … entries.

    Menu items that need to be added are as follows:

    Menu.i("List Contacts") / "contacts" / "list",
    Menu.i("Create") / "contacts" / "create" >> canManage_?,
    Menu.i("Edit") / "contacts" / "edit" >> canManage_?,
    Menu.i("Delete") / "contacts" / "delete" >> isAdmin_?,
    Menu.i("View") / "contacts" / "view" >> canManage_?,
  4. Create a folder called contacts inside the webapp folder.
  5. Create five HTML files, one for each menu item defined in SiteMap:
    • list.html
    • create.html
    • edit.html
    • delete.html
    • view.html
  6. Then run the application and go to the URL http://localhost:8080 in your browser. The following screen will be displayed:

How it works...

Lift will build the SiteMap object during the application boot process as a result of the invocation of the method, LiftRules.setSiteMap.

When a user tries to access, for example /contacts/list, Lift will use SiteMap to decide whether the resource is defined or not by matching the URL against the link list defined by the menu entries in SiteMap. Since we've defined it, Lift will serve the page to the user. However, if the user tries to access /contacts/lists, the match will fail and Lift will return a 404 error.

We still need to understand what the LocParam functions are that were mentioned in steps 1 and 2. LocParam is something that modifies how Lift handles the given entry of SiteMap. In our case, it is a function that redirects the user to a different URL if the condition being tested fails. So, if someone tries to access /contacts/create, Lift will execute the canManage_? function after matching the URL. If the function returns true, Lift will serve the page to the user; otherwise, it will redirect the user to /. The same logic applies to the view and edit URLs.

The same logic applies to the delete link; the only difference being the fact that the isAdmin_? function not only redirects the user to /, but also passes a message that will be displayed in the page. This happens because we've used If LocParam that takes two arguments. The first is a test function. If it returns true, the page can be accessed; and if the return value is false, the result of evaluating the second parameter, FailMsg, will be sent to the browser.

There's more...

The entries variable is a list of menu items that will be processed when the application is started by Jetty.

Each menu item has a structure as shown in the following image.

  • Unique identifier
  • Link object
  • Text
  • Parameters

The unique identifier is used by Lift to keep a track of all the menu entries in SiteMap.

The Link contains the list that will be used to match the requested URL to check if it is defined or not.

The match head parameter, which is a Boolean value, defines whether Lift will match the exact URL or everything beginning with the given path. Notice that if you pass true, Lift will serve URLs containing /contacts/create or /contacts/create/*, where * is any HTML file existing inside webapps/contacts/create. If no such folder exists, Lift will return a 404 error. If you pass false, Lift will only serve /contacts/create and will return the 404 error for any other URL containing /contacts/create.

The URL that will be used to create the link on the page will be the value rendered in the href attribute of the anchor tag, <a href={url}>...</a>.

text is the text that will be shown when the link is displayed.

The parameters are functions, LocParam, that you want to execute when rendering the link or accessing the page.

Lift comes with several types of the LocParam functions, such as MenuCssClass to add a CSS class to the Menu and Title to define the title of the page.

See also

To learn more about SiteMap, Menu, and the LocParam functions, please go to https://www.assembla.com/wiki/show/liftweb/SiteMap.

主站蜘蛛池模板: 温州市| 韩城市| 谢通门县| 新巴尔虎左旗| 屏山县| 邻水| 武邑县| 太谷县| 吉木萨尔县| 高邑县| 安新县| 西吉县| 安义县| 绿春县| 江川县| 正安县| 大姚县| 鸡泽县| 通河县| 聂荣县| 玉林市| 汶上县| 新建县| 英超| 依兰县| 绥化市| 梁山县| 司法| 清新县| 定州市| 普陀区| 常山县| 八宿县| 勃利县| 股票| 龙南县| 马公市| 新绛县| 通辽市| 九江县| 江安县|