Notes by Fang
Displaying keyword search results 1 - 10
Created by Fang on April 16, 2012 13:32:10
Last update: April 16, 2012 13:32:10
There are two steps to create a custom function for JSP:
Declare the function in the TLD:
<?xml version="1.0" encoding="UTF-8" ?>
<taglib...
Implement the function (must be static):
package com.example;
public class UrlTransl...
To use the function:
<%@ taglib uri="http://www.example.com/jsp/tags" p...
Created by Fang on March 30, 2012 15:12:14
Last update: March 30, 2012 15:12:14
I tested this on Spring MVC 3.1.
<?xml version="1.0" encoding="UTF-8"?>
<beans x...
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 10:07:25
Last update: March 30, 2012 10:09:08
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 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 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 March 02, 2012 13:23:35
Last update: March 02, 2012 13:23:35
The landing page after login can be configured with the default-target-url attribute of form-login . If a user was redirected to the login form after requesting a restricted URL, she's redirected to the original requested page after successful login.
An easy configuration looks like this:
<beans:beans xmlns="http://www.springframework.org...
But there are times that you want to do more initialization after login (such as loading user data), or apply more complex logic before redirecting. This is where the authentication-success-handler-ref attribute comes into play. You create a class that implements org.springframework.security.web.authentication.AuthenticationSuccessHandler and use that as the authentication-success-handler-ref :
<http
entry-point-ref="authProcessFilterEn...
This is a skeleton implementation:
public class MyAuthenticationSuccessHandler implem...
Created by Fang on February 23, 2012 14:25:57
Last update: March 01, 2012 13:53:59
Some example snippets for Spring message configuration and usage.
To configure a message source in Spring context (basename=messages):
<bean id="messageSource"
class="org.springf...
Locale change interceptor can also be setup with:
<?xml version="1.0" encoding="UTF-8"?>
<beans x...
The messages file should be named messages.properties (or messages_en.properties , etc.) and located on CLASSPATH , for example: WEB-INF/classes .
To use a message resource in JSP:
<%@ taglib prefix="spring" uri="http://www.springf...
Created by Fang on March 01, 2012 13:52:14
Last update: March 01, 2012 13:52:14
Step 1: define the message code:
code.with.arg=arg1: {0}, arg2: {1}, arg3: {2}
Step 2: use it in the JSP with comma separated arguments:
<%@ taglib uri="http://www.springframework.org/tag...
If the argument itself contains a comma:
<%@ taglib uri="http://www.springframework.org/tag...
Created by Fang on February 21, 2012 20:54:23
Last update: February 21, 2012 20:57:57
Tomcat gzip compression filter can be turned on with " compression=on " in server.xml :
<Connector port="8080" protocol="HTTP/1.1"
...
The default compressed MIME types are: text/html,text/xml,text/plain .
It would be nice to add other types:
<Connector
port="8080"
protocol="HTT...