Cheat Sheet
From LiftWiki
Starting a new lift project
To start a new lift project install Java 1.5 or 1.6, and Maven 2.0.8 (newer version should work as well).
Now let's create your new project
cd ~/tmp mvn archetype:create -U \ -DarchetypeGroupId=net.liftweb \ -DarchetypeArtifactId=lift-archetype-basic \ -DarchetypeVersion=1.0.2 \ -DremoteRepositories=http://scala-tools.org/repo-releases \ -DgroupId=com.liftone -DartifactId=liftone cd liftone mvn jetty:run -U
In this example, LiftOne is the name of the project, ~/tmp/liftone/ is the location of the project, and com.liftone is the package name for the project classes. Remember that Scala package names are similar to Java package names.
Point your browser to http://localhost:8080 and see your first lift project.
Troubleshooting
- Problem: Out of Memory Errors Reported by maven:
- Possible Solution: On 64bit machines you need more than a gig of RAM. If you have a 32bit machine, and a gig of RAM, a possible work around is to change the maxmemory parameter in the liftweb/lift/pom.xml to 960m. This has been successfully Tested On:
- Windows XP 32bit, 1 Gig of RAM, JDK/JRE 1.6, Maven 2.0.8
See also
- Lift without maven: Once you have created a new project with maven, you can use any other build tool.
Boot (setting the lift environment)
When the lift servlet is initialized, it looks for a class bootstrap.liftweb.Boot and invokes the "boot" method on an instance of this class. This allows the project to define special request routing rules, tell lift where certain classes can be found. See the Boot Example for more information.
Project Layout
lift uses Maven as its build system and lift projects are laid out accordingly. In the root of all projects is a src directory and a pom.xml file. The POM file contains the Maven build instructions. The src directory contains main which contains scala and webapp.
The src/main/scala directory contains the Scala source code.
The src/main/webapp directory contains the HTML, JavaScript, CSS, and WEB-INF code.
The lift_example directory contains the derby database.
Run Mode
One can run a lift application in different modes based on starting the JVM with the -Drun.mode=production. The run modes are:
object RunModes extends Enumeration {
val Development = Value(1, "Development")
val Test = Value(2, "Test")
val Staging = Value(3, "Staging")
val Production = Value(4, "Production")
val Pilot = Value(5, "Pilot")
val Profile = Value(6, "Profile")
}
Property files
Different developers can have different properties files within the same project. The properties files are located in
src/main/webapp/WEB-INF/classes/props Properties files are retrieved based on RunMode, user name, and host name. So, if you're running in development mode, your username is 'dpp' and your hostname is 'goat', the
dpp.goat.props file will be used. If that cannot be found, lift will check for
dpp.props, goat.props, and finally default.props.
If the RunMode is anything other than "development", the RunMode name will be prepended to the name so test.dpp.goat.props, test.dpp.props,
test.goat.props, and test.default.props will be checked for in that order.
The properties files are in standard Java properties format.
Where HTML goes
Put the HTML, CSS, JavaScript, images, etc. in the src/main/webapp directory or
subdirectories. For file names that end in .xml, .html, .xhtml, .htm, .liftjs, and .liftcss, lift will parse and
serve the page. For all other files, they will be served up as static content by the default servlet.
If a request comes in for /hello lift will look for src/main/webapp/hello.html
If you want to localize your content, /hello will look for hello_en_US.html, then
hello_en.html, then hello.html where en and US are the language and country for the current locale.
See lift View First rendering.
Defining Models
lift O-R mapped models are defined based on a class with fields.
class WikiEntry extends KeyedMapper[Long, WikiEntry] {
def getSingleton = WikiEntry // what's the "meta" object
def primaryKeyField = id
// the primary key
object id extends MappedLongIndex(this)
// the name of the entry
object name extends MappedString(this, 32) {
override def dbIndexed_? = true // indexed in the DB
}
object owner extends MappedLongForeignKey(this, User)
// the text of the entry
object entry extends MappedTextarea(this, 8192) {
override def textareaRows = 10
override def textareaCols = 50
}
}
Discussion on having shared base traits for Models
Accessing Model Fields
Model fields are set with an apply operator:
val e: WikiEntry = WikiEntry.create
e.name("Hello") // set the name
// chained setting
e.name("hello").entry("Dogs and Cats")
// and models can be saved
e.name("hello").entry("Dogs and Cats").save
// accessing fields:
e.name == "hello" // true
e.name.asHtml // get the HTML formatted value of the field
e.owner.obj.asHtml // accessing many-to-one relationship objects
Schemifier
The Schemifier makes sure that the database schema supports the declared model. If a database column or table is missing the Schemifier will create it according to the model. If you want lift to create tables from your model entities remember to register the model entities in the Schemifier (this i normally done in Boot.scala):
Schemifier.schemify(true, Log.infoF _, User) Schemifier.schemify(true, Log.infoF _, Auction) Schemifier.schemify(true, Log.infoF _, Bid)
Web 1.0 Forms
AJAX
If you want to submit a whole form, it is very easy with HowTo_use_JSON_forms.
In your snippet have the following:
class Submission {
def form = {
val url = ""
val title = ""
<span> { SHtml.text("URL", u => url = u) }<br />
{ SHtml.text("Title", t => title = t) }<br />
{ SHtml.submit("Submit", ignore => LinkStore ! AddLink(url, title)) }</span>
}
}
And in your template, have the following:
<lift:snippet type="Submission:Form" form="POST" />
Making ajax-ified anchor tags tags are easy, too.
SHtml.a(() => {doSomething; Noop}, Text("Click me to do something!"))
Clicking on the anchor tag will execute the function {doSomething; Noop}
If you want to add elements to your anchor tags (or to any Elements) you can use % like so:
SHtml.a(() => Noop), Text("I don't do much but click me anyway")) % ("class" -> "does-nothing-really")
Comet
See CometActor
Lift Tags
See LiftTags
Running with Tomcat and JavaRebel
See JavaRebel
Cookies
See Cookies
Connection String Quickies
Define in <lift-app>/src/main/scala/bootstrap/liftweb/Boot.scala
-- 8< -- 8< -- 8< -- 8< --
// Derby - http://db.apache.org/derby/
Class.forName("org.apache.derby.jdbc.EmbeddedDriver")
val dm = DriverManager.getConnection("jdbc:derby:DATABASE;create=true")
-- 8< -- 8< -- 8< -- 8< --
-- 8< -- 8< -- 8< -- 8< --
// H2 - http://www.h2database.com/
Class.forName("org.h2.Driver")
val dm = DriverManager.getConnection("jdbc:h2:mem:DATABASE;DB_CLOSE_DELAY=-1")
-- 8< -- 8< -- 8< -- 8< --
-- 8< -- 8< -- 8< -- 8< --
// MySQL - http://www.mysql.org
Class.forName("com.mysql.jdbc.Driver")
val dm = DriverManager.getConnection("jdbc:mysql://localhost:POST_NUMBER/DATABASE", "USER", "PASSWORD")
-- 8< -- 8< -- 8< -- 8< --
-- 8< -- 8< -- 8< -- 8< --
// PostgreSQL - http://www.postgresql.org
Class.forName("org.postgresql.Driver")
val dm = DriverManager.getConnection("jdbc:postgresql://localhost/DATABASE?user=USER&password=PASSWORD")
-- 8< -- 8< -- 8< -- 8< --
Sometimes it can be useful to be able to access the database into which lift is writing with an external application via JDBC. Since you don't want to start up another JVM to have a network server running, if you're using derby you can do the following. Add a file to the toplevel folder of your project and name it derby.properties with the content:
derby.drda.startNetworkServer=true
Now you can point a generic JDBC client (or the built in ij-console) to the follwing URL: jdbc:derby://localhost:1527/<database-name>.
Always redirect to login if the User is not logged in
In Boot.scala add this RequestMatcher and if the user hits any page and is not logged in, they will be sent to /user_mgt/login
val dispatcher: LiftServlet.DispatchPf = {
case RequestMatcher(_, ParsePath(path, _, _),_,_) if !User.loggedIn_? && path.head != "user_mgt" =>
ignore => Full(RedirectResponse("/user_mgt/login", state))
}

