alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Struts example source code file (AddressTypeConverter.java)

This example Struts source code file (AddressTypeConverter.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Struts tags/keywords

address, address, addresstypeconverter, class, defaulttypeconverter, defaulttypeconverter, object, object, override, string, string, util

The Struts AddressTypeConverter.java source code

package com.opensymphony.xwork2.test.annotations;

import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;

import java.util.Map;

public class AddressTypeConverter extends DefaultTypeConverter {
	@Override public Object convertValue(Map<String, Object> context, Object value, Class toType) {
		if(value instanceof String) {
			return decodeAddress((String)value);
		} else if(value instanceof String && value.getClass().isArray()) {
			return decodeAddress(((String[])value)[0]);
		} else {
			Address address = (Address)value;
			return address.getLine1() + ":" + address.getLine2() + ":" +
			       address.getCity() + ":" + address.getCountry();
		}
	}

	private Address decodeAddress(String encodedAddress) {
		String[] parts = ((String)encodedAddress).split(":");
		Address address = new Address();
		address.setLine1(parts[0]);
		address.setLine2(parts[1]);
		address.setCity(parts[2]);
		address.setCountry(parts[3]);
		return address;
	}
}

Other Struts examples (source code examples)

Here is a short list of links related to this Struts AddressTypeConverter.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.