Recent Notes
Displaying keyword search results 1 - 10
Created by Fang on May 03, 2012 15:07:17
Last update: May 03, 2012 15:07:52
Scaling an image with default Java API loses quality (horribly!):
public static BufferedImage resizeImage(Buffer...
The imgscalr library does the job beautifully. And its' very easy to do:
// import org.imgscalr.Scalr;
public static...
To import the library in Maven:
<dependency>
<groupId>org.imgscalr</groupId>
...
Created by Dr. Xi on June 22, 2011 13:23:19
Last update: April 30, 2012 11:40:06
This is a utility to convert a byte array to hex string, and to convert a hex string back to the original.
To convert a String to hex, you must call String.getBytes() first. The utility does not pretend to know your string encoding.
public class StringHexUtil {
public static ...
This also works:
String binaryToHex(byte[] data) {
StringBui...
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 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 February 24, 2012 14:38:06
Last update: April 06, 2012 13:19:29
Step 1: create a Json factory:
package com.my.service.dev;
import java.io....
Step 2: use it:
ObjectMapper mapper = new ObjectMapper(new AllowCo...
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 Dr. Xi on March 08, 2012 12:13:57
Last update: March 08, 2012 12:13:57
This example creates an instance of XMLGregorianCalendar and converts it to java.util.Date :
import java.util.Date;
import javax.xml.datatyp...
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,...