<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>lstierneyltd &#187; Development</title>
	<atom:link href="http://lstierneyltd.com/blog/category/development/feed/" rel="self" type="application/rss+xml" />
	<link>http://lstierneyltd.com/blog</link>
	<description>Yet another development blog</description>
	<lastBuildDate>Wed, 13 Jul 2011 12:55:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Java to XML, XML to Java (Marshalling and Unmarshalling)</title>
		<link>http://lstierneyltd.com/blog/development/examples/java-to-xml-xml-to-java-marshalling-and-unmarshalling/</link>
		<comments>http://lstierneyltd.com/blog/development/examples/java-to-xml-xml-to-java-marshalling-and-unmarshalling/#comments</comments>
		<pubDate>Wed, 13 Jul 2011 12:55:13 +0000</pubDate>
		<dc:creator>lawrence</dc:creator>
				<category><![CDATA[Examples]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jaxb]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://lstierneyltd.com/blog/?p=273</guid>
		<description><![CDATA[Introduction JDK6 and JAXB2.x (which comes with JDK6) make marshalling Java to XML and unmarshalling XML to Java a snap, almost trivial. Example Java to XML XML to Java product.java Note: the @XmlRootElement is vital here! product.xml]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>JDK6 and JAXB2.x (which comes with JDK6) make marshalling Java to XML and unmarshalling XML to Java a snap, almost trivial.</p>
<h3>Example</h3>
<h4>Java to XML</h4>
<pre class="brush: java; title: ; notranslate">
package foo.bar;

import java.math.BigDecimal;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class JavaToXML {
	public static void main(String[] args) throws Exception {
		JAXBContext context = JAXBContext.newInstance(Product.class);

		Marshaller m = context.createMarshaller();
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

		Product object = new Product();
		object.setCode(&quot;WI1&quot;);
		object.setName(&quot;Widget Number One&quot;);
		object.setPrice(BigDecimal.valueOf(300.00));

		m.marshal(object, System.out);
	}
}
</pre>
<h4>XML to Java</h4>
<pre class="brush: java; title: ; notranslate">
package foo.bar;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class XMLToJava {

	public static void main(String[] args) {
		try {
			JAXBContext jc = JAXBContext.newInstance(Product.class);
			Unmarshaller u = jc.createUnmarshaller();

			File f = new File(&quot;product.xml&quot;);
			Product product = (Product) u.unmarshal(f);

			System.out.println(product.getCode());
			System.out.println(product.getName());
			System.out.println(product.getPrice());
		} catch (JAXBException e) {
			e.printStackTrace();
		}
	}
}
</pre>
<h4>product.java</h4>
<p>Note: the @XmlRootElement is vital here!</p>
<pre class="brush: java; title: ; notranslate">
package foo.bar;

import java.math.BigDecimal;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Product {
	private String code;
	private String name;
	private BigDecimal price;

	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public BigDecimal getPrice() {
		return price;
	}
	public void setPrice(BigDecimal price) {
		this.price = price;
	}
}
</pre>
<h4>product.xml</h4>
<pre class="brush: xml; title: ; notranslate">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?&gt;
&lt;product&gt;
    &lt;code&gt;WI1&lt;/code&gt;
    &lt;name&gt;Widget Number One&lt;/name&gt;
    &lt;price&gt;300.0&lt;/price&gt;
&lt;/product&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://lstierneyltd.com/blog/development/examples/java-to-xml-xml-to-java-marshalling-and-unmarshalling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Processing Instruction Target Matching &#8220;[xX][mM][lL]&#8221; is Not Allowed</title>
		<link>http://lstierneyltd.com/blog/development/tips/the-processing-instruction-target-matching-xxmmll-is-not-allowed/</link>
		<comments>http://lstierneyltd.com/blog/development/tips/the-processing-instruction-target-matching-xxmmll-is-not-allowed/#comments</comments>
		<pubDate>Fri, 24 Jun 2011 08:54:18 +0000</pubDate>
		<dc:creator>lawrence</dc:creator>
				<category><![CDATA[quick tips]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://lstierneyltd.com/blog/?p=268</guid>
		<description><![CDATA[Background When parsing XML you receive the following error: ...The Processing Instruction Target Matching "[xX][mM][lL]" is Not Allowed... Solution The chances are you have some sort of whitespace (or control character) infront of your XML declaration: ..&#60;?xml version="1.0" encoding="utf-8"?&#62; It may even be that you have more than one XML declaration in the document..!]]></description>
			<content:encoded><![CDATA[<h4>Background</h4>
<p>When parsing XML you receive the following error:</p>
<pre>
...The Processing Instruction Target Matching "[xX][mM][lL]" is Not Allowed...
</pre>
<h4>Solution</h4>
<p>The chances are you have some sort of whitespace (or control character) infront of your XML declaration:</p>
<pre>
..&lt;?xml version="1.0" encoding="utf-8"?&gt;
</pre>
<p>It may even be that you have more than one XML declaration in the document..!</p>
]]></content:encoded>
			<wfw:commentRss>http://lstierneyltd.com/blog/development/tips/the-processing-instruction-target-matching-xxmmll-is-not-allowed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Read a File as String with Java</title>
		<link>http://lstierneyltd.com/blog/development/tips/read-a-file-as-string-with-java/</link>
		<comments>http://lstierneyltd.com/blog/development/tips/read-a-file-as-string-with-java/#comments</comments>
		<pubDate>Thu, 16 Jun 2011 08:19:36 +0000</pubDate>
		<dc:creator>lawrence</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[How to's]]></category>
		<category><![CDATA[quick tips]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://lstierneyltd.com/blog/?p=264</guid>
		<description><![CDATA[Introduction I&#8217;m always Googling for a way to do this. This seems to be the best &#8220;idiomatic&#8221; solution I&#8217;ve found. So without further ado&#8230; Example]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>I&#8217;m always Googling for a way to do this. This seems to be the best &#8220;idiomatic&#8221; solution I&#8217;ve found. So without further ado&#8230;</p>
<h3>Example</h3>
<pre class="brush: java; title: ; notranslate">
public String readFile(String path) throws IOException {
    FileInputStream stream = new FileInputStream(new File(path));
    try{
        FileChannel fc = stream.getChannel();
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        return Charset.defaultCharset().decode(bb).toString();
    }
    finally {
        stream.close();
    }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://lstierneyltd.com/blog/development/tips/read-a-file-as-string-with-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>View Source of Oracle Trigger</title>
		<link>http://lstierneyltd.com/blog/development/tips/view-source-of-oracle-trigger/</link>
		<comments>http://lstierneyltd.com/blog/development/tips/view-source-of-oracle-trigger/#comments</comments>
		<pubDate>Wed, 11 May 2011 12:09:09 +0000</pubDate>
		<dc:creator>lawrence</dc:creator>
				<category><![CDATA[quick tips]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://lstierneyltd.com/blog/?p=257</guid>
		<description><![CDATA[select trigger_body from user_triggers where trigger_name = 'XXXXX']]></description>
			<content:encoded><![CDATA[<pre>
select trigger_body from user_triggers where trigger_name = 'XXXXX'
</pre>
]]></content:encoded>
			<wfw:commentRss>http://lstierneyltd.com/blog/development/tips/view-source-of-oracle-trigger/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unit Test Private Java Methods using Reflection</title>
		<link>http://lstierneyltd.com/blog/development/unit-test-private-java-methods-using-reflection/</link>
		<comments>http://lstierneyltd.com/blog/development/unit-test-private-java-methods-using-reflection/#comments</comments>
		<pubDate>Thu, 05 May 2011 17:02:06 +0000</pubDate>
		<dc:creator>lawrence</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[How to's]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://lstierneyltd.com/blog/?p=254</guid>
		<description><![CDATA[Introduction I realise that the thought of Unit Testing private methods in Java classes can be like a red rag to a bull for some people and I understand the arguments. Therefore, I present the following as a &#8220;how to&#8221;, not a moral argument for or against! Example The class under test The (Reflection Based) [...]]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>I realise that the thought of Unit Testing private methods in Java classes can be like a red rag to a bull for some people and I understand the arguments.</p>
<p>Therefore, I present the following as a &#8220;how to&#8221;, not a moral argument for or against!</p>
<h3>Example</h3>
<h4>The class under test</h4>
<pre class="brush: java; title: ; notranslate">
public class Product() {
    private String privateMethod(String id) {
    //Do something private
    return &quot;product_&quot; + id;
  }
}
</pre>
<h4>The (Reflection Based) Unit Test</h4>
<pre class="brush: java; title: ; notranslate">
import java.lang.reflect.Method;

import static org.junit.Assert.*;
import org.junit.Test;

public class ProductTest {
    private Product product; // the class under test
    private Method m;
    private static String METHOD_NAME = &quot;privateMethod&quot;;
    private Class[] parameterTypes;
    private Object[] parameters;

    @Before
    public void setUp() throws Exception {
        product = new Product();
        parameterTypes = new Class[1];
        parameterTypes[0] = java.lang.String.class;
        m = product.getClass().getDeclaredMethod(METHOD_NAME, parameterTypes);
        m.setAccessible(true);
        parameters = new Object[1];
    }

    @Test
    public void testPrivateMethod() throws Exception {
        parameters[0] = &quot;someIdentifier&quot;;
        String result = (String) m.invoke(product, parameters); 

        //Do your assertions
        assertNotNull(result);
    }
}
</pre>
<h3>Update</h3>
<p>I&#8217;ve since been told that if <a href="http://code.google.com/p/dp4j/" target="_blank">dp4j.jar</a> is in the classpath at compile-time, it will inject the necessary reflection to make this work. I haven&#8217;t had time to try this yet so YMMV.</p>
]]></content:encoded>
			<wfw:commentRss>http://lstierneyltd.com/blog/development/unit-test-private-java-methods-using-reflection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Oracle&#8217;s SYS_GUID()</title>
		<link>http://lstierneyltd.com/blog/development/using-oracles-sys_guid/</link>
		<comments>http://lstierneyltd.com/blog/development/using-oracles-sys_guid/#comments</comments>
		<pubDate>Fri, 29 Apr 2011 12:28:26 +0000</pubDate>
		<dc:creator>lawrence</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://lstierneyltd.com/blog/?p=250</guid>
		<description><![CDATA[Background For years I&#8217;ve bemoaned Oracle&#8217;s lack of an auto-number/identity type column (think MySQL AUTO_INCREMENT). Obviously you can create sequences and triggers to produce the same effect but it always seemed like a bit of a palaver to me. So, I had a smile on my when I discovered the delight that is SYS_GUID(). Taken [...]]]></description>
			<content:encoded><![CDATA[<h3>Background</h3>
<p>For years I&#8217;ve bemoaned Oracle&#8217;s lack of an auto-number/identity type column (think MySQL AUTO_INCREMENT). Obviously you can create sequences and triggers to produce the same effect but it always seemed like a bit of a palaver to me. </p>
<p>So, I had a smile on my when I discovered the delight that is SYS_GUID().</p>
<p>Taken from the <a href="http://download.oracle.com/docs/cd/B14117_01/server.101/b10759/functions153.htm" target="_blank">Oracle documentation</a>:</p>
<blockquote><p>
SYS_GUID generates and returns a globally unique identifier (RAW value) made up of 16 bytes. On most platforms, the generated identifier consists of a host identifier, a process or thread identifier of the process or thread invoking the function, and a nonrepeating value (sequence of bytes) for that process or thread.
</p></blockquote>
<h3>Example</h3>
<pre class="brush: sql; title: ; notranslate">
select SYS_GUID() from dual

  SYS_GUID()
= ================================
1 A20D42BA1F165DB5E04400144FB99F0A
</pre>
<h3>SYS_GUID() as a default</h3>
<p>Because SYS_GUID() &#8220;returns a globally unique identifier&#8221; we can use it as a default value in a Primary Key column of a table.</p>
<pre class="brush: sql; title: ; notranslate">
create table products(
    product_id raw(32) default sys_guid() not null primary key
    ............
);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://lstierneyltd.com/blog/development/using-oracles-sys_guid/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unit Testing Validation in Annotation Based Validating Spring Beans</title>
		<link>http://lstierneyltd.com/blog/development/examples/spring-examples/unit-testing-validation-in-annotation-based-validating-spring-beans/</link>
		<comments>http://lstierneyltd.com/blog/development/examples/spring-examples/unit-testing-validation-in-annotation-based-validating-spring-beans/#comments</comments>
		<pubDate>Fri, 22 Apr 2011 08:57:07 +0000</pubDate>
		<dc:creator>lawrence</dc:creator>
				<category><![CDATA[Spring]]></category>
		<category><![CDATA[annotations]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://lstierneyltd.com/blog/?p=245</guid>
		<description><![CDATA[Motivation I added validation, via annotations, to a Spring &#8220;Model&#8221; bean. I needed someway to Unit Test this validation, without running the container and without initialising the Spring context. The bean (simplified) The Unit Test More It actually took me a few hours to work the above test out (simple as it is). If I [...]]]></description>
			<content:encoded><![CDATA[<h3>Motivation</h3>
<p>I added validation, via annotations, to a Spring &#8220;Model&#8221; bean. I needed someway to Unit Test this validation, <em>without</em> running the container and <em>without</em> initialising the Spring context.</p>
<h3>The bean (simplified)</h3>
<pre class="brush: java; title: ; notranslate">
package foo.bar;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

public final class ProductModel {

    @NotNull
    @Size(max=100)
    @Pattern(regexp=&quot;[^\n^\t^\r]+&quot;, message=&quot;Long Name must not contain New Lines, Carriage Returns or Tabs&quot;)
    private String longName;

    @Size(max=20)
    private String shortName;

    // rest snipped for brevity
}
</pre>
<h3>The Unit Test</h3>
<pre class="brush: java; title: ; notranslate">
package foo.bar;

import java.util.Set;

import javax.validation.ConstraintViolation;

import junit.framework.Assert;

import org.hibernate.validator.HibernateValidator;
import org.junit.Before;
import org.junit.Test;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

public class Temp {
    private LocalValidatorFactoryBean localValidatorFactory;

    @Before
    public void setup() {
        localValidatorFactory = new LocalValidatorFactoryBean();
        localValidatorFactory.setProviderClass(HibernateValidator.class);
        localValidatorFactory.afterPropertiesSet();
    }
    @Test
    public void testLongNameWithInvalidCharCausesValidationError() {
        final ProductModel productModel = new ProductModel();
        productModel.setLongName(&quot;A long name with\t a Tab character&quot;);
    	Set&lt;ConstraintViolation&lt;ProductModel&gt;&gt; constraintViolations = localValidatorFactory.validate(productModel);
    	Assert.assertTrue(&quot;Expected validation error not found&quot;, constraintViolations.size() == 1);
    }
}
</pre>
<h3>More</h3>
<p>It actually took me a few hours to work the above test out (simple as it is). If I hadn&#8217;t stumbled upon <a href="https://src.springsource.org/svn/spring-framework/trunk/org.springframework.context/src/test/java/org/springframework/validation/beanvalidation/ValidatorFactoryTests.java">these Spring Unit Tests</a>, I might never have got it.</p>
]]></content:encoded>
			<wfw:commentRss>http://lstierneyltd.com/blog/development/examples/spring-examples/unit-testing-validation-in-annotation-based-validating-spring-beans/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring MVC URI Template Example</title>
		<link>http://lstierneyltd.com/blog/development/examples/spring-examples/spring-mvc-uri-template-example/</link>
		<comments>http://lstierneyltd.com/blog/development/examples/spring-examples/spring-mvc-uri-template-example/#comments</comments>
		<pubDate>Mon, 18 Apr 2011 14:04:25 +0000</pubDate>
		<dc:creator>lawrence</dc:creator>
				<category><![CDATA[Spring]]></category>
		<category><![CDATA[annotations]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://lstierneyltd.com/blog/?p=241</guid>
		<description><![CDATA[Background So, I&#8217;m working on an existing Spring 3.x web project. This project heavily favours the use of annotations over XML configuration. As I was trawling through the source code I noticed &#8220;some wierd&#8221; syntax: &#8220;/{xxxx}&#8221; (which of course I had to look up!). Most of the following is based upon the excellent Spring Documentation. [...]]]></description>
			<content:encoded><![CDATA[<h3>Background</h3>
<p>So, I&#8217;m working on an existing Spring 3.x web project. This project heavily favours the use of annotations over XML configuration. As I was trawling through the source code I noticed &#8220;some wierd&#8221; syntax: &#8220;/{xxxx}&#8221; (which of course I had to look up!).</p>
<p>Most of the following is based upon the excellent <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib" target="_blank">Spring Documentation</a>. I&#8217;ve lifted and rewritten the parts I require to serve as a condensed &#8220;aide-mémoire&#8221;.</p>
<h3>Description</h3>
<p>A URI Template is a URI-like string, containing one or more variable names. When you substitute values for these variables, the template becomes a URI.<br />
<code><span id="more-241"></span></code><br />
For example, the URI Template</p>
<pre>http://foo.bar.com/products/{productName}</pre>
<p>contains the variable &#8220;productName&#8221;. If we assign the variable the value &#8220;widgetA&#8221;, the URI Template yields:</p>
<pre>http://foo.bar.com/products/widgetA</pre>
<h3>Example</h3>
<pre class="brush: java; title: ; notranslate">
@Controller
public class ProductController {
    @RequestMapping(value=&quot;/products/{productName}, method = RequestMethod.GET)
    public String getProductDetails(@PathVariable(&quot;productName&quot;) String productName, Model model) {
        Product product = this.productService.getProduct(productName);
        model.addAttribute(&quot;productDetails&quot;, product);
        return &quot;productDetailPage&quot;;
    }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://lstierneyltd.com/blog/development/examples/spring-examples/spring-mvc-uri-template-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring MVC 3 @ModelAttribute Annotation Example</title>
		<link>http://lstierneyltd.com/blog/development/examples/spring-examples/spring-mvc-3-modelattribute-annotation-example/</link>
		<comments>http://lstierneyltd.com/blog/development/examples/spring-examples/spring-mvc-3-modelattribute-annotation-example/#comments</comments>
		<pubDate>Mon, 18 Apr 2011 10:53:01 +0000</pubDate>
		<dc:creator>lawrence</dc:creator>
				<category><![CDATA[Spring]]></category>
		<category><![CDATA[annotations]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://lstierneyltd.com/blog/?p=236</guid>
		<description><![CDATA[Background So, I&#8217;m working on an existing Spring 3.x web project. This project heavily favours the use of annotations over XML configuration files. As I was trawling through the source code I noticed the &#8220;@ModelAttribute&#8221; annotation (which of course I had to look up!). If the above background seems familiar that&#8217;s because it&#8217;s the same [...]]]></description>
			<content:encoded><![CDATA[<h3>Background</h3>
<p>So, I&#8217;m working on an existing Spring 3.x web project. This project heavily favours the use of annotations over XML configuration files. As I was trawling through the source code I noticed the &#8220;@ModelAttribute&#8221; annotation (which of course I had to look up!).</p>
<p>If the above background seems familiar that&#8217;s because it&#8217;s the same as for a previous Spring Annotation post I made (ed: this is in danger of turning into a mini-series)</p>
<h3>Description</h3>
<p>Most of the following is based upon the excellent <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib" target="_blank">Spring Documentation</a>. I&#8217;ve simply lifted and rewritten the parts I require to serve as an &#8220;aide-mémoire&#8221;.</p>
<p>The @ModelAttribute annotation serves two purposes depending on how it is used:</p>
<p><code><span id="more-236"></span></code></p>
<h4>At Method level</h4>
<p>Use @ModelAttribute at the method level to <em>provide reference data for the model</em>. @ModelAttribute annotated methods are executed before the chosen @RequestMapping annotated handler method. They effectively pre-populate the implicit model with specific attributes, often loaded from a database. Such an attribute can then already be accessed through @ModelAttribute annotated handler method parameters in the chosen handler method, potentially with binding and validation applied to it.</p>
<p>In other words; a method annotated with @ModelAttribute will populate the specified &#8220;key&#8221; in the model. This happens BEFORE the @RequestMapping</p>
<h4>At Method Parameter level</h4>
<p>When you place @ModelAttribute on a method parameter, @ModelAttribute maps a model attribute to the specific, annotated method parameter. This is how the controller gets a reference to the object holding the data entered in the form.</p>
<h3>Examples</h3>
<h4>Method Level</h4>
<pre class="brush: java; title: ; notranslate">
@Controller
public class MyController {
    @ModelAttribute(&quot;productsList&quot;)
    public Collection&lt;Product&gt; populateProducts() {
        return this.productsService.getProducts();
    }
    // @RequestMapping etc omitted for brevity
}
</pre>
<p>So, in the above example, &#8220;productsList&#8221; in the Model is populated before the the @RequestMapping is performed.</p>
<h4>Method parameter level</h4>
<pre class="brush: java; title: ; notranslate">
@Controller
public class MyController {
    @RequestMapping(method = RequestMethod.POST)
    public String processSubmit(@ModelAttribute(&quot;product&quot;) Product myProduct, BindingResult result, SessionStatus status) {

        new ProductValidator().validate(myProduct, result);
        if (result.hasErrors()) {
            return &quot;productForm&quot;;
        }
        else {
            this.productsService.saveProduct(myProduct);
            status.setComplete();
            return &quot;productSaved&quot;;
        }
    }
}
</pre>
<p>In the above example &#8220;myProduct&#8221; is populated with the corresponding value from the Model.</p>
]]></content:encoded>
			<wfw:commentRss>http://lstierneyltd.com/blog/development/examples/spring-examples/spring-mvc-3-modelattribute-annotation-example/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Spring MVC 3.x @PathVariable Annotation Example</title>
		<link>http://lstierneyltd.com/blog/development/spring-mvc-3-x-pathvariable-annotation-example/</link>
		<comments>http://lstierneyltd.com/blog/development/spring-mvc-3-x-pathvariable-annotation-example/#comments</comments>
		<pubDate>Fri, 15 Apr 2011 13:41:09 +0000</pubDate>
		<dc:creator>lawrence</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Examples]]></category>
		<category><![CDATA[annotations]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://lstierneyltd.com/blog/?p=228</guid>
		<description><![CDATA[Background So, I&#8217;m working on an existing Spring 3.x web project. This project heavily favours the use of annotations over XML configuration files. As I was trawling through the source code I noticed the &#8220;@PathVariable&#8221; annotation (which of course I had to look up!). Example Imagine you&#8217;d like to create a controller that would allow [...]]]></description>
			<content:encoded><![CDATA[<h3>Background</h3>
<p>So, I&#8217;m working on an existing Spring 3.x web project. This project heavily favours the use of annotations over XML configuration files. As I was trawling through the source code I noticed the &#8220;@PathVariable&#8221; annotation (which of course I had to look up!).</p>
<h3>Example</h3>
<p>Imagine you&#8217;d like to create a controller that would allow users to see view products by using a URL such as http://foo.bar.com/productDetails/someProduct</p>
<pre>
e.g.

http://foo.bar.com/productDetails/widgetA - display details for widgetA
http://foo.bar.com/productDetails/widgetB - display details for widgetB
</pre>
<p>The @PathVariable annotation makes this easy&#8230;</p>
<pre class="brush: java; title: ; notranslate">
package foo.bar;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

@Controller
public class ShowProductController {

    @RequestMapping(&quot;/productDetails/{productName}&quot;) // Handle any request of the form &quot;/productDetails/XXXXX&quot;
    public String showProduct(Model model, @PathVariable(&quot;productName&quot;) String productName) {
        model.addAttribute(&quot;productName&quot;, productName);
        return &quot;showProduct&quot;; // viewname
    }
}
</pre>
<h3>What just happened?</h3>
<p>Note the {productName} in the @RequestMapping &#8211; this is the path variable. The @PathVariable annotation on the next line captures the value and binds it to the method argument</p>
]]></content:encoded>
			<wfw:commentRss>http://lstierneyltd.com/blog/development/spring-mvc-3-x-pathvariable-annotation-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

