Category: Java

Spring 4 Web MVC with AngularJS

Spring 4 Web MVC with AngularJS

Introduction The project downloadable below is an answer to a question repeated in this blog (Spring 3 MVC – Adding Objects to a List element on the fly at form submit – (Generic method)) “How can I submit nested lists of objects?” The answer is AJAX and JSON. A very good […]

A brief note on the merits of using generic types over contextual types

A brief note on the merits of using generic types over contextual types

Introduction Generic types can reduce the class count in any project thus allowing easier maintenance, less mapping and more consistency. It promotes code reuse in a big way.

Hibernate – Populating an unmapped Entity field with COUNT(*) using @Formula

Hibernate – Populating an unmapped Entity field with COUNT(*) using @Formula

This post demonstrates how to populate an Entity Bean property that is not mapped to a DB table column. It uses the @Formula annotation to insert native SQL into the hibernate generated query. My

Hibernate @ManyToMany delete non-owner – prevent owner being deleted

Hibernate @ManyToMany delete non-owner – prevent owner being deleted

The solution to deleting a child in a @ManyToMany relationship and deleting the parents in the process.

Java Hibernate ManyToMany Tutorial – With Add and Delete examples

Java Hibernate ManyToMany Tutorial – With Add and Delete examples

The many-to-many mappings and usage thereof can be a little bit weird on first glance. This tutorial demonstrates: The mappings (Annotations not XML). Adding an entity to a group. Removing an entity from a group. Listing parents with children. Listing children with parents. (The Hibernate Logic is completely separate from […]

Spring 3 MVC – Adding Objects to a List element on the fly at form submit – (Generic method)

Spring 3 MVC – Adding Objects to a List element on the fly at form submit – (Generic method)

Edit 10-Feb-2015 A much better approach to this problem is simply to use JSON. A number of comments below are in regard to nested lists. The solution detailed here does not readily facilitate nested lists. The solution outlined in this blog does: http://outbottle.com/spring-4-web-mvc-json-handling/. JSON is the recommended way of achieving “Adding […]

Spring 3 Security – Custom Login Form with Remember Me

Spring 3 Security – Custom Login Form with Remember Me

This tutorial will demonstrate the following: Login with a custom login form (rather than the spring security default) Implement a remember me function Distinguish between being logged in via ‘Remember-Me’ versus Actually logged in

Permit CSS, JS and other resources in a Spring application

Permit CSS, JS and other resources in a Spring application

In Spring 3 Web MVC applications every request is sent through an @Controller method. This is undesirable for CSS, JS and image files. Overcome the problem as so.

Hello Spring 3 Security with Netbeans – Role Based Method Protection

Hello Spring 3 Security with Netbeans – Role Based Method Protection

This tutorial will outline in simple steps how to do the following: Create Spring 3 Web MVC project using Netbeans Introduce Spring Security measures Limit @Controller method invocation to certain User-Role’s using Annotations Roles and users are defined within XML configurations. The full source code is available for download here

Useful Regex (Regular Expressions)

Useful Regex (Regular Expressions)

Description JavaScript Java or C# Integer No Spaces (anywhere) /^([\d]+)$|^$/g ^([\\d]+)$|^$ Empty String or Integer No Spaces (anywhere) /^([\d]+)$/g ^([\\d]+)$ Alfa or Numeric Characters Only /^[A-Za-z0-9]+$/ ^[A-Za-z0-9]+$ Alfa or Numeric Characters Plus listed Other Chars /^[A-Za-z0-9#!]+$/ ^[A-Za-z0-9#!]+$ Matches alpha or numeric or # or !  

Custom Annotated Validation with Spring 3 Web MVC

Custom Annotated Validation with Spring 3 Web MVC

My previous tutorial detailed using Hibernate Validation within Spring 3 Web MVC.  This tutorial shows how to create custom validation annotations. E.g.

This example annotation only permits the word ‘John’. Optionally, it can be case-sensitive. 

Spring 3 Web MVC – Exception Handling – incorporating AJAX

Spring 3 Web MVC – Exception Handling – incorporating AJAX

This tutorial demonstrates how to handle exceptions in Spring 3 Web MCV. Emphasis is given to Handling  exceptions that occur during AJAX requests. I.e. in the event of an exception during an AJAX request,  it is not desirable to redirect to an error page, instead JavaScript error handling code should be […]

Validation for Hibernate in Spring 3 Web MVC (with Netbeans)

Validation for Hibernate in Spring 3 Web MVC (with Netbeans)

This blog details how to apply Validation to Spring 3 Web MVC forms using Hibernate’s validation framework.

Integrating Hibernate with Spring 3 Web MVC

Integrating Hibernate with Spring 3 Web MVC

This tutorial on “Integrating Hibernate with Spring 3 Web MVC” incorporates: Hibernate Transaction Control @RequestMapping friendly URL’s. More on this specific Spring 3 Web MVC friendly URL topic here. (Alternative @RequestParam method here) A Generic DAO suitable for Hibernate operations with Spring 3 Web MVC. More here in this separate […]

Setting the Welcome Page in Spring Web MVC

Setting the Welcome Page in Spring Web MVC

In any Spring Web MVC application, there is a need to set the default landing or welcome page. Traditionally in Java web applications, this is done in the web.xml file as so:

Or in a spring web mvc application, it might be like this:

With a corresponding controller class RequestMapping: […]

A Generic DAO for Hibernate

A Generic DAO for Hibernate

This short blog shares one Generic DAO (Data Access Object) for Hibernate that may be useful in a Spring 3 project. The source code is available for download at the bottom of this page.

Determining the primary key value of an entity bean using reflection

Determining the primary key value of an entity bean using reflection

Given some Entity Object or generic type T, it may be useful to determine the value of the primary key without knowing the object type.

Determining the primary key property name of an entity bean using reflection

Determining the primary key property name of an entity bean using reflection

The name of the property that represents the primary key of an Entity Bean can be determined using reflection as follows: To get the actual value see this tutorial

Determining the primary key property name using Hibernate

Determining the primary key property name using Hibernate

Simple method:

String idPropertyName = sessionFactory.getClassMetadata(User.class).getIdentifierPropertyName(); To get the actual value see this tutorial Elaboration

Using @RequestParam to pass in HTTP GET parameters in Spring 3 Web MVC

Using @RequestParam to pass in HTTP GET parameters in Spring 3 Web MVC

Receiving HTTP GET parameters in Spring is made very easy using the @RequestParam annotation.