Announcement Announcement Module
Collapse
No announcement yet.
Question on SimpleFormController Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Question on SimpleFormController

    Hi,

    I have a question on SimpleFormController. The basic senario for my question is as below:
    There are two pages, when is used to register a user(create_user.html), and the other is to show success message to the user when they register a account successfully(create_user_successfully.html).
    On the create_user.html, assume we have only username & password fields. And I register a validator to validate whether the username & password fields are less than 6 characters. But I do not want to verify the username is unique in the validator because I think it is a complex logic. And I want to handle it in the controller class (extends SimpleFormController).
    I try to handle it in onSubmitAction method, but I cannot find a way to raise a error and return back to the create_user page(form). Then I try to overwrite onSubmit method. The code block is as below:
    public ModelAndView onSubmit (Object command) throws Exception {
    AccountCommand accountCommand = (AccountCommand) command;

    Customer customer = new Customer();
    //..... Set customer's status

    if (accountService.isUsernameUnique(customer.getUsern ame())) {
    accountService.registerCustomer(customer);
    } else {
    RedirectView rv = new RedirectView("create_account.html");
    return new ModelAndView(rv);
    }

    return new ModelAndView(this.getSuccessView());
    }
    Then when the program go back to the formview(create_account.html), all the information are gone. I just have a quick view at the source code. And I see that when I set the sessionForm to true, spring framework will delete the form object from the session after get the command object. So I don't know how to maintain the information. Could you tell me a way to get me out?

    Thanks.

  • #2
    anyway, use Validator

    You can inject some service class to your validator and call service method validateUsername(). Then your validation could be reused, and it will be decoupled from your validator

    Comment


    • #3
      Yes. It works sometimes. But sometimes, the solution is not excellent.

      Assume the following scenario:
      1. spring call validator to validate the username's uniqueness. And it passes.
      2. unfortunately, before doSubmitAction() is called, another person has registered that email.
      3. I have to validate the uniqueness again in the doSubmitAction() or doSubmit(). And I would like to turn back to the formView with the fields filled with the original values(because the username is not unique now). But how can I do that?

      Although it has few possibility to have this situation, I want to know how to handle it in the right way. Thanks.

      Comment


      • #4
        In this case, i would treat this situation as exceptional and throw some business exception while creating new user. Exception will be caught by my exceptions interceptor and error message will be shown to the user.
        Or, you can override onSubmit(command, errors) (this method locates in SimpleFormController). You'll need to reuse functionality of these method from Spring SimpleFormController.java, but add you business method call and redirect to formView again if some errors occur. But note that somwhere in the depth of the Spring, the form bean is removed from the session, so you'll need to manage the bean in the session yourself. It's not an easy task ....

        Comment


        • #5
          sample apps

          Take a look into the sample apps that comes with the Spring distribution. There are atleast couple of nice solutions on how to handle the validation and how to raise an error and reopen some forms to user.

          Don't remember which application holds this code, but there are not that many of them so I think you find them

          Comment

          Working...
          X