JSS Commands


JSS files are BeanShell scripts and normal Beanshell commands apply. In addition, there are a number of JSS-specific commands that can be used for your convenience.
redirect_to(String action)
Redirects the browser to the given action, for the same controller. Use this to redirect after a committing a transaction.

redirect_to(String controller, String action)
Redirects the browser to another controller and action. Use this to redirect after a committing a transaction.

render_view(String layout, String view, Object data)
Forward the request to the view, given the layout and the data. Use this if you still want the request to be used by the receiving view.

render_view(String view, Object data)
Forward the request to the view, with the given data. Use this if you still want the request to be used by the receiving view.

render_view(String view)
Forward the request to the view. Use this if you still want the request to be used by the receiving view.

get_parameter(String parameter)
Get the parameter of the given name from the request object

delete(Object object)
Remove the persistence of this object from the database

update(Object object)
Update the persistence of the object

update(Object object, Map params)
Populate the object with the given parameter map and update the persistence in the database. If the object doesn’t exist, create it

find(Class clazz, Serializable id)
Finds and returns a single object of Class clazz given the id

query(String query)
Creates and returns a Hibernate Query object

sql(String sql)
Creates and returns a Hibernate SQLQuery object

sql(String sql, Class clazz)
Executes the SQL query provided for the provided class, and returns a list of objects

id()
Retrieves the id from the request parameters

With these changes in the JSS commands, a simple controller file (from the tutorial) becomes something like this:

import com.saush.jss.model.Recipe;

layout = “main”;

index() {
redirect_to(“list”);
}

list() {
query(“from Recipe”).list();
}

edit() {
layout = null;
find(Recipe.class, id());
}

update() {
Recipe recipe = jss.find(Recipe.class, id());
update(recipe, params);
redirect_to(“list”);
}

add() {
return new Recipe();
}

create() {
update(new Recipe(), params);
redirect_to(“list”);
}

delete() {
Recipe recipe = jss.find(Recipe.class, id());
delete(recipe);
redirect_to(“list”);
}

Notice that this file has become much simpler than before. Noticeably, I dropped the usage of the ‘jss’ default object, as well as created a command id() that extracts the id from the request parameters automatically. This controller is much more readable than the previous one. One comment I had from someone is that such code becomes ‘un-Java like’ and it becomes another chore to understand another programming language, which is counter to JSS’s simplicity principle. Not really. The code is still Java, and if you choose to do so, you can still program the whole thing in 100% Java — but you have ‘helper’ commands that simplify the command tasks and makes the code much more readable and maintenable.

Note that JSS’s simplicity principle is largely meant for the maintenance of the software, and not for building it. Anyone can write complex code. The rationale for writing simple code is not to build faster applications — piling enough competent  programmers in a project can usually do the job (to a certain extent). However complex code is hugely difficult to maintain and that is usually the trick in large-scale software development. It’s not in the building but in the maintaining. There’s a saying in Chinese — ‘Building an empire is easy but maintaining it is difficult.’ Heed the saying.

Advertisements

2 thoughts on “JSS Commands

  1. Thanks , I have just been snhcraieg for information approximately this subject for a while and yours is the best I have came upon till now. But, what about the conclusion? Are you sure in regards to the supply?|What i don’t realize is if truth be told how you’re no longer really a lot more well-liked than you may be right now. You are very intelligent.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s