开发者

Managing state with scalatra

开发者 https://www.devze.com 2023-04-08 17:26 出处:网络
I understand that Scalatra is a lightweight framework.However, I\'m wondering if there are any tricks for managing small amounts of state.I have a form with a textarea and a few checkboxes.For example

I understand that Scalatra is a lightweight framework. However, I'm wondering if there are any tricks for managing small amounts of state. I have a form with a textarea and a few checkboxes. For example, suppose the textarea contains a math equation and the checkboxes some additional options. When I submit (method=POST), I would like to display开发者_运维技巧 a result beneath the form, but maintain the same options (same text in the textarea, same checkboxes checked).

Is there a good way to accomplish this? Or do I need to manually set the values when generating the HTML? I'm presently generating HTML using Jade.


Do it server side with a resource returning JSON, and call it dynamically using JavaScript XHR.

No need of state! Go away Evil State!

EDIT

~A bit more explanation~

  1. You create a webservice that take computation parameter as input (using ?= in url with GET, or better using JSON payload with POST), then the service do the computation and return a JSON representation of the result

  2. You call that scalatra webservice using AJAX from your web application and you update the content of the webpage using the JSON result returned by the web server

If you don't already know much about AJAX, read tutorials and I think you will understand better what I mean. You can too search for example of Scalatra resource that return JSON representation (using lift-json i.e.)

As a side note, you can take a look at Bowler: http://www.bowlerframework.org which make REST development on scalatra easier.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号