Spring MVC Error Handling
This is not a tutorial of any sort. I’m adding this post to record the error-handling techniques of Spring MVC.
Catching
Errors can be caught in two places:
- Inside a validator (extended from Validator)
- Inside a controller
1. Validator
In the validate() overridden method, we can use two approaches:
a) Using the errors object:
errors.rejectValue(field, code, defaultMessage) → for field errors
errors.reject(code, defaultMessage) → for global/general errors
b) Using ValidationUtils.rejectXxx(…) methods
2. Controller
In the onSubmit(request, response, command, BindException errors) overridden method:
errors.rejectValue(field, code, defaultMessage) → for field errors
errors.reject(code, defaultMessage) → for global/general errors
…
return new ModelAndView(getFormView(), errors.getModel())
getFormView() returns us to the input form. The call to errors.getModel() is crucial here.
Reporting with JSP
<form:form command=”commandNameIfChangeFromDefault”>
<span class=”…”>
<spring:hasBindErrors name=”command”>
<c:forEach items=”${errors.allErrors} var=”error”> <!– or errors.globalErrors or errors.fieldErrors –>
<spring:message code=”${error.code}” text=”${error.defaultMessage}”/>
</c:forEach>
</spring:hasBindErrors>
</span>
</form:form>
Spring MVC Tutorial – Using Tiles 2
This post is also based on my previous work:
- Spring MVC Tutorial – Hibernate Integration – build a Spring MVC app using Hibernate from the ground up.
- Spring MVC Tutorial – Paging through Hibernate and Selection Handling – add table/list browsing support utilizing paging in Hibernate, add a page navigation bar, add checkbox handling against table rows.
Here I will not add any new functionality – just move the view technology to Tiles 2 that comes bundled with Spring complete distribution.
The new completed project is available as ibank-v3.zip.
Till now, I have repeated header/footer/menu code in all the JSP files. Tiles 2 can be used to remove that repetition. Let’s begin.
Spring MVC Tutorial – Hibernate Integration
After a long gap…
For introduction to Spring MVC in particular, see Spring MVC Tutorial and Spring MVC Tutorial 2.
The project done in STS can be downloaded from my Box.net account. The project libraries you will have to edit to match your setup.
Along with learning to integrate Hibernate, I’ll be introducing some new pieces of Spring MVC.
Read the rest of this entry »
SQL – Dynamic Date Range, Number Range in Oracle, SQL Server, and DB2/400
Often, we need to make coverage reports that span over a period of time and where all points in the period do not contain data.
Assume a shop has 50 outlets and we want to find the number of sales in each branch over the past ten days. Business is bad in some areas and so some outlets had no sale at all. A query like the following will give only those outlets that had sales but not a list of all outlets:
SELECT outletid, saledate, count(*) FROM sales GROUP BY outletid, saledate ORDER BY outletid, saledate
ODI – Incremental Update and Surrogate Key using Database Sequence
I am writing this because it had me lose my sanity for some time. If,
- You’re using Oracle Data Integrator
- You’ve defined a dimension table with a surrogate key even if it is not a SCD type 2 (recommended by Kimball)
- The surrogate key is maintained through a database sequence (Oracle sequence in my case)
- Your IKM in Incremental Update
- You’re frustrated about how to control updates to a dimension having a sequenced surrogate key
Then read on for the solution that worked for me. The solution is pretty standard – you only need to know what you’re doing and setting the right options.
ODI – How to Reverse Flat Files
The most important thing to remember before using flat files as datastores in ODI is that the Designer looks for them in the client PC where it runs. I haven’t tried using a UNC (\\computer\share\…) but that should work.
ODI comes pre-configured with a folder for dealing with flat files – in the Topology Manger, you’ll find the FILE_GENERIC data server set up to serve files from the ODI_HOME/oracledi/demo/file directory. But if you want your own file data server then follow these steps:
Active Directory Authentication using Java/JNDI
I’ve successfully performed AD authentication using JNDI from Java. It is almost a copy of the code found in Mauricio Rojas Blog. Thanks Mauricio!
The ADAuthenticator class tries to connect to the AD using the given credentials and retuns a Map containing some information of the user if authentication succeeds. It can be instantiated to authenticate to a default AD or a specific AD in the constructor.
iSeries, Oracle Reports 10g and ODBC-JDBC
I’ve been pulling my hair on this issue:
A complex sql with subqueries and a union written and executed OK in iSeriese Navigator for V5R4. Then I copied and pasted the same SQL in the query definition window of Oracle Reports Builder JDBC query dialog. The connection to iSeries is through the JDBC-ODBC bridge driver. I put
_xxx parameters in the query.
Guess what? I got an error ‘Variable P_xxx not defined or not usable’.
Earlier, I tested using a simple query with a parameter and it worked fine. After a lot of head-scratching and trial-and-error, finally I got the solution:
Place a blank/space (‘ ‘) around the parameter(s) even if it is at the end of a line (not query)!
The parser seems to stumble upon detecting tokens without spaces.