<?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"
	>

<channel>
	<title>JT Dev</title>
	<atom:link href="http://www.jtict.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jtict.com/blog</link>
	<description>About programming stuff</description>
	<pubDate>Thu, 24 Jul 2008 15:54:28 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
	<language>en</language>
			<item>
		<title>Shorter Grails textField</title>
		<link>http://www.jtict.com/blog/shorter-grails-textfield/</link>
		<comments>http://www.jtict.com/blog/shorter-grails-textfield/#comments</comments>
		<pubDate>Thu, 24 Jul 2008 15:53:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Web programming]]></category>

		<category><![CDATA[grails]]></category>

		<guid isPermaLink="false">http://www.jtict.com/blog/?p=23</guid>
		<description><![CDATA[Making textFields in Grails is a bit verbose, at least if you want error highlighting and returned values on errors. This post shows a quick solution.
Problem
A full textField would be:
&#60;g:textField class=&#34;${hasErrors(bean:user, field:&#039;name&#039;, &#039;errors&#039;)}&#34; value=&#34;${fieldValue(bean:user,field:&#039;name&#039;)}&#34; name=&#34;name&#34;/&#62;
Which clearly is very verbose; you have to specify &#8220;user&#8221; twice, and &#8220;name&#8221; even three times. This verbosity also makes the [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Making textFields in Grails is a bit verbose, at least if you want error highlighting and returned values on errors. This post shows a quick solution.</strong></p>
<h3>Problem</h3>
<p>A full textField would be:</p>
<pre class="syntax-highlight:xml">&lt;g:textField class=&quot;${hasErrors(bean:user, field:&#039;name&#039;, &#039;errors&#039;)}&quot; value=&quot;${fieldValue(bean:user,field:&#039;name&#039;)}&quot; name=&quot;name&quot;/&gt;</pre>
<p>Which clearly is very verbose; you have to specify &#8220;user&#8221; twice, and &#8220;name&#8221; even three times. This verbosity also makes the view less readable. </p>
<p>Googling for this problem resulted in: <a href="http://docs.codehaus.org/display/GRAILS/Smarter+grails+tags">Smarter Grails Tags</a> (a proposal). Funny that the writer exactly sees the same problem in it, also uses the &#8220;user&#8221; domain model, and also knows Stripes where you could just use something like</p>
<pre class="syntax-highlight:xml">&lt;stripes:text name=&quot;name&quot;/&gt;</pre>
<p> and specify the bean name in the form. (Stripes has a very good <a href="http://www.stripesframework.org/display/stripes/Quick+Start+Guide">Quick Start Guide</a>, which also shows error highlighting.)</p>
<p>One of the less verbose proposed formats of textField is:</p>
<pre class="syntax-highlight:xml">&lt;g:textField bean=&quot;user&quot; field=&quot;username&quot; /&gt;</pre>
<h3>Implemented Solution</h3>
<p>I found that implementing a quick version of this is surprisingly easy in Grails, it only takes for a few statements. Here is the code:</p>
<pre class="syntax-highlight:java">
import org.codehaus.groovy.grails.plugins.web.taglib.FormTagLib

// file: project/grails-app/taglib/MyTagLib.groovy
class MyTagLib {
    // tagname &quot;myTextField&quot; within the &quot;g&quot; namespace
    def myTextField = {attrs -&gt;
        // If a controller returned the bean, and the field has an error,
        // then &quot;errors&quot; will be returned as HTML class, otherwise the class will be empty.
        attrs.class = hasErrors(bean:attrs.bean, field:attrs.field, &#039;errors&#039;)
        // Retrieves the field value of the given bean to be rendered in the view.
        // Note: specify the bean and not the bean name. So &quot;${user}&quot; instead of &quot;user&quot;
        attrs.value = fieldValue(bean:attrs.bean, field:attrs.field)
        // Required for textField taglib. attrs.name is a keyname of the params map
        attrs.name = attrs.field
        // renders the HTML tag
        out &lt;&lt; new FormTagLib().textField(attrs)
    }
}
</pre>
<h3>Usage</h3>
<pre class="syntax-highlight:xml">&lt;g:myTextField bean=&quot;$user&quot; field=&quot;name&quot; /&gt;</pre>
<p>And ofcourse some CSS to highlight the error in a color. This can be put in the HTML head (or better, a seperate CSS file) :</p>
<pre class="syntax-highlight:xml">
&lt;style type=&quot;text/css&quot;&gt;
   .errors {
      background-color: red;
   }
&lt;/style&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.jtict.com/blog/shorter-grails-textfield/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Grails and existing Java Hibernate DAO&#8217;s</title>
		<link>http://www.jtict.com/blog/grails-existing-hibernate-dao/</link>
		<comments>http://www.jtict.com/blog/grails-existing-hibernate-dao/#comments</comments>
		<pubDate>Sun, 20 Jul 2008 16:02:57 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Web programming]]></category>

		<category><![CDATA[grails]]></category>

		<category><![CDATA[hibernate]]></category>

		<guid isPermaLink="false">http://www.jtict.com/blog/?p=22</guid>
		<description><![CDATA[If you have some existing Java Hibernate entity classes (outside Grails), you only have to add one line in Datasource config, and you can use them immediately. However, if you have some exising POJO (Plain Old Java Object) Hibernate DAO&#8217;s (classes that implement a Data Access Object pattern), you&#8217;ll have to do some additional steps [...]]]></description>
			<content:encoded><![CDATA[<p><strong>If you have some existing Java Hibernate entity classes (outside Grails), you only have to add one line in Datasource config, and you can use them immediately. However, if you have some exising POJO (Plain Old Java Object) Hibernate DAO&#8217;s (classes that implement a Data Access Object pattern), you&#8217;ll have to do some additional steps to make it a bit suitable.</strong></p>
<p>(The solution described in this post can also be handy when you need data access on places where GORM is NOT available. GORM not available? Yes, GORM is not fully available when you access collections via a taglib that is accessed via a Sitemesh layout. (A scenerio that isn&#8217;t rare.) (Should be considered as a bug?) In that case you will get a <em>lazily initialize a collection</em> error. If you use HibernateUtil, you can get around this by just opening new session yourself.)</p>
<h3>Existing Hibernate entities and GORM</h3>
<p>You can use existing Hibernate Java entities directly in Grails. The only thing that you&#8217;ll have to configure is:</p>
<ol>
<li>define mappings in hibernate.cfg.xml</li>
<li>set GrailsAnnotationConfiguration as configClass in DataSource.groovy</li>
</ol>
<p>For example:<br />
webapp/grails-app/conf/hibernate/hibernate.cfg.xml :</p>
<pre class="syntax-highlight:xml">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;!DOCTYPE hibernate-configuration PUBLIC
		&quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&quot;
		&quot;http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd&quot;&gt;
&lt;hibernate-configuration&gt;
    &lt;session-factory&gt;
        &lt;mapping class=&quot;myPackage.Product&quot;/&gt;
        &lt;mapping class=&quot;myPackage.Customer&quot;/&gt;
        &lt;mapping class=&quot;myPackage.Supplier&quot;/&gt;
    &lt;/session-factory&gt;
&lt;/hibernate-configuration&gt;
</pre>
<p>webapp/grails-app/conf/DataSource.groovy :</p>
<pre class="syntax-highlight:java">
import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsAnnotationConfiguration

dataSource {
    configClass = GrailsAnnotationConfiguration.class
    pooled = true
    driverClassName = &quot;com.mysql.jdbc.Driver&quot;
    url = &quot;jdbc:mysql://localhost/mydb&quot;
    username = &quot;myusername&quot;
    password = &quot;mypass&quot;
    dbCreate = &quot;create&quot; // one of &#039;create&#039;, &#039;create-drop&#039;,&#039;update&#039;
    dialect= org.hibernate.dialect.MySQLInnoDBDialect
}
hibernate {
    cache.use_second_level_cache=true
    cache.use_query_cache=true
    cache.provider_class=&#039;com.opensymphony.oscache.hibernate.OSCacheProvider&#039;
}
</pre>
<p>Now you can put your existing Hibernate java entities in <em>webapp/src/entities</em> (or any other package), and they will work immediately and have GORM functionality.<br />
For example, in the Grails controller classes, you can direcly use Product.get(100) (to get a Product instance that has id 100). Hibernate sessions and transactions are created/invoked automatically.</p>
<h3>Exising Hibernate DAO&#8217;s</h3>
<p>If you don&#8217;t need any GORM functionality and you don&#8217;t want Grails to touch sessions, then you can use your existing unaltered full hibernate.cfg.xml.</p>
<p>However, if you have some POJO DAO&#8217;s that use the HibernateUtil helperclass, you will have to do some additional steps to make it a bit suitable. (In this example, I use the configurations above.)</p>
<p><strong>By default Grails injects a SessionFactory</strong><br />
First, your DAO&#8217;s will need a HibernateSession to perform work. By default Grails injects a SessionFactory into Grails controllers that have a &#8220;sessionFactory&#8221; variable defined. So the following controller will automatically have a SessionFactory available:</p>
<pre class="syntax-highlight:java">
import org.hibernate.*;

class ProductController {
    // This one will be injected
    SessionFactory sessionFactory

    def index = {
        Session session = sessionFactory.getCurrentSession()
        println session.createQuery(&quot;from Product&quot;).list()
    }
}
</pre>
<p>(You can also use Gorm in this controller example and don&#8217;t use the SessionFactory yourself, but this just to show that a SessionFactory will be injected.)</p>
<p><strong>HibernateUtil used by the DAO&#8217;s</strong><br />
In the example, the DAO&#8217;s use HibernateUtil to get a SessionFactory. By default HibernateUtil create a SessionFactory itself, but the SessionFactory provided by Grails is needed instead, so we alter HibernateUtil slightly:</p>
<pre class="syntax-highlight:java">
package persistence;

import org.hibernate.*;

public class HibernateUtil {
    private static SessionFactory sessionFactory;

    public static SessionFactory getSessionFactory() { return sessionFactory; }
    public static void setSessionFactory(SessionFactory sf) { sessionFactory = sf; }

    public static void shutdown() { sessionFactory.close(); }
}
</pre>
<p><strong>Set SessionFactory for HibernateUtil</strong><br />
We want HibernatUtil to be ready with a SessionFactory after booting, so we configure HibernateUtil during the bootstap. We alter webapp/grails-app/conf/BootStrap.groovy to:</p>
<pre class="syntax-highlight:java">
import persistence.HibernateUtil
import org.springframework.web.context.support.WebApplicationContextUtils

class BootStrap {

     def init = { servletContext -&gt;
        def ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)
        def sf  = ctx.getBean(&#039;sessionFactory&#039;)
        HibernateUtil.setSessionFactory(sf)
     }

     def destroy = {
         HibernateUtil.shutdown()
     }
}
</pre>
<p>Now the DAO&#8217;s can use HibernateUtil.getSessionFactory().getCurrentSession() to get a HibernateSession.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jtict.com/blog/grails-existing-hibernate-dao/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Microsoft Access and informative id keys</title>
		<link>http://www.jtict.com/blog/access-informative-ids/</link>
		<comments>http://www.jtict.com/blog/access-informative-ids/#comments</comments>
		<pubDate>Thu, 17 Jul 2008 17:19:26 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[databases]]></category>

		<category><![CDATA[access2007]]></category>

		<guid isPermaLink="false">http://www.jtict.com/blog/?p=13</guid>
		<description><![CDATA[(Programmers usually don&#8217;t use Microsoft Access in multi user apps, but it can be handy if you quickly need to setup some single user personal administration database.)
A thing that I find very cumbersome in Access, is the use of surrogate keys (e.g. autonumbering IDs instead of business keys like &#8220;name&#8221;), because autonumbering keys are not [...]]]></description>
			<content:encoded><![CDATA[<p><strong>(Programmers usually don&#8217;t use Microsoft Access in multi user apps, but it can be handy if you quickly need to setup some single user personal administration database.)</strong></p>
<p><strong>A thing that I find very cumbersome in Access, is the use of surrogate keys (e.g. autonumbering IDs instead of business keys like &#8220;name&#8221;), because autonumbering keys are not very informative. This blog post tells you how you can show informative field while at the same time keep using your existing surrogate keys.</strong></p>
<p>Tested with Microsoft Access 2007.</p>
<h3>Example of the problem</h3>
<p>Here an example of the problem: you have a STUDENT table and a COLLEGE table, where a student has only one college, and a college can have multiple students. (A one-to-many relation.)</p>
<p><img class="aligncenter size-full wp-image-15" title="relationship" src="http://www.jtict.com/blog/wp-content/uploads/2008/07/relationship.png" alt="" width="441" height="149" /></p>
<p><img class="aligncenter size-full wp-image-14" title="college-data" src="http://www.jtict.com/blog/wp-content/uploads/2008/07/college-data.png" alt="" width="314" height="122" /></p>
<p><img class="aligncenter size-full wp-image-21" title="student-data" src="http://www.jtict.com/blog/wp-content/uploads/2008/07/student-data.png" alt="" width="309" height="125" /></p>
<p>Now when you want to select/assign a college to a student, you don&#8217;t want to select it using <strong>NON-informative</strong> college_id&#8217;s like this:</p>
<p><img class="aligncenter size-full wp-image-16" title="select-college-id" src="http://www.jtict.com/blog/wp-content/uploads/2008/07/select-college-id.png" alt="" width="329" height="146" /></p>
<p>instead you want select college_id&#8217;s using <strong>informative</strong> college names like :</p>
<p><img class="aligncenter size-full wp-image-17" title="select-college-id-informative" src="http://www.jtict.com/blog/wp-content/uploads/2008/07/select-college-id-informative.png" alt="" width="415" height="137" /></p>
<p>And after after selecting the college_id, you want the informative college fields to stay visible in the list, like this:<br />
<img class="aligncenter size-full wp-image-19" title="show-college-id-informative" src="http://www.jtict.com/blog/wp-content/uploads/2008/07/show-college-id-informative.png" alt="" width="423" height="159" /></p>
<p>while at the same time having a transparant relationship using the numbered surrogate id&#8217;s as foreign keys.</p>
<h3>Composed of two subproblems</h3>
<p>The problem consists of two sub problems:</p>
<ol>
<li>selecting non-informative foreign key id&#8217;s transparently using informative fields.</li>
<li>showing (single or multiple) informative fields in the list instead of a non-informative foreign id key</li>
</ol>
<h3>Solution using Lookup Wizard</h3>
<p>The easiest way to accomplish this is to use the &#8220;Lookup Wizard&#8221;. This wizard creates a (new) relationship between the 2 tables, so you have to make sure that a relation doesn&#8217;t exist yet.<br />
In this case, college_id will be the foreign key and thus the lookup column.</p>
<p>There are two ways to access the Lookup Wizard:</p>
<ol>
<li>via the &#8220;Lookup Column&#8221; button. This creates a new column, so college_id shoudn&#8217;t exist yet. Or</li>
<li>by selecting &#8220;Lookup Wizard&#8221; as &#8220;Data Type&#8221; for college_id.</li>
</ol>
<p>The wizard is self-explanatory. When selecting the column in this wizard, make sure you select BOTH the id, AND the columns that you want to be visible. You also have an option to hide the ID, which can be enabled if you prefer.</p>
<p>After finishing the wizard, you&#8217;ll see that you can now use informative fields instead of non-informative id&#8217;s, while transparently maintaining a foreign key relation using the non-informative id&#8217;s.</p>
<h3>Manual solution</h3>
<p>If you don&#8217;t to use the Lookup wizards, you can do it manually yourself in the field properties of college_id:</p>
<ol>
<li>change the &#8220;Display Control&#8221; to &#8220;List Box&#8221; or &#8220;Combo Box&#8221;</li>
<li>set &#8220;Row Source Type&#8221; to &#8220;Table/Query&#8221;</li>
<li>set &#8220;Row Source&#8221; to a query that contain BOTH the foreign key (college_id) AND the data that you want to be visible as value (cname in this case). The id column MUST be the first column, even if you don&#8217;t want it to be visible.</li>
<li>set &#8220;Column Count&#8221; to the number of columns of the query from step 3</li>
<li>set &#8220;Column Widths&#8221; for the columns. The ugly part: it is important that you set 0 as width for the id, so that it will be skipped and not be visible in the row, and thus the second column will be visible, which is our informative column.</li>
</ol>
<h3>Multiple informative field in the list</h3>
<p>The text above only partially solves subproblem 2; it shows ONLY 1 informative field, even when you selected multiple informative field in the wizard.<br />
For example, it shows</p>
<p><img class="aligncenter size-full wp-image-18" title="select-multiple" src="http://www.jtict.com/blog/wp-content/uploads/2008/07/select-multiple.png" alt="" width="403" height="151" /></p>
<p>where only &#8220;MyFirstCollege&#8221; is visible instead of also the value &#8220;Rotterdam&#8221;. Instead we want:</p>
<p><img class="aligncenter size-full wp-image-20" title="show-multiple" src="http://www.jtict.com/blog/wp-content/uploads/2008/07/show-multiple.png" alt="" width="464" height="166" /></p>
<p>where the &#8220;location&#8221; field of the COLLEGE table is also visible.</p>
<p>A manual solution to show multiple informative field in a row (like the last picture), is to concatenate the fields. This can be done by going to the field properties of college_id in the STUDENT table, and alter the SQL query:<br />
SELECT COLLEGE.ID, COLLEGE.cname, COLLEGE.location FROM COLLEGE;<br />
to<br />
SELECT COLLEGE.ID, COLLEGE.cname &amp; &#8216;-&#8217; &amp; COLLEGE.location FROM COLLEGE;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jtict.com/blog/access-informative-ids/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Grails &#038; Jetty with multiple contexts</title>
		<link>http://www.jtict.com/blog/grails-jetty-multiple-contexts/</link>
		<comments>http://www.jtict.com/blog/grails-jetty-multiple-contexts/#comments</comments>
		<pubDate>Sat, 12 Jul 2008 18:39:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[grails]]></category>

		<guid isPermaLink="false">http://www.jtict.com/blog/?p=12</guid>
		<description><![CDATA[A webapp usually contains both dynamic resources (scripts, config files, html files) and static resources (jpg, zip files, data etc). These resources can be deployed all together as one package, but when you have a lot of static resources, you usually want them to be seperate from the dynamic resources. Reasons might be that you [...]]]></description>
			<content:encoded><![CDATA[<p><strong>A webapp usually contains both dynamic resources (scripts, config files, html files) and static resources (jpg, zip files, data etc). These resources can be deployed all together as one package, but when you have a lot of static resources, you usually want them to be seperate from the dynamic resources. Reasons might be that you have that much static resources that it will slow down deployment or testing when packaged as a single unit, or you might want static resources to be served by a seperate service to improve performance, or you might want them to be in seperate filesytem hierarchy because the static resources might also be needed outside the webapp. In this post I describe two methods to solve the problem of seperating dynamic from static resources when using Grails and Jetty.</strong></p>
<p>The methods are tested using:</p>
<ul>
<li>Grails 1.0.3</li>
<li>Jetty 6.1.4 (default Jetty for Grails 1.0.3)</li>
<li>Static Resources 0.5 plugin</li>
</ul>
<h3>Method 1: Static Resources plugin &amp; symbolic links</h3>
<p>The Grails <a href="http://grails.org/Static+Resources+Plugin">Static-Resources</a> plugin is designed to separate dynamic from static resources.</p>
<p>Install:</p>
<ol>
<li>open a cmd/shell prompt and go into the directory of your Grails webapp project. I will call this directory &lt;myapp-grails-project-base&gt;.</li>
<li>type: grails install-plugin static-resources<br />
The plugin will now be installed for the current webapp only.</li>
</ol>
<p>I&#8217;ll show by example how the Static-Resources plugin works. Suppose you have a website called example.com and a webapp called myapp, then the webapp is available at <em>http://example.com:8080/myapp/</em>. When you have the plugin installed, everything under the <em>/static/</em> (filesystem directory) will be available under <em>http://example.com:8088/</em> when run in dev environment. Notice that port 8088 is used instead of port 8080. The plugin does this by starting a <a href="http://elonen.iki.fi/code/nanohttpd/">NanoHTTPD</a>, which is a lightweight single class Java HTTP server. The plugin calls this the <em>resource server</em>.<br />
When you go to <em>http://example.com:8088/</em>, you will get a directory listing of all the files located in the <em>static</em> directory, so you can start using them and linking to them, but there is a better way (read on).</p>
<p>If you put a pdf file at <em>&lt;myapp-grails-project-base&gt;/static/archive/catalog2005.pdf</em> (filesystem) then it will be available automatically and directly at <em>http://example.com:8088/archive/catalog2005.pdf</em> in a dev env.<br />
You can hardcode links to <em>http://example.com:8080/archive/catalog2005.pdf</em> manually in the view, but you can also use the plugins dedicated <em>resourceLinkTo</em> tag for this. For the pdf link, this tag is used as: <em>&lt;g:resourceLinkTo dir=&#8221;archive&#8221; file=&#8221;catalog2005.pdf&#8221;/&gt;</em></p>
<p>When running in prod env instead of dev, the resourceLinkTo tag will link to the URI <em>/resources/archive/catalog2005.pdf</em> instead of <em>http://example.com:8080/archive/catalog2005.pdf</em>. And this time it won&#8217;t start a NanoHTTPD server, it only changes the link. So in a prod env, you have to map <em>/resources/</em> to <em>&lt;myapp-grails-project-base&gt;/static/</em> yourself, in whatever production HTTP server you are using. You could do this in Tomcat 6 by putting something like <em>&lt;Context docBase=&#8221; &lt;myapp-grails-project-base&gt;/static/&#8221; path=&#8221;/resources&#8221;/&gt;</em> in the host element of server.xml, or better, in context.xml.</p>
<p>If you don&#8217;t want your resources to be located in the &lt;myapp-grails-project-base&gt;/static/ directory, you can use symbolic links in the static directory, and map them to elsewhere. Note that symbolic links are not only available on POSIX systems (Linux etc), but <a href="http://en.wikipedia.org/wiki/Symbolic_link">also on Windows systems</a> using special tools. (I&#8217;m not talking about Windows Shortcuts, but Windows symbolic links, hard links or junctions.)</p>
<p>Another way is to change the <em>static</em> directory location by modifying <em>&lt;myapp-grails-project-base&gt;\plugins\static-resources-0.5\grails-app\services\ExternalResourceServerService.groovy</em>.</p>
<h3>Method 2: a second Jetty webapp context</h3>
<p>The static resources method (described above) doesn&#8217;t have a complete built-in solution for a production environment; you have to make/map the <em>/resources/</em> URI yourself.</p>
<p>If want you to use Jetty in both dev and prod env, or if you want to have a separate static resources directory without using the Static-Resources plugin, then read on:</p>
<p>1) Open %GRAILS_HOME%\scripts\RunApp.groovy<br />
2) change</p>
<pre class="syntax-highlight:java">
    target( configureHttpServer : &amp;quot;Returns a jetty server configured with an HTTP connector&amp;quot;) {
    def server = new Server()
    grailsServer = server
    def connectors = [new SelectChannelConnector()]
    connectors[0].setPort(serverPort)
    server.setConnectors( (Connector [])connectors )
    setupWebContext()
    server.setHandler( webContext )
    event(&amp;quot;ConfigureJetty&amp;quot;, [server])
    return server
}
</pre>
<p>to</p>
<pre class="syntax-highlight:java">
    target( configureHttpServer : &amp;quot;Returns a jetty server configured with an HTTP connector&amp;quot;) {
    def server = new Server()
    grailsServer = server
    def connectors = [new SelectChannelConnector()]
    connectors[0].setPort(serverPort)
    server.setConnectors( (Connector [])connectors )
    setupWebContext()

    server.addHandler( webContext )
    WebAppContext resourceContext = new WebAppContext(&amp;quot;D:/myapp-grails-project-base/static&amp;quot;, &amp;quot;/resources&amp;quot;)
    server.addHandler(resourceContext);

    event(&amp;quot;ConfigureJetty&amp;quot;, [server])
    return server
}
</pre>
<p>Now, everything under <em>D:/myapp-grails-project-base/static</em> will be mapped to <em>http://&#8230;../resources</em>, in both dev and prod evironment, and will be served seperate from the webapp.</p>
<p>A drawback of placing this in RunApp.groovy is that every Grails app will have a <em>/resources</em> context running next to it.</p>
<p>Note that Jetty 5 and earlier versions of Grails probably needs different code.</p>
<p>Edit:<br />
Grails maintains a temperary directory for scripts in %userhome%\.grails\1.0.3\scriptCache. I you find that modifications aren&#8217;t visible, then try deleting that directory. </p>
<p>More info:</p>
<ul>
<li><a href="http://www.mortbay.org/jetty-6/apidocs/org/mortbay/jetty/webapp/WebAppContext.html">http://www.mortbay.org/jetty-6/apidocs/org/mortbay/jetty/webapp/WebAppContext.html</a></li>
<li><a href="http://jetty.mortbay.org/jetty5/tut/Server.html">http://jetty.mortbay.org/jetty5/tut/Server.html</a></li>
<li><a href="http://docs.codehaus.org/display/JETTY/Embedding+Jetty">http://docs.codehaus.org/display/JETTY/Embedding+Jetty</a></li>
<li><a href="http://www.mortbay.org/jetty-6/apidocs/org/mortbay/jetty/Server.html">http://www.mortbay.org/jetty-6/apidocs/org/mortbay/jetty/Server.html</a></li>
<li><a href="http://www.nabble.com/Jetty-and-virtual-host-td7505119.html#a7505119">http://www.nabble.com/Jetty-and-virtual-host-td7505119.html#a7505119</a></li>
<li><a href="http://grails.org/Static+Resources+Plugin">http://grails.org/Static+Resources+Plugin</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.jtict.com/blog/grails-jetty-multiple-contexts/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Struts2 table annoyance</title>
		<link>http://www.jtict.com/blog/struts2-table-annoyance/</link>
		<comments>http://www.jtict.com/blog/struts2-table-annoyance/#comments</comments>
		<pubDate>Sat, 05 Jul 2008 12:24:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Web programming]]></category>

		<category><![CDATA[java]]></category>

		<category><![CDATA[struts2]]></category>

		<guid isPermaLink="false">http://www.jtict.com/blog/?p=11</guid>
		<description><![CDATA[I was trying out a bit of Struts 2 just to test it out. An annoyance that quickly came up is that the HTML layout of forms are all table based. For example, if you want an input field for your name, then a column-based HTML table will be created automatically for that. The first [...]]]></description>
			<content:encoded><![CDATA[<p>I was trying out a bit of Struts 2 just to test it out. An annoyance that quickly came up is that the HTML layout of forms are all table based. For example, if you want an input field for your name, then a column-based HTML table will be created automatically for that. The first column contains a HTML label, and the second the input field.</p>
<p>For example, the following Struts2 code:</p>
<pre class="syntax-highlight:xml">&lt;s:form action=&quot;HelloWorld&quot;&gt;
    		&lt;s:textfield name=&quot;name&quot; label=&quot;Your name&quot;/&gt;
    		&lt;s:submit/&gt;
		&lt;/s:form&gt;</pre>
<p>will yield this HTML code:</p>
<pre class="syntax-highlight:xml">&lt;form id=&quot;HelloWorld&quot; name=&quot;HelloWorld&quot; onsubmit=&quot;return true;&quot; action=&quot;/StrutsInAction/chapterTwo/HelloWorld.action&quot; method=&quot;post&quot;&gt;
	&lt;table class=&quot;wwFormTable&quot;&gt;
		&lt;tr&gt;
			&lt;td class=&quot;tdLabel&quot;&gt;&lt;label for=&quot;HelloWorld_name&quot; class=&quot;label&quot;&gt;Your name:&lt;/label&gt;&lt;/td&gt;
			&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;name&quot; value=&quot;&quot; id=&quot;HelloWorld_name&quot;/&gt;&lt;/td&gt;
		&lt;/tr&gt;
   		&lt;tr&gt;
			&lt;td colspan=&quot;2&quot;&gt;&lt;div align=&quot;right&quot;&gt;&lt;input type=&quot;submit&quot; id=&quot;HelloWorld_0&quot; value=&quot;Submit&quot;/&gt;&lt;/div&gt;&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;
&lt;/form&gt;
</pre>
<p>What if you want to work without HTML tables? Or what if you want to put a label on the first row and its input field on the second row, instead of using two columns? You can by using the &#8220;<em>simple</em>&#8221; theme instead of the default &#8220;<em>xhtml</em>&#8221; theme. Set it using:</p>
<pre class="syntax-highlight:xml">&lt;s:form action=&quot;HelloWorld&quot; theme=&quot;simple&quot;&gt;</pre>
<p>The generated HTML code will now be:</p>
<pre class="syntax-highlight:xml">&lt;form id=&quot;HelloWorld&quot; name=&quot;HelloWorld&quot; onsubmit=&quot;return true;&quot; action=&quot;/StrutsInAction/chapterTwo/HelloWorld.action&quot; method=&quot;post&quot;&gt;
	&lt;input type=&quot;text&quot; name=&quot;name&quot; value=&quot;&quot; id=&quot;HelloWorld_name&quot;/&gt;
	&lt;input type=&quot;submit&quot; id=&quot;HelloWorld_0&quot; value=&quot;Submit&quot;/&gt;
&lt;/form&gt;</pre>
<p>But&#8230;. using the simple theme, will drop validation, error reporting, ajax etc&#8230;&#8230;</p>
<p>So if you don&#8217;t want those column based HTML tables but do want the default framework functionality (validation etc), you will have to make your own themes&#8230;&#8230;</p>
<p>JSF doesn&#8217;t have this problem, there you could just write something like:</p>
<pre class="syntax-highlight:xml">&lt;h:form&gt;
	&lt;h:outputLabel for=&quot;name&quot;&gt;Your name:&lt;/h:outputLabel&gt;
	&lt;h:inputText value=&quot;#{formAction.name}&quot;/&gt;
</pre>
<p>This is a bit more coding then using Struts 2, but it gives you the flexibility to place the outputLabel wherever you want, instead of being locked to two HTML columns.</p>
<p>More info:<br />
<a href="http://struts.apache.org/2.x/docs/themes-and-templates.html">http://struts.apache.org/2.x/docs/themes-and-templates.html</a><br />
<a href="http://www.vitarara.org/cms/struts_2_cookbook/creating_a_theme">http://www.vitarara.org/cms/struts_2_cookbook/creating_a_theme</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.jtict.com/blog/struts2-table-annoyance/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Seam-gen and default ROOT context</title>
		<link>http://www.jtict.com/blog/seam-gen-context-root/</link>
		<comments>http://www.jtict.com/blog/seam-gen-context-root/#comments</comments>
		<pubDate>Mon, 30 Jun 2008 14:04:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Web programming]]></category>

		<category><![CDATA[seam]]></category>

		<guid isPermaLink="false">http://www.jtict.com/blog/?p=10</guid>
		<description><![CDATA[If you are using seam-gen to deploy webapps as war-files to a webserver (for example JBoss AS), the root url of your webapp will be something like http://example.com/myproject/. (Where &#8220;myproject&#8221; is the name that you gave to your webapps when setting it up using seam-gen.) If you want your webapp to be assigned to the root [...]]]></description>
			<content:encoded><![CDATA[<p>If you are using seam-gen to deploy webapps as <strong>war</strong>-files to a webserver (for example JBoss AS), the root url of your webapp will be something like <a href="http://example.com/myproject/">http://example.com/myproject/</a>. (Where &#8220;myproject&#8221; is the name that you gave to your webapps when setting it up using seam-gen.) If you want your webapp to be assigned to the root (<a href="http://example.com/">http://example.com/</a>) (also known as the <strong>default ROOT context</strong>), then read on.</p>
<p>To change the root context, I perform the following steps. Note, there might be a better way, but this one works for me. If you&#8217;re using ejb instead of war, then you probably have to do some additional steps.</p>
<p>Tested using jboss-4.2.2.GA and jboss-seam-2.0.2.SP1.</p>
<p>1) After creating the project, open build.xml. Change</p>
<pre>&lt;property name="war.dir" value="exploded-archives/${project.name}.war" /&gt;</pre>
<p>to</p>
<pre>&lt;property name="war.dir" value="exploded-archives/ROOT.war" /&gt;</pre>
<p>and change</p>
<pre>&lt;property name="war.deploy.dir" value="${deploy.dir}/${project.name}.war" /&gt;</pre>
<p>to</p>
<pre>&lt;property name="war.deploy.dir" value="${deploy.dir}/ROOT.war" /&gt;</pre>
<p>2) Go to the following directory: {your.jboss.home.dir}\server\default\deploy\jboss-web.deployer. And rename ROOT.war to something else. The JBoss manager might then not be available anymore.</p>
<p>3) Now run an explode, and your webapp will be at the ROOT context.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jtict.com/blog/seam-gen-context-root/feed/</wfw:commentRss>
		</item>
		<item>
		<title>JSF doesn&#8217;t validate empty forms</title>
		<link>http://www.jtict.com/blog/jsf-empty-form-validation/</link>
		<comments>http://www.jtict.com/blog/jsf-empty-form-validation/#comments</comments>
		<pubDate>Thu, 19 Jun 2008 19:38:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Web programming]]></category>

		<category><![CDATA[jsf]]></category>

		<category><![CDATA[seam]]></category>

		<guid isPermaLink="false">http://www.jtict.com/blog/?p=7</guid>
		<description><![CDATA[Error:
Caused by org.hibernate.validator.InvalidStateException with message: &#8220;validation failed for: MyHibernateEntity&#8221;
Are you getting this error while trying to validate a form with empty fields using Seam, JSF and Hibernate? Then read on.
Seam allows you to define validation conditions on a single place: on the Hibernate / JPA entities. And then use this condition in the view without [...]]]></description>
			<content:encoded><![CDATA[<p>Error:</p>
<blockquote><p>Caused by org.hibernate.validator.InvalidStateException with message: &#8220;validation failed for: MyHibernateEntity&#8221;</p></blockquote>
<p>Are you getting this error while trying to validate a form with empty fields using Seam, JSF and Hibernate? Then read on.</p>
<p>Seam allows you to define validation conditions on a single place: on the Hibernate / JPA entities. And then use this condition in the view without duplicating them.<br />
Sounds nice, but when you try it in practice, you can easily run into the &#8220;<em>validation failed</em>&#8221; error above.</p>
<p>The problem is caused by JSF. It sounds idiot (and it is), but JSF validators aren&#8217;t invoked on empty fields. So the following code already causes the error when submitting an empty comment field</p>
<pre class="syntax-highlight:xml">&lt;h:inputTextarea id=&quot;comment&quot; value=&quot;#{user.comment}&quot; cols=&quot;10&quot; rows=&quot;3&quot;&gt;
   &lt;s:validate/&gt;
&lt;/h:inputTextarea&gt;
&lt;h:message for=&quot;comment&quot; /&gt;</pre>
<pre class="syntax-highlight:java">
@Entity
public class User {
  // ....

  @NotNull
  private String comment;

  // ....
}</pre>
<p>Fortunately a solution is simple, set the <em>required</em> attribute of the input field element to <em>true</em>. Like this:</p>
<pre class="syntax-highlight:xml">&lt;h:inputTextarea id=&quot;comment&quot; value=&quot;#{user.comment}&quot; cols=&quot;10&quot; rows=&quot;3&quot; required=&quot;true&quot;&gt;</pre>
<p>More info:<br />
<a href="http://jamiemcilroy.wordpress.com/2006/10/10/not-quite-what-i-expectedjsf-validation/">http://jamiemcilroy.wordpress.com/2006/10/10/not-quite-what-i-expectedjsf-validation/</a><br />
<a href="http://forum.java.sun.com/thread.jspa?threadID=5055877">http://forum.java.sun.com/thread.jspa?threadID=5055877</a><br />
<a href="http://www.jboss.com/index.html?module=bb&#038;op=viewtopic&#038;t=127793">http://www.jboss.com/index.html?module=bb&#038;op=viewtopic&#038;t=127793</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.jtict.com/blog/jsf-empty-form-validation/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Seam and URL rewriting with parameters and forms</title>
		<link>http://www.jtict.com/blog/seam-form-url-rewriting/</link>
		<comments>http://www.jtict.com/blog/seam-form-url-rewriting/#comments</comments>
		<pubDate>Mon, 16 Jun 2008 12:27:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Web programming]]></category>

		<category><![CDATA[jsf]]></category>

		<category><![CDATA[seam]]></category>

		<category><![CDATA[url rewriting]]></category>

		<guid isPermaLink="false">http://www.jtict.com/blog/?p=4</guid>
		<description><![CDATA[Making pretty bookmarkable RESTful URLs in JSF is hard. Fortunately the Seam framework solves this problem by supporting HTTP parameters, advanced page navigation, and can use the help of UrlRewriteFilter. But making bookmarkable parameter URLs in combination with a POST form can still be a bit tricky. This blog post shows an example how to solve this [...]]]></description>
			<content:encoded><![CDATA[<p>Making pretty bookmarkable RESTful URLs in JSF is hard. Fortunately the Seam framework solves this problem by supporting HTTP parameters, advanced page navigation, and can use the help of <a title="UrlRewriteFilter" href="http://tuckey.org/urlrewrite/" target="_blank">UrlRewriteFilter</a>. But making bookmarkable parameter URLs in combination with a POST form can still be a bit tricky. This blog post shows an example how to solve this problem.</p>
<p>Note: The code snippets used are only fragments. I also assume that UrlRewriteFilter is already installed etc. Seam-gen also contains UrlRewriteFilter, but you might have to add it to the deployment and configure web.xml. Use <span style="text-decoration: underline;">/rewrite-status</span> to see if it&#8217;s working.</p>
<h3>Example</h3>
<p>We have a website that shows products of a given catagory. In our case, we use the catagory &#8216;audio&#8217;. We want bookmarkable URLs, like <span style="text-decoration: underline;">http://example.com/products.jsf?catagory=audio</span> instead of <span style="text-decoration: underline;">http://example.com/products.jsf</span>. And we want pretty URLs that hide technology implementation and parameter names, so in the end we want only URLs like <span style="text-decoration: underline;">http://example.com/products/audio</span>.</p>
<p>Also we want a HTML POST form on the page that allows us to add products to a catagory (&#8217;audio&#8217; in this case). After submitting this form, we want the URLs to still be pretty, so also here we only want URLs like <span style="text-decoration: underline;">/products/audio</span>.</p>
<h4>Making bookmarkable URLs for first HTTP requests</h4>
<p>A URL is bookmarkable when the URL by itself contains all parameters needed to retrieve the page, like<br />
<span style="text-decoration: underline;">/products.jsf?catagory=audio</span>.</p>
<p>JSF is unable to do this because it cannot handle URL parameters. JSF depends on a HTTP POST (the postback request) to retrieve the page, so the user first has to click a button, after which JSF will show the page with audio products.<br />
The Seam framework solves this problem by handling URL parameters, so we use Seam. (This is just one reason to use Seam around JSF. There are many more.)</p>
<p>Because we are using Seam around JSF, the URL becomes <span style="text-decoration: underline;">/products.<strong>seam</strong>?catagory=audio</span>.<br />
To make these parameters, we add the following to pages.xml:</p>
<pre class="syntax-highlight:xml">&lt;page view-id=&quot;/products.seam&quot;&gt;
    &lt;param name=&quot;catagory&quot; value=&quot;#{productsManager.catagory}&quot; /&gt;
&lt;/page&gt;</pre>
<p>In the code above, productsManager is a backingbean and catagory is a property that will be set to &#8216;audio&#8217; automatically when the url <span style="text-decoration: underline;">/products.<strong>seam</strong>?catagory=audio</span> is requested.</p>
<h4>Making pretty URLs for first HTTP requests</h4>
<p>After enabling parameters for our URLs, we want them to be pretty. We want <span style="text-decoration: underline;">/products/audio</span> instead of <span style="text-decoration: underline;">/products.seam?catagory=audio</span>.<br />
This is done by using UrlRewriteFilter. We add the following code to urlrewrite.xml:</p>
<pre class="syntax-highlight:xml">&lt;rule&gt;
    &lt;from&gt;/products/(.*)&lt;/from&gt;
    &lt;to&gt;/products.seam?catagory=$1&lt;/to&gt;
&lt;/rule&gt;</pre>
<h4>Bookmarkable URLs after a form submit</h4>
<p>By default, with the code used above, when we use the form at <span style="text-decoration: underline;">/products/audio</span> to add a product the catagory &#8216;audio&#8217;, the form will send us to the URL <span style="text-decoration: underline;">/products.seam</span> without parameters, which is not bookmarkable! To make it bookmarkable, change the code that we have added to pages.xml to:</p>
<pre class="syntax-highlight:xml">&lt;page view-id=&quot;/products.seam&quot;&gt;
    &lt;param name=&quot;catagory&quot; value=&quot;#{productsManager.catagory}&quot; /&gt;
    &lt;navigation from-action=&quot;#{productsManager.addProduct}&quot;&gt;
        &lt;redirect view-id=&quot;/products.seam&quot;/&gt;
    &lt;/navigation&gt;
&lt;/page&gt;</pre>
<p>Where addProduct is an action method bound to the h:commandButton of the form.</p>
<p>When the form is now submitted, the user will be redirected to <span style="text-decoration: underline;">/products.seam?catagory=audio&amp;cid=1</span>, which is bookmarkable.<br />
Notice that a cid parameter is added by Seam, which Seam uses to track conversations. (To remove it, read further.)</p>
<h4>Pretty URLs after a form submit</h4>
<p>After submitting the form, we have <span style="text-decoration: underline;">/products.seam?catagory=audio&amp;cid=1</span>. To make it pretty again (<span style="text-decoration: underline;">/products/audio</span>), we use outbound urlrewriting. Add the following code to urlrewrite.xml:</p>
<pre class="syntax-highlight:xml">&lt;outbound-rule&gt;
    &lt;from&gt;^/products.seam\?catagory=(.*)&lt;/from&gt;
    &lt;to&gt;/products/$1&lt;/to&gt;
&lt;/outbound-rule&gt;</pre>
<p>After a form submit, the URL will now be: <span style="text-decoration: underline;">/products/audio?cid=1</span>. The URL is now prettier, but not completely, we still have the cid parameter. If you don&#8217;t need to maintain conversations, the cid parameter can be removed with by changing the outbound rule to:</p>
<pre class="syntax-highlight:xml">&lt;outbound-rule&gt;
    &lt;from&gt;^/products.seam\?catagory=(\w*)&amp;amp;cid=\d*&lt;/from&gt;
    &lt;to&gt;/products/$1&lt;/to&gt;
&lt;/outbound-rule&gt;</pre>
<h4>Disadvantage of the solution</h4>
<p>1) Extra delay:<br />
If you would make a HTML form without JSF, you could immediately specify the action URL (attribute of the HTML form), so you could immediately specify to URL you want to send form data to, without all the rewriting. When the browser then submits the POST data (immediately to the pretty bookmarkable URL), the server will immediately return the page with results. So only one HTTP request/response is needed. But with Seam/JSF two request/reponses are needed if you want pretty bookmarkable URLs, which is an extra unwanted delay, just to get the job done in these frameworks.</p>
<p>The following steps happen in the background in our example:</p>
<ol>
<li>The user enters <span style="text-decoration: underline;">/products/audio</span> in the browser.</li>
<li>The server responds with the page containing a HTML form like: <code>&lt;form method="post" action="/products.seam"&gt;</code>. Notice that it refers to <span style="text-decoration: underline;">/products.seam</span> without parameters.</li>
<li>The user enters form data and submits it. The browser will generate a HTTP POST request to <span style="text-decoration: underline;">/products.seam</span>.</li>
<li>The server processes the form data using productsManager.addProduct(), and responds with a HTTP REDIRECT to <span style="text-decoration: underline;">/products/audio</span>. (A HTTP redirect doesn&#8217;t contain a page.)</li>
<li>The browser receives the redirect, and immediately requests for <span style="text-decoration: underline;">/products/audio</span>, using a HTTP GET.</li>
<li>The server receives the HTTP GET and gives a HTTP OK reponse containing the page the user already was on.</li>
</ol>
<p>Without the Seam/JSF framework, these steps would be shorter and faster, saving an extra client-server-client roundtrip. Because you could then specify the URL to submit to in the HTML form, and thus won&#8217;t need the HTTP redirect. But these extra step can also be used as an advantage; it allow serversided navigation rules. But it would be nice if both options were possible, allowing the developer to choose.</p>
<p>2) SEO:<br />
Google recently announced that they are going to <a href="http://googlewebmastercentral.blogspot.com/2008/04/crawling-through-html-forms.html">crawl through HTML forms</a> to find URLs.<br />
Because the HTML form is using <span style="text-decoration: underline;">/products.seam</span> in the HTML code instead of the pretty bookmarkable url, Google might index URLs you don&#8217;t want to be indexed. (This can be stopped using robots.txt, but other crawler maybe might not use that file.) </p>
<p>3) Doesn&#8217;t work for validation:<br />
When a form is not validated because the form fields are not passing the specified field conditions, the JSF life cycle will end and thus skip the <em>Invoke Application</em> phase, and thus the navigation rules above will not be executed. I don&#8217;t know if there&#8217;s an easy solution for this.</p>
<h4>Notes:</h4>
<p>1)<br />
When using UrlRewriteFilter, make sure you encode XML entities, so a <em><span style="color: #3366ff;">&amp;</span></em> becomes <em><span style="color: #3366ff;">&amp;</span></em> in urls that have a querystring.</p>
<p>2)<br />
The querystring of an URL is everything behind the questionmark, like <span style="text-decoration: underline;">/products.seam?catagory=audio&amp;login=true</span>. In UrlRewriteFilter, like in mod_rewrite, querystrings are handled separatly from the URL. In UrlRewriteFilter you&#8217;ll have to use the &lt;condition&gt; element when using a &lt;rule&gt; element for an URL containing a querystring. For example:</p>
<pre class="syntax-highlight:xml">&lt;rule&gt;
&lt;condition type=&quot;query-string&quot;&gt;catagory=(\w*)&amp;amp;login=true&lt;/condition&gt;
&lt;from&gt;/products.seam&lt;/from&gt;
&lt;to type=&quot;redirect&quot;&gt;/products/%1&lt;/to&gt;
&lt;/rule&gt;</pre>
<p>Also note that you&#8217;ll have to use %1 instead of $1 when using conditions.<br />
You don&#8217;t have to use the &lt;condition&gt; element when using querystrings with the &lt;outbound-rule&gt; element.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jtict.com/blog/seam-form-url-rewriting/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
