Notes by Fang
Displaying keyword search results 1 - 10
Created by Fang on March 30, 2012 10:07:25
Last update: March 08, 2013 13:41:57
After a user resets a password, I want to force the user to change the password before she gets access to secured content. This is usually done with a servlet filter. But with Spring MVC, you can also use a HandlerInterceptor . According to Spring JavaDoc: HandlerInterceptor is basically similar to a Servlet 2.3 Filter, but in contrast to the latter it just allows custom pre-processing with the option of prohibiting the execution of the handler itself, and custom post-processing. Filters are more powerful, for example they allow for exchanging the request and response objects that are handed down the chain. Note that a filter gets configured in web.xml, a HandlerInterceptor in the application context. As a basic guideline, fine-grained handler-related preprocessing tasks are candidates...
Created by Fang on March 30, 2012 15:04:04
Last update: March 30, 2012 15:04:04
Spring MVC 3.1 can send either JSON or HTML response on the same URL, depending on the type of response requested. With this mechanism, a page can be sent when directly requested from a link, but a JSON response can be sent in response to an AJAX request. This is the controller code:
package com.example; import java.util.Map; ... In the above example, JSON response will be sent when the HTTP request contains header "Accept: application/json". HTML response will be sent then the header is "Accept: */*", or "Accept: text/html", or anything else. You can add a limitation that the HTML response does not produce "application/json". But then the question is which response will be sent when the HTTP header is "Accept: */*"? Both methods will...
Created by Fang on March 30, 2012 12:15:49
Last update: March 30, 2012 12:15:49
1. mvc:default-servlet-handler Configures a handler for serving static resources by forwarding to the Servlet container's default Servlet. Use of this handler allows using a "/" mapping with the DispatcherServlet while still utilizing the Servlet container to serve static resources. HandlerMapping: org.springframework.web.servlet.handler.SimpleUrlHandlerMapping Handler: org.springframework.web.servlet.handler.SimpleUrlHandlerMapping Attribute Description default-servlet-name The name of the default Servlet to forward to for static resource requests. The handler will try to auto-detect the container's default Servlet at startup time using a list of known names. If the default Servlet cannot be detected because of using an unknown container or because it has been manually configured, the servlet name must be set explicitly. 2. mvc:view-controller Defines a simple Controller that selects a view to render the response. HandlerMapping: org.springframework.web.servlet.handler.SimpleUrlHandlerMapping Handler: org.springframework.web.servlet.mvc.ParameterizableViewController Attribute Description...
Created by Fang on March 30, 2012 10:23:21
Last update: March 30, 2012 10:23:21
These bean types are essential for the Spring MVC framework. I copied them here from the Spring Documentation for quick reference. Bean type Explanation HandlerMapping Maps incoming requests to handlers and a list of pre- and post-processors (handler interceptors) based on some criteria the details of which vary by HandlerMapping implementation. The most popular implementation supports annotated controllers but other implementations exists as well. HandlerAdapter Helps the DispatcherServlet to invoke a handler mapped to a request regardless of the handler is actually invoked. For example, invoking an annotated controller requires resolving various annotations. Thus the main purpose of a HandlerAdapter is to shield the DispatcherServlet from such details. HandlerExceptionResolver Maps exceptions to views also allowing for more complex exception handling code. ViewResolver Resolves logical String-based...
Created by Fang on March 06, 2012 12:25:33
Last update: March 06, 2012 12:25:33
In the bean validation API javadoc, for every constraint annotation, there's a corresponding .List annotation. For example, for @NotNull , there's @NotNull.List , for which JavaDoc says: Defines several @NotNull annotations on the same element What would you accomplish with multiple @NotNull annotations that you cannot accomplish with one @NotNull ? This is a test to reveal some of the facts. Change the Person class to:
package com.example; public class Person { ... Add another JUnit test ( src/test/com/example/TestPersonWithList.java ): package com.example; import java.util.Itera... As the test shows, a Person bean can never be valid because we are requiring that name must begin with Mr and Ms . One might think that the same can be accomplished by simply repeating the @Pattern annotation multiple times,...
Created by Fang on March 06, 2012 12:24:53
Last update: March 06, 2012 12:24:53
Validation groups can be used to control which rules validation rules to run. A validation group can be identified by any Java interface (not class!). Multiple validation groups may be specified when validating.
In this example, I added a validation group named MyValidationGroup ( src/main/java/com/example/MyValidationGroup.java in Maven project):
package com.example;
public interface MyVal...
and added a @Size rule for a person's name, because my database can only store up to 15 characters for a person's name:
package com.example;
import javax.validatio...
Now validate Person with a JUnit test ( src/test/java/com/example/TestPersonWithGroup.java in Maven project):
package com.example;
import java.util.Set;
...
Test with " mvn clean test ". The rules where groups is not specified, which belong to the javax.validation.groups.Default group, are not executed with these tests.
Created by Fang on March 06, 2012 12:24:05
Last update: March 06, 2012 12:24:05
A bean class may also be defined through composition. The validation rules of referenced beans are not automatically called when a composite bean is validated. You need to use the @Valid annotation to trigger cascade validation.
As an example, I create a class named AccountPerson2 , which contains a Person with the addition of an email field ( src/main/java/com/example/AccountPerson2.java in Maven project):
package com.example;
import javax.validatio...
Now validate AccountPerson2 with a JUnit test ( src/test/java/com/example/AccountPerson2Test.java in Maven project):
package com.example;
import java.util.Set;
...
Test with " mvn clean test " and you'll see that the validation rules of the Person class are executed when an AccountPerson2 bean is validated. Without the @Valid annotation, the validation rules of Person will not be called.
Created by Fang on March 06, 2012 12:22:48
Last update: March 06, 2012 12:22:48
When a bean class inherits another class, the validation rules of the parent class is automatically executed when a child class bean is validated.
As an example, I create a class named AccountPerson , which inherits the Person class with the addition of an email field ( src/main/java/com/example/AccountPerson.java in Maven project):
package com.example;
import javax.validatio...
Now validate AccountPerson with a JUnit test ( src/test/java/com/example/AccountPersonTest.java in Maven project):
package com.example;
import java.util.Set;
...
Test with " mvn clean test " and you'll see that the validation rules of the Person class are executed when an AccountPerson bean is validated.
Created by Fang on March 05, 2012 20:11:56
Last update: March 05, 2012 20:11:56
This is a bare bones Maven project to get started with Java JSR 303 bean validation.
Directory structure:
./pom.xml
./src
./src/main
./src/main/jav...
pom.xml :
<project xmlns="http://maven.apache.org/POM/4.0.0"...
which includes dependencies on JUnit, Java bean validation API and the Hibernate validator reference implementation.
Created by Fang on February 10, 2012 16:17:13
Last update: February 10, 2012 16:17:13
The annotation @org.hibernate.annotations.Type overrides the default hibernate mapping type used for a column. This can usually be omitted since Hibernate normaly infers the correct type to use.
But @Type is required in ambiguous scenarios such as a java.util.Date attribute, which can map to SQL DATE , TIME or TIMESTAMP . You use the @Type("timestamp") annotation to tell Hibernate that a timestamp converter should be used, which identifies an instance of org.hibernate.type.TimestampType .
@Type can also be used to identify custom type converters, which can be defined with @TypeDef at the class level:
@TypeDefs(
{
@TypeDef(
na...
or with an xml file:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mappi...