开发者

How can I post a form and show the result on the same page with Play framework?

开发者 https://www.devze.com 2023-01-29 12:22 出处:网络
I\'ve been playing around with Play framework for a few days, and it seems really cool. However, I ran into some problems when I wanted to have a form on a page, post the form and show the results on

I've been playing around with Play framework for a few days, and it seems really cool. However, I ran into some problems when I wanted to have a form on a page, post the form and show the results on the same page (with the form still on the page). Does anybody know if this is possible with Play, and if so: how?

EDIT: I should have explained better. The form is a search-style form not a save-style form. I want to be able to search for something, have the results come up under the form and still have the values the user filled in in the form (as it is if you type in something that don't validate. I have开发者_开发问答 tried to set values on the params object directly in the search action, but it disappears when the search action calls the new action.


Second try of an answer untested, hope this fits to your problem.

public static void search(String criteria1, String criteria2) {
....
    params.flash();
}

search.gsp

    <p id="criteria1-field">
        <label for="criteria1">&{'criteria1'}</label>
        <input type="text" name="criteria1" id="criteria1" value="${flash.criteria1}" />
   </p>
   <p id="criteria2-field">
    <label for="criteria2">&{'criteria2'}</label>
    <input type="text" name="criteria2" id="criteria2" value="${flash.criteria2}" />
   </p>


Well this is quite simple. You put into the routes.conf a

GET /myPageWithForm   MyController.read     
POST /myPageWithForm  MyController.save

In read you read the data and render your page with the form. In save you save the data and redirect to read via read();

Hope this answers your question.

0

精彩评论

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