Apache Wicket: nesting dependencies in form validators (using Guice)

(This is basically the following on this question .)

I need to access the database service level in one of my form validators (to make sure that the email message is not yet received when registering a new user).

I tried the following (some input fields are omitted for brevity):

public class RegistrationPage extends WebPage { @Inject private UserService userService; public RegistrationPage() { add(new FeedbackPanel("feedback")); TextField<String> email = new TextField<String>("email", Model.of("")); ... email.add(new IValidator<String>() { @Override public void validate(IValidatable<String> validatable) { String email = validatable.getValue(); if (userService.findUserByEmail(email) != null) { // report error... } } }); Form<?> form = new Form<Void>("registrationForm") { ... }; form.add(email); add(form); } } 

Unfortunately, this can lead to

 java.lang.IllegalStateException: EntityManager is closed 

I suspect that the problem is that I am using open-session-in-view and that several form requests span multiple requests. userService is introduced for the first request and (illegally) reused in subsequent requests. (Multiple forms submission occurs if validation fails and the user tries to submit the form again.)

My question

What is the best way to solve this problem? In the same way as I solved the previous, similar problem ? In this case, it will undoubtedly become useless.

+1
source share
1 answer

There is a slightly different life cycle that you want, and this gate uses.

Think first, first understand that the Wicket component and injection:

  • Enter component
  • Call Injector - IComponentInstantionListener.onInstantiation (component component)
  • Annotated field entered
  • Use component (rendering, etc.)
  • The next query - use the same component with the entered fields

What to do? Use the proxy class that is entered in the field (s). When calling the proxy class, it uses the current bean.

See SprinComponentInejctor at http://ci.apache.org/projects/wicket/apidocs/6.0.x/org/apache/wicket/spring/injection/annot/SpringComponentInjector.html

see AnnotProxyFieldValueFactory at http://ci.apache.org/projects/wicket/apidocs/6.0.x/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactory.html

Even you need to write your own implementation or look at the Wicket Contrib or Wicket Stuff.

0
source

Source: https://habr.com/ru/post/1487651/


All Articles