Thursday, January 13, 2011

Code license

Just to keep it simple for those who use code here that are made by me...
All my original contributions in this blog are placed into the public domain. If this is not legally possible, then anyone receiving a copy of them by any means is granted a non-exclusive perpetual license to use, distribute and modify them, and to distribute modifications, under any license or none, with or without attribution to me.

Tuesday, September 28, 2010

Partcover output

PartCover is a test coverage tool for .net projects. Recently, i had modified xslt files for converting the output from Partcover to a report in html. I copied the modified xslt from here and updated it to support Partcover 4.0.

Click here to download the modified xslt.

Using a custom validator with JSR 303 annotations

I was using JSR 303 annotations for validating my models but i had a scenario where i need to extend the validation by adding my own validator. So what i did was create a validator implementing spring's validator interface then extending LocalValidatorFactoryBean. The thing to note is not to instantiate the validator yourself but just add the autowire annotation and let the container inject the correct validator for you.

The model
public class User {
   @NotEmpty
   @Size(min=3, max=20)
   private String userName

   @NotEmpty
   @Size(min=1, max=30)
   private String firstName;

   @NotEmpty
   @Size(min=1, max=30)
   private String lastName;
}

A snippet of the controller
@Autowired
private Validator userValidator;

@InitBinder
protected void initBinder(WebDataBinder binder) {
   binder.setValidator(userValidator);
}

@RequestMapping(value="/create", method = RequestMethod.POST)
public String add(@Valid User user, BindingResult result) {
    if (result.hasErrors()) {
        return "/admin/createUser";
    }
    
    this.users.put(user.getUserName(), user);
    return "redirect:/admin/listUsers";
}

The custom validator
@Component
public class UserValidator extends LocalValidatorFactoryBean implements Validator { 
    @Override
    public boolean supports(Class clazz) {
        return User.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        super.validate(target, errors);
        User user = (User) target;
        if (user != null) {
            // custom validation here
        }
    }
}

Thursday, August 12, 2010

Hibernate, Oracle and the empty string

We had a model that contained an annotation specifying that it shouldn't be null. It works with Postgres when we were inserting empty strings. But with Oracle, it's throwing an exception. Apparently, Oracle converts empty strings to null. Just one of those quirks. As a workaround, we just allowed null entries.

Tuesday, June 1, 2010

Using Spring-WS for creating a client

Sample config for WebServiceTemplate

<bean id="saajMessageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
    <property name="soapVersion">
      <util:constant static-field="org.springframework.ws.soap.SoapVersion.SOAP_12"/>
    </property>
  </bean>

  <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"
        p:contextPath="com.whatever:com.another.package"/>

  <bean id="wsTemplate" class="org.springframework.ws.client.core.WebServiceTemplate"
        p:messageFactory-ref="saajMessageFactory"
        p:defaultUri="http://whatever"
        p:marshaller-ref="marshaller"
        p:unmarshaller-ref="marshaller"/>

To specify more than 1 path for the marshaller, just use colons to separate the packages.

Thursday, May 20, 2010

Accessing spring beans from your jsp

Spring MVC can expose your beans to JSTL. To expose all your beans, you can configure your view resolver like the following.
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="exposeContextBeansAsAttributes" value="true"/>
<property name="prefix" value="/WEB-INF/view"/>
<property name="suffix" value=".jsp"/>
</bean>

Alternatively, to only expose selected beans you can use exposedContextBeanNames to only expose specific beans. You can use either of those 2 properties.