Recent Notes
Displaying keyword search results 1 - 10
Created by freyo on February 06, 2013 21:10:47
Last update: February 06, 2013 21:12:18
I have an old Samung phone to be used as a toy. After restoring back to factory image and power on, I was stuck at the activate service screen. Unfortunately, the four corner magic touch did not work. So I did quite a bit of digging and this is what worked on my Samsung Continuum: Press emergency call button, then at the dialer, press * # 8 3 7 8 6 6 3 3 , press the Home key From the home screen, tap phone icon, Dial * # 2 2 7 4 5 9 2 7 Enter SPC code: ______ displays tap in white box to show virtual keyboard, enter 6 digit code (default: 000000), tap OK Select “Hidden menu Enable”, tap OK From...
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 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:32:37
Last update: March 05, 2012 20:32:37
In this simple example, I create a simple validating bean and create a JUnit test to test the validation.
The bean ( src/main/java/com/example/Person.java ):
package com.example;
import javax.validatio...
The test ( src/test/java/com/example/TestPerson.java ):
package com.example;
import java.util.Set;
...
Run the test:
mvn clean test
You'll notice that one test passed and the other failed.
The tests require that a person must have a name and the name cannot be empty, so @NotNull is not the right rule to use here. To make sure that the name is not empty, we need to use @Pattern . But since a null String matches any pattern, @NotNull is also needed:
package com.example;
import javax.validatio...
Created by Fang on February 27, 2012 12:19:19
Last update: February 27, 2012 12:19:19
Mapping Java objects to Jackson JSON is pretty simple. But if you name a JSON field wrong, you'll get the "Unrecognized field ... (Class ...), not marked as ignorable" error. The rule for mapping a Java bean attribute name to a JSON field name is: lower all leading capital letters until the first lower case letter .
For example, this Java class:
package com.example;
public class Person {
...
maps to this JSON string:
{
"firstName": "Jane",
"lastName": "...
Test code:
package com.example;
import java.net.URL;
...
Created by Dr. Xi on May 02, 2011 15:59:37
Last update: February 25, 2012 09:16:37
This code snippet gets the default keystore used by the Java keytool and displays the list of aliases along with the key type (certificate or private key).
import java.io.File;
import java.io.FileInputSt...
The default keystore used by the above code is: $HOME/.keystore .
Created by Fang on November 10, 2011 13:19:13
Last update: December 01, 2011 19:10:43
You can add custom implicit variables to JSF pages by using a custom EL resolver, in two simple steps: Write an ELResolver class to resolve the variable Add the ELResolver to faces-config.xml Starting from the Maven Hello World example: Add faces API and EL dependencies to pom.xml :
<dependencies> <dependency> <groupId>o... Add a simple greeter class ( src/main/java/com/example/Greeter.java ): package com.example; public class Greeter {... Add our custom EL resolver ( src/main/java/com/example/ELResolver.java ): package com.example; import java.util.Itera... Add the custom EL resolver to src/main/resources/META-INF/faces-config.xml <?xml version="1.0" encoding="UTF-8"?> <faces-c... Build JAR with mvn package Drop the JAR into WEB-INF/lib of a webapp and test the new EL with: <h:outputText value="#{Greeter.sayHi('Mike')}"/> Fixed: the setValue method used to throw an exception, which is wrong. @Override public void setValue(ELContext ctx, O......