Uppercase Converter

“In many situations it’s useful to store some properties in upper case. It’s a common practice of many data modeller and designers which prefer upper case for visible unique keys like codes, names, etc.”

Let’s say we have an unique requirement of changing the inputted string to its Uppercase type. There are indeed 2 ways to salvage this.

1. Using JavaScript.

<h:inputText ... onblur="value=value.toUpperCase()" />

2. Using JSF.

JSF Page:

<h:inputText value="#{exampleTableBean.object.code}" converter="UpperCaseConverter" 
             required="true" requiredMessage="#{msg.error_required_example_code}" />

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<faces-config version="1.2"			
              xmlns="http://java.sun.com/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation=
              "http://java.sun.com/xml/ns/javaee 
             	 http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd
              ">
        
    <application>
        .
        .
        .
	<converter>
	     <converter-id>UpperCaseConverter</converter-id>
	     <converter-class>com.example.util.converter.UpperCaseConverter</converter-class>
	</converter>
        .
        .
        .
    </application>
</faces-config>

Converter Class

package com.example.util.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

/**
 * @author k.mutlu
 * 
 */
public class UpperCaseConverter implements Converter {
	
	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) {
		return value.toUpperCase();
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component, Object value) {
		return value.toString();
	}
	
}

“The JS approach is extremely simple. However, this is tamperable by the enduser as it’s performed fully at the client side, under full control of the enduser. The enduser can disable/skip that JS code and/or edit the request parameter before it’s actually being sent to the server side. The JSF approach isn’t tamperable as this is performed fully at the server side, so this results in a more solid and reliable result.

You have to decide based on those facts which one fits the business requirements the best.”

Resources:
http://stackoverflow.com/questions/11021123/how-to-make-a-jsf-inputtext-field-upper-case-on-blur

http://ayushmanindia.blogspot.com/2010/09/feedjit-live-blog-stats-need-for-custom.html

http://www.dobosz.at/java/uppercase-converter/

http://riyazmsm.blogspot.com/2009/07/uppercase-converter.html

Advertisements

About kaanmutlu

Software Developer - Computer Engineer from Istanbul, Turkey
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

One Response to Uppercase Converter

  1. Diorgenes Morais says:

    Tão bom, que funcionou aqui, congratulations

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s