1

This isn't so much a programming problem with Spring but a request for advice.

I have a form on a JSP requiring a user to enter their ID number.

The form on the page calls a method in a controller. A validator is called from the controller.

If the user enters a non-numeric value, I believe I can check for this using:

typeMismatch.java.lang.Integer=You have input a non-numeric value into a field expecting a number...

But my question relates to the checking of the user's ID in the database, i.e. if the ID doesn't exist, then input should be rejected.

So, can services be used in validator classes? Are there any considerations against this?

2
  • User authorization code should be separate from data validation code. Commented Jun 13, 2014 at 14:02
  • Normally, I would agree. But this is a demo application so I am happy to bend the rules slightly. Commented Jun 13, 2014 at 14:17

2 Answers 2

2

In a demo application you can do what looks simpler. But you also asked i there are considerations against calling service methods form validation bean.

IMHO the main problem is that you will enter two times in the service layer once for validation and once for actual treatment. And this is where the transactions are generally managed. So you could end up opening 2 different transactions in the same request instead of only one. If your application or database can be heavily loaded, this should be considered.

Of course, if you have a transaction during all along the request such with the Open Session In View pattern, this is no longer a concern.

Sign up to request clarification or add additional context in comments.

1 Comment

The code isn't written yet, so I can't say at this point. but you've given some good advice.
1

It depends on whether you consider this validation rule as part of your business logic or not. I can see the following options:

  1. The validation rule is considered to be part of your business logic. Then throw an exception on the service layer if the user's ID already exists. In this way you will handle everything in a single transaction.
  2. The validation rule is not consider to be part of your business logic. The use a service method on your validation bean. This will provide you a common place for all validations.

1 Comment

This is only a demo application anyway. It is not business logic so much as validating input. But the method testing the existence of the user will be a service method (which will in turn call a DAO method). I plan to call it from the validator handling the input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.