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 May 17, 2012 19:31:39    Last update: May 17, 2012 19:31:39
To inject ServletContext into a Spring bean: implement ServletContextAware : import javax.servlet.ServletContext; import... Define bean in Spring application context: <beans:bean id="myBean" class="com.example.M...
Created by Fang on April 16, 2012 13:18:40    Last update: April 16, 2012 13:18:40
Simply call pageContext.setAttribute() to export a variable from within a JSP custom tag: public class MyCustomVarTag extends TagSupport { ... The availability of the exported variable can be limited in the TLD: <tag> <name>setVar</name> <tag-class... The availability scopes are: Value Availability NESTED Between the start tag and the end tag. AT_BEGIN From the start tag until the scope of any enclosing tag. If there’s no enclosing tag, then to the end of the page. AT_END After the end tag until the scope of any enclosing tag. If there’s no enclosing tag, then to the end of the page.
Created by Fang on April 16, 2012 12:58:35    Last update: April 16, 2012 12:58:35
To implement a JSP custom tag with dynamic attributes (for example, to pass-thru arbitrary attributes not handled by the JSP tag): Set the dynamic-attributes element to true in the TLD: <tag> <name>mark</name> <tag-class>c... The tag handler must implement javax.servlet.jsp.tagext.DynamicAttributes : package com.example.jsp; import java.io.*; ...
Created by Fang on March 15, 2012 10:24:35    Last update: March 15, 2012 10:24:35
Suppose that I have an email field annotated with: @NotEmpty(message="Please enter email address") ... Bean validation will trigger two errors when no email address is entered: the email field is empty an empty email field is not a valid email address Displaying both errors to the user with <form:errors> would be redundant and confusing: <%@ taglib uri="http://www.springframework.org/tag... This is how to display the first error only: <spring:bind path="emailAddress"> <c:if test="$...
Created by Fang on March 06, 2012 14:38:52    Last update: March 06, 2012 15:56:45
This may or may not be useful, but I did the research so here's the code. import org.springframework.beans.factory.annotatio...
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.
Previous  1 2 Next