Saturday, October 19, 2013

Basic web application with SBT for Netbeans from scratch

If you haven't read my previous post SBT application for Netbeans from scratch, you will find it helpful to read it.
This post will illustrate how to create a webapplication from scratch. We will assume some knowledge of setting up a basic scala application with sbt. We won't be creating any scala code. Instead we will try to render a basic index.html page.
  • Create a project directory and name it what you want. cd into the project directory.
  • Create the following directories
mkdir -p src/main/resources src/main/scala src/main/webapp src/main/webapp/WEB-INF src/main/webapp/META-INF src/test/scala src/test/resources project

Application build, dependencies, plugins setup

  • Create build.sbt inside the project root directory and copy the text below into the file.
 name := "scalatra-sbt-1"

 version := "1.0"

 scalaVersion := "2.10.2"

 sbtVersion := "0.12.4"

 seq(webSettings :_*)

 libraryDependencies ++= Seq(
    "ch.qos.logback" % "logback-classic" % "1.0.6" % "runtime",
    "org.mortbay.jetty" % "jetty" % "6.1.22" % "container"
  )
Make sure that each setting is separated by an empty line.
  • Create a new file inside the project directory and name it build.properties. Copy the text below into the build.properties file.
sbt.version=0.12.4
  • Create another file plugins.sbt inside the project directory. Copy the text blow into that file.
// you can also add multiple repositories at the same time
resolvers ++= Seq(
  "Scala Tools Releases" at "http://scala-tools.org/repo-releases/",
  "Java.net Maven2 Repository" at "http://download.java.net/maven/2/"
)

addSbtPlugin("com.earldouglas" % "xsbt-web-plugin" % "0.4.2")

addSbtPlugin("com.mojolly.scalate" % "xsbt-scalate-generator" % "0.4.2")

addSbtPlugin("org.scalatra.sbt" % "scalatra-sbt" % "0.3.2")

addSbtPlugin( "org.netbeans.nbsbt" % "nbsbt-plugin" % "1.0.2" )
Once again, make sure each sbt task above is followed by an empty line, other wise sbt will complain.

web application setup

  • Create a new file src/main/webapp/index.html and copy the following text into it.
<html>
   <head>
      <title> First Scala WebApp</title>
   </head>
   <body>
      <h1>Hello, world from Scala webapp</h1>
   </body>
</html>
  • Create a new file src/main/webapp/WEB-INF/web.xml file and add the following lines into it.
  <xml version="1.0" encoding="UTF-8">

 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

 <web-app>

 </web-app>

At this point you should be good to run the application. Use the following commands.
 > sbt clean compile
 >
 > sbt container:start
Now go to app by clicking on the link.
Bingo ! you got your first scala webapp running.

No comments:

Post a Comment