Skip to content
kmkim edited this page Jan 13, 2014 · 1 revision

User Guide

Here is about Milonga's basic usage.

Atmos

Atmos is the object defined as global variable and the start pointe of Milonga usage.

Making MVC handler

Atmos object contains the function of defining Spring MVC handler. The function is handler(url, handler). It has parameters of url being requested and Javascript method which is executed when url is requested. We call Javascript method handler method. The value returned by handler method is passed on Http response body. In other words, this mechanism is similar to @Responsebody method's return value in Spring MVC.

Atmos.handler('/hello', function(req, res) {
	return 'Hello Milonga!';
});

Handler Method

url, the first parameter of hadnler method is the string value which means URL requested. handler, the second parameter is the function of being executed when URL is requested. It is possible to declare parameters of that function as Http request and response objects. The names of parameters are req and res. req means Http request object and res does Http response.

req

req is the abstraction to HTTP request. req stores request parameter values.

Atmos.handler('/get_attribute', function(req, res) {
	var foo = req.foo;
});

req can return request parameter data as specific Java class type by using bindAs method. The parameter of bindAs is full name of Java class.

Atmos.handler('/binding_object', function(req, res) {
	var foo = request.bindAs('com.sample.Foo');
	return foo;
});

URI template variable can be declared and be used as a variable in handler method. This is similar to PathVariable annotation in Spring MVC handler.

Atmos.handler('/foo/{bar1}/{bar2}', function(req, res) {
	return bar1 + bar2;
});

HTTP session can be handled by req.session.

Atmos.handler('/login', function(req, res) {
	var userId = req.userId;
	req.session.userId = userId;
});

res

res is the abstraction to HTTP response. Its function contains HTTP redrect and forward functions.

Atmos.handler('/search', function(req, res) {
	res.redirect('http://www.google.com');
}.toView();

or

Atmos.handler('/search', function(req, res) {
	
}.redirect('http://www.google.com');

Http Cookie can be set by res.cookie.

Atmos.handler('/login', function(req, res) {
	var userId = req.userId;
	res.cookie.mail = userId;
};

Setting View Page

Return value of Milonga handler can be not only response body but also view page. Handler's method, namely toView(viewName) method can make user define view page after request processing. String of view page name should be input to viewName parameter of toView method. In the case of leaving out view page name, default page will be called depending on hanlder's url. Called view page depends on the configuration of Spring MVC's ViewResolver.

Atmos.handler('/contents', function() {
	// blog.jsp
}).toView('blog');


Atmos.handler('/contents', function() {
	// contents.jsp	
}).toView();

Return Type Of Handler

Milonga can return the response data depending on extension of requested URL. Provided return types of Milonga handler are JSON, XML and plain text.

Returning JSON

This handler example is for returning JSON data.

Atmos.handler('/user/{id}', function(req) {
	var result = new Object();
	result.id = id;
	return result;
});

Calling this handler by curl.

$ curl http://127.0.0.1:8080/{context-root}/user/foo.json
{
	"id" : "foo"
}

Returning XML

You need to firstly define Java bean style class in order to return XML type data.

package com.skp.milonga.externals.blog.model;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonProperty;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "blog")
public class Blog {

	@JsonProperty("email")
	private String id;

	@JsonIgnore
	private String title;

	//@XmlAttribute
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	//@XmlAttribute
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

}

Then the handler's return value should be Java object to return XML data.

Atmos.handler('/blog/{id}', function(req) {
	var blog = new com.skp.milonga.externals.blog.model.Blog();
	blog.setId(id);
	blog.setTitle("This is " + id + "'s blog.");
	return blog;
});

Calling this handler by url.

$ curl http://127.0.0.1:8080/{context-root}/blog/foo.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<blog>
	<id>kmkim</id>
	<title>This is kmkim's blog.</title>
</blog>