|
GettingStarted
RequiredYou will need the following tools to start developing Sweet web application:
Getting started with SweetIn this tutorial we are going to create a simple webapp, add couple changes and you should see the result. I assume you know how to navigate around your Terminal of your own OS. For WindowsXP, that will be cmd.exe, for MacOSX that will be the Terminal app, and for Linux guys, I need to say no more :) The commands used in the following tutorial should be the same for any OS, if you installed and set your PATH correctly according to the documents mentioned above. The only difference is that / means root of your files sysstem in Unix/Linus, but on Windows, you will have to change it to C:\. Setting up a projectStep 1: Create a project cd /tmp toolbox create-project my-sweet Step 2: Run the application cd /tmp/my-sweet mvn jetty:run Now open your browser to http://localhost:8080/my-sweet Say Hello to the Sweet WorldStep 1: Add a new action handler in MainController Use the text editor and edit the class in file /tmp/my-sweet/src/main/scala/mysweet/MainController.scala: class MainController extends HandlerNameViewController{
handle("/index"){ HandlerNameView() }
handle("/about"){ HandlerNameView() }
//Add the following mapping.
handle("/greeting"){
HandlerNameView("message"->"Hello! My name is Zemian Deng.")
}
}Step 2: Add a new view Use the text editor and create a file /tmp/my-sweet/src/main/webapp/view/main/greeting.ftl with following: <html><body>
<p>Sweet message: ${message}</p>
</body></html>Step 3: Compile your codes and restart server cd /tmp/my-sweet mvn jetty:run Now open your browser to http://localhost:8080/my-sweet/webapp/main/greeting Note: mvn jetty:run will recompile your source before starts up the server. If you have your previous server running on foreground, hit CTRL+C to kill it. Writing your own controllerStep 1: Generate a controller and a view cd /tmp/my-sweet toolbox create-controller system-info Step 2: Edit your controller to return system props data Use the text editor and edit file /tmp/my-sweet/src/main/scala/mysweet/SystemInfoController.scala with following: class SystemInfoController extends Controller {
handle("/index"){ HandlerNameView("sysprops"-> System.getProperties) }
}Step 3: Edit the view Use the text editor and edit file /tmp/my-sweet/src/main/webapp/view/system-info/index.ftl with following: [@main.layout title="system-info - index"]
<h1>Your System Properties</h1>
<table>
[#list sysprops?keys as name]
<tr><td>${name}</td><td>${sysprops[name]}</td></tr>
[/#list]
</table>
[/@main.layout]Step 4: Compile your codes cd /tmp/my-sweet mvn jetty:run Now open your browser to http://localhost:8080/my-sweet/webapp/system-info/index Note: I used the default layout [@main.layout title="system-info - index"], which was setup when you generate the project. You may change it under /tmp/my-sweet/src/main/webapp/view/layout/main.ftl. It's just a Freemarker macro and you can write any plain HTML to pretty up your page. If you want to confirm your progress, you should see something similar to this. Is this Sweeeeet or what? Now it's time to get more sweet with the Programmer Manual Guide! |
When going through "Setup project" at the top, I got an exception. I was required to install the scala maven plugin, mvn jetty:run failed with an exception stack trace.
Adding this to the /tmp/mysweet/pom.xml fixed my problem.
<project> <repositories> <repository> <id>scala-tools.org</id> <name>Scala-tools Maven2 Repository</name> <url>http://scala-tools.org/repo-releases</url> </repository> </repositories> ... <pluginRepositories> <pluginRepository> <id>scala-tools.org</id> <name>Scala-tools Maven2 Repository</name> <url>http://scala-tools.org/repo-releases</url> </pluginRepository> </pluginRepositories> ...Hi Gunnar,
Thanks for the feedback.
The default sweet project pom.xml should default to our sweetsoftwaredesign.com/maven repo, and that should have pull in scala-tools.org automatically. If you encounter this next time, you may retry with -U option on your maven command, or print out the stack trace will also help.
Great! But it seems that sweet has encoding problem. When I put some Chinese into the index.ftl, and set the encoding to utf-8, and navigate to http://localhost:8080/my-sweet/webapp/main/index, the content change to ???
I'm running 2.8 and get this:
C:\tmp>toolbox create-project my-sweet Parameter '-Dsweet.toolbox.home=C' is not recognised by Scalac. bad option: '-Dsweet.toolbox.home=C:\opt\sweet\bin\..' scalac <option> ... [<torun> <arguments>]
All options to scalac are allowed. See scalac -help.
<torun>, if present, is an object or script file to run. If no <torun> is present, run an interactive shell.
Option -howtorun allows explicitly specifying how to run <torun>:
Option -i requests that a file be pre-loaded. It is only meaningful for interactive shells.
Option -e requests that its argument be executed as Scala code.
Option -savecompiled requests that the compiled script be saved for future use.
Option -nocompdaemon requests that the fsc offline compiler not be used.
Option -Dproperty=value sets a Java system property.
Do I need to go back to 2.7.5?