tag:blogger.com,1999:blog-183133362008-05-16T09:02:34.769-04:00Abhi On JavaAbhihttp://www.blogger.com/profile/05788965755655733389noreply@blogger.comBlogger182125tag:blogger.com,1999:blog-18313336.post-82547204644276656652008-04-17T09:35:00.008-04:002008-04-17T10:03:25.858-04:00Integrating Spring and Hibernate: TransactionsIn the previous post, I described different ways in which spring and hibernate can<br />be integrated. In this post I will describe how to use Spring's transaction features<br />in hibernate. The following methods of transaction management with spring and hibernate are discussed.<ol><li>Declarative Transaction Mangement with AOP Interceptors</li><li>Schema-based Declarative Transaction Management</li><li>Schema-based Declarative Transaction Management with Annotations</li><li>Programmatic Transaction Management</li></ol><br />The easiest way to check if transactions are working for this example is to remove the transaction declarations for the getStockQuote() method. Removing transactions will cause the following exception<pre>org.hibernate.LazyInitializationException: could not initialize proxy - no Session</pre><br /><a href="http://java-x.blogspot.com/2008/04/integrating-spring-and-hibernate_17.html">There's More</a> ...<span class="fullpost"><br />For this example, start off with the following as described in the "<a href="http://java-x.blogspot.com/2008/04/integrating-spring-and-hibernate.html">Integrating Spring and Hibernate</a>" post.<br /><ol><li>The bean StockQuoteBean</li><li>The Portfolio classes : PortfolioDAO, PortfolioDAOSupport and PortfolioDAOTemplate</li><li>The hibernate mapping file stockquote.hbm.xml</li></ol><br />The following main class can be used:<pre>package springhibernate;<br /><br /><br />import org.springframework.context.ApplicationContext;<br />import org.springframework.context.support.ClassPathXmlApplicationContext;<br /><br /><br />public class SpringHibernateTest {<br /><br /><br />public static void main(String[] args) {<br /><span style="font-weight: bold;">ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");</span><br /><br /><span style="font-weight: bold;"> IPortfolioService portfolioService = (IPortfolioService) ctx.getBean("portfolioService");</span><br />System.out.println("Portfolio Service type : " + portfolioService.getClass());<br />portfolioService.getStockQuote("123");<br />}<br /><br /><br />}</pre><center>SpringHibernateTest.java</center><br />Note that here we use an Interface IPortfolioService, and also note the change to ApplicationContext instead of a BeanFactory.<br /><span style="font-style: italic;">The reason for using ApplicationContext is enable the use of AOP for declarative transaction management.</span> Also we are not expecting any return to the main class as all the execution is expected to happen in the transaction context.<br /><br /><span style="font-size:100%;"><span style="font-weight: bold;">The Portfolio Service Interface:</span></span><pre>package springhibernate;<br /><br />import beans.StockQuoteBean;<br /><br />public interface IPortfolioService {<br /><br />public void getStockQuote(String id);<br /><br />public void updateStockQuote(StockQuoteBean stockQuoteBean);<br /><br />}</pre><center>IPortfolioService.java</center><br /><div class="post-inner-heading">Declarative Transaction Management</div><br />Declarative transaction management in Spring has the advantage of being less invasive. There is no need for changing application code when using declarative transactions. All you have to do is to modify the application context.<br /><br /><span style="font-weight: bold;">The Service class</span> will be same for all the modes of Declarative transaction management described below.<pre>package springhibernate;<br /><br />import beans.StockQuoteBean;<br />import dao.PortfolioDAO;<br /><br /><br />public class PortfolioService implements IPortfolioService {<br />private PortfolioDAO portfolioDAO;<br /><br /><br /><br />public void getStockQuote(String id) {<br />StockQuoteBean result = portfolioDAO.getStockQuote(id);<br />System.out.println("Result in Service : " + result.getStockSymbol());<br /><br />}<br /><br />public void updateStockQuote(StockQuoteBean stockQuoteBean) {<br />portfolioDAO.updateStockQuote(stockQuoteBean);<br />}<br /><br />public PortfolioDAO getPortfolioDAO() {<br />return portfolioDAO;<br />}<br /><br />public void setPortfolioDAO(PortfolioDAO portfolioDAO) {<br />this.portfolioDAO = portfolioDAO;<br />System.out.println("Setting portfolio DAO to : " + portfolioDAO.getClass());<br />}<br /><br />}</pre><center>PortfolioService.java</center><br /><div class="post-inner-heading">Declarative Transaction Management with AOP Interceptor</div><br />In this method, you have to define a proxy for the bean that will be made transactional.<pre>&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />&lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br />xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"<br />xsi:schemaLocation="<br /> http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd<br /> http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd<br /> http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"&gt;<br /><br />&lt;bean id="portfolioDAOTemplate" class="dao.PortfolioDAOTemplate"&gt;<br />&lt;property name="hibernateTemplate" ref="hibernateTemplate" /&gt;<br />&lt;/bean&gt;<br /><br />&lt;bean id="portfolioDAOSupport" class="dao.PortfolioDAOSupport"&gt;<br />&lt;property name="hibernateTemplate" ref="hibernateTemplate" /&gt;<br />&lt;/bean&gt;<br /><br /><span style="font-weight: bold;"> &lt;bean id="myPortfolioService" class="springhibernate.PortfolioService"&gt;</span><br /><span style="font-weight: bold;"> &lt;property name="portfolioDAO" ref="portfolioDAOTemplate"&gt;&lt;/property&gt;</span><br /><span style="font-weight: bold;"> &lt;/bean&gt;</span><br /><br /><span style="font-weight: bold;"> &lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;</span><br /><span style="font-weight: bold;"> &lt;property name="sessionFactory" ref="sessionFactory" /&gt;</span><br /><span style="font-weight: bold;"> &lt;/bean&gt;</span><br /><br /><span style="font-weight: bold;"> &lt;bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"&gt;</span><br /><span style="font-weight: bold;"> &lt;property name="transactionManager"&gt;</span><br /><span style="font-weight: bold;"> &lt;ref bean="transactionManager" /&gt;</span><br /><span style="font-weight: bold;"> &lt;/property&gt;</span><br /><span style="font-weight: bold;"> &lt;property name="transactionAttributeSource"&gt;</span><br /><span style="font-weight: bold;"> &lt;value&gt;springhibernate.PortfolioService.*=PROPAGATION_REQUIRED&lt;/value&gt;</span><br /><span style="font-weight: bold;"> &lt;/property&gt;</span><br /><span style="font-weight: bold;"> &lt;/bean&gt;</span><br /><br /><span style="font-weight: bold;"> &lt;bean id="hibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor"&gt;</span><br /><span style="font-weight: bold;"> &lt;property name="sessionFactory" ref="sessionFactory" /&gt;</span><br /><span style="font-weight: bold;"> &lt;/bean&gt;</span><br /><br /><span style="font-weight: bold;"> &lt;bean id="portfolioService" class="org.springframework.aop.framework.ProxyFactoryBean"&gt;</span><br /><span style="font-weight: bold;"> &lt;property name="target" ref="myPortfolioService"&gt;&lt;/property&gt;</span><br /><span style="font-weight: bold;"> &lt;property name="interceptorNames"&gt;</span><br /><span style="font-weight: bold;"> &lt;list&gt;</span><br /><span style="font-weight: bold;"> &lt;value&gt;transactionInterceptor&lt;/value&gt;</span><br /><span style="font-weight: bold;"> &lt;value&gt;hibernateInterceptor&lt;/value&gt;</span><br /><span style="font-weight: bold;"> &lt;/list&gt;</span><br /><span style="font-weight: bold;"> &lt;/property&gt;</span><br /><span style="font-weight: bold;"> &lt;/bean&gt;</span><br /><br />&lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;<br />&lt;property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /&gt;<br />&lt;property name="url" value="jdbc:oracle:thin:@localhost:1521/xe" /&gt;<br />&lt;property name="username" value="appUser" /&gt;<br />&lt;property name="password" value="password" /&gt;<br />&lt;/bean&gt;<br />&lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;<br />&lt;property name="dataSource" ref="dataSource" /&gt;<br />&lt;property name="mappingResources"&gt;<br />&lt;list&gt;<br /> &lt;value&gt;stockquote.hbm.xml&lt;/value&gt;<br />&lt;/list&gt;<br />&lt;/property&gt;<br /><br />&lt;property name="hibernateProperties"&gt;<br />&lt;props&gt;<br /> &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.Oracle9Dialect&lt;/prop&gt;<br /> &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;<br /> &lt;prop key="hibernate.generate_statistics"&gt;true&lt;/prop&gt;<br />&lt;/props&gt;<br />&lt;/property&gt;<br />&lt;/bean&gt;<br />&lt;bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"&gt;<br />&lt;property name="sessionFactory" ref="sessionFactory" /&gt;<br />&lt;/bean&gt;<br /><br />&lt;/beans&gt;<br /></pre><center>applicationContext.xml</center><br />Note that the transactionInterceptor and hibernateTransactionInterceptor are declared. While transactionInterceptor is used to set the transaction properties, the hibernateTransactionInterceptor will manage the hibernate transactions.<br /><br /><div class="post-inner-heading">Schema-based Declarative Transaction Management</div><br /><pre>&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />&lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br />xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"<br />xsi:schemaLocation="<br /> http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd<br /> http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd<br /> http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"&gt;<br /><br />&lt;bean id="portfolioDAOTemplate" class="dao.PortfolioDAOTemplate"&gt;<br />&lt;property name="hibernateTemplate" ref="hibernateTemplate" /&gt;<br />&lt;/bean&gt;<br /><br />&lt;bean id="portfolioDAOSupport" class="dao.PortfolioDAOSupport"&gt;<br />&lt;property name="hibernateTemplate" ref="hibernateTemplate" /&gt;<br />&lt;/bean&gt;<br /><br /><span style="font-weight: bold;"> &lt;bean id="portfolioService" class="springhibernate.PortfolioService"&gt;</span><br /><span style="font-weight: bold;"> &lt;property name="portfolioDAO" ref="portfolioDAOTemplate"&gt;&lt;/property&gt;</span><br /><span style="font-weight: bold;"> &lt;/bean&gt;</span><br /><br /><span style="font-weight: bold;"> &lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;</span><br /><span style="font-weight: bold;"> &lt;property name="sessionFactory" ref="sessionFactory" /&gt;</span><br /><span style="font-weight: bold;"> &lt;/bean&gt;</span><br /><br /><span style="font-weight: bold;"> &lt;aop:config&gt;</span><br /><span style="font-weight: bold;"> &lt;aop:pointcut id="serviceMethods" expression="execution(* springhibernate.IPortfolioService.*(..))" /&gt;</span><br /><span style="font-weight: bold;"> &lt;aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" /&gt;</span><br /><span style="font-weight: bold;"> &lt;/aop:config&gt;</span><br /><span style="font-weight: bold;"> </span><br /><span style="font-weight: bold;"> &lt;tx:advice id="txAdvice" transaction-manager="transactionManager" &gt;</span><br /><span style="font-weight: bold;"> &lt;tx:attributes&gt;</span><br /><span style="font-weight: bold;"> &lt;tx:method name="*" propagation="REQUIRES_NEW" /&gt;</span><br /><span style="font-weight: bold;"> &lt;/tx:attributes&gt;</span><br /><span style="font-weight: bold;"> &lt;/tx:advice&gt;</span><br /><br /><br />&lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;<br />&lt;property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /&gt;<br />&lt;property name="url" value="jdbc:oracle:thin:@localhost:1521/xe" /&gt;<br />&lt;property name="username" value="appUser" /&gt;<br />&lt;property name="password" value="password" /&gt;<br />&lt;/bean&gt;<br />&lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;<br />&lt;property name="dataSource" ref="dataSource" /&gt;<br />&lt;property name="mappingResources"&gt;<br />&lt;list&gt;<br /> &lt;value&gt;stockquote.hbm.xml&lt;/value&gt;<br />&lt;/list&gt;<br />&lt;/property&gt;<br /><br />&lt;property name="hibernateProperties"&gt;<br />&lt;props&gt;<br /> &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.Oracle9Dialect&lt;/prop&gt;<br /> &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;<br /> &lt;prop key="hibernate.generate_statistics"&gt;true&lt;/prop&gt;<br />&lt;/props&gt;<br />&lt;/property&gt;<br />&lt;/bean&gt;<br />&lt;bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"&gt;<br />&lt;property name="sessionFactory" ref="sessionFactory" /&gt;<br />&lt;/bean&gt;<br /><br />&lt;/beans&gt;<br /></pre><center>applicationContext.xml</center>Using Schema-based declarative transaction management is a lot simpler and a lot cleaner. However it is <span style="font-style: italic;">adviced that this method not be used in conjunction with explicit auto-proxying using BeanNameAutoProxyCreator, as it might raise issues like advice not being woven etc. </span>Note that the AOP pointcut is defined to be all methods on the IPortfolioService inteface.<br /><div class="post-inner-heading"><br />Schema-based Declarative Transaction Management with Annotations </div><br /><pre>&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />&lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br />xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"<br />xsi:schemaLocation="<br /> http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd<br /> http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd<br /> http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"&gt;<br /><br />&lt;bean id="portfolioDAOTemplate" class="dao.PortfolioDAOTemplate"&gt;<br />&lt;property name="hibernateTemplate" ref="hibernateTemplate" /&gt;<br />&lt;/bean&gt;<br /><br />&lt;bean id="portfolioDAOSupport" class="dao.PortfolioDAOSupport"&gt;<br />&lt;property name="hibernateTemplate" ref="hibernateTemplate" /&gt;<br />&lt;/bean&gt;<br /><br />&lt;bean id="portfolioService" class="springhibernate.PortfolioService"&gt;<br />&lt;property name="portfolioDAO" ref="portfolioDAOTemplate"&gt;&lt;/property&gt;<br />&lt;/bean&gt;<br /><br />&lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;<br />&lt;property name="sessionFactory" ref="sessionFactory" /&gt;<br />&lt;/bean&gt;<br /><br /><span style="font-weight: bold;"> &lt;tx:annotation-driven/&gt;</span><br /><br />&lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;<br />&lt;property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /&gt;<br />&lt;property name="url" value="jdbc:oracle:thin:@localhost:1521/xe" /&gt;<br />&lt;property name="username" value="appUser" /&gt;<br />&lt;property name="password" value="password" /&gt;<br />&lt;/bean&gt;<br />&lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;<br />&lt;property name="dataSource" ref="dataSource" /&gt;<br />&lt;property name="mappingResources"&gt;<br />&lt;list&gt;<br /> &lt;value&gt;stockquote.hbm.xml&lt;/value&gt;<br />&lt;/list&gt;<br />&lt;/property&gt;<br /><br />&lt;property name="hibernateProperties"&gt;<br />&lt;props&gt;<br /> &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.Oracle9Dialect&lt;/prop&gt;<br /> &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;<br /> &lt;prop key="hibernate.generate_statistics"&gt;true&lt;/prop&gt;<br />&lt;/props&gt;<br />&lt;/property&gt;<br />&lt;/bean&gt;<br />&lt;bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"&gt;<br />&lt;property name="sessionFactory" ref="sessionFactory" /&gt;<br />&lt;/bean&gt;<br /><br />&lt;/beans&gt;</pre><center>applicationContext.xml</center>When using annotations, you only have to declare the transaction manager and add &lt;tx:annotation-driven/&gt; to the application context. If the transaction manager is named "transactionManager" then you don't have to declare a transaction-manager attribute for &lt;tx:annotation-driven/&gt; as it happens to be the default value for that attribute.<br />Also we have to declare the pointcuts in the Service class itself. Note that as annotations are not inherited, declaring the annotations has to be done at the class level.<pre style="font-weight: bold;">@Transactional<br />public class PortfolioService implements IPortfolioService {<br />private PortfolioDAO portfolioDAO;...</pre><br /><div class="post-inner-heading">Programmatic Transaction Management</div><br />For programmatic transaction management in spring, you will need a PlatformTransctionManger in your bean which will be used to create a TransactionTemplate. The TransactionTemplate is used in the same way the HibernateTemplate was used in previous example. Additionally you will have to create a HibernateTransactionManager as shown in the above example.<pre>package springhibernate;<br /><br />import org.springframework.transaction.PlatformTransactionManager;<br />import org.springframework.transaction.TransactionStatus;<br />import org.springframework.transaction.support.TransactionCallbackWithoutResult;<br />import org.springframework.transaction.support.TransactionTemplate;<br /><br />import beans.StockQuoteBean;<br />import dao.PortfolioDAO;<br /><br />public class PortfolioServiceTransaction implements IPortfolioService{<br />private PortfolioDAO portfolioDAO;<br /><br />private PlatformTransactionManager transactionManager;<br /><br />public void getStockQuote(final String id) {<br /><span style="font-weight: bold;"> TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);</span><br /><span style="font-weight: bold;"> transactionTemplate.execute(new TransactionCallbackWithoutResult() {</span><br /><br /><span style="font-weight: bold;"> public void doInTransactionWithoutResult(TransactionStatus status) {</span><br /><span style="font-weight: bold;"> StockQuoteBean result = portfolioDAO.getStockQuote(id);</span><br /><span style="font-weight: bold;"> System.out.println("Symbol in transaction " + result.getStockSymbol());</span><br /><span style="font-weight: bold;"> }</span><br /><span style="font-weight: bold;"> });</span><br /><br />}<br /><br /><span style="font-weight: bold;"> public void updateStockQuote(final StockQuoteBean stockQuoteBean) {</span><br /><span style="font-weight: bold;"> TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);</span><br /><span style="font-weight: bold;"> transactionTemplate.execute(new TransactionCallbackWithoutResult() {</span><br /><br /><span style="font-weight: bold;"> public void doInTransactionWithoutResult(TransactionStatus status) {</span><br /><span style="font-weight: bold;"> portfolioDAO.updateStockQuote(stockQuoteBean);</span><br /><span style="font-weight: bold;"> }</span><br /><span style="font-weight: bold;"> });</span><br /><span style="font-weight: bold;"> </span><br /><span style="font-weight: bold;"> }</span><br /><br />public PortfolioDAO getPortfolioDAO() {<br />return portfolioDAO;<br />}<br /><br />public void setTransactionManager(PlatformTransactionManager transactionManager) {<br />this.transactionManager = transactionManager;<br />}<br /><br />public void setPortfolioDAO(PortfolioDAO portfolioDAO) {<br />this.portfolioDAO = portfolioDAO;<br />System.out.println("Setting portfolio DAO to : " + portfolioDAO.getClass());<br />}<br /><br />}<br /></pre><center>PortfolioService.java</center></span>Abhihttp://www.blogger.com/profile/05788965755655733389noreply@blogger.comtag:blogger.com,1999:blog-18313336.post-80017263985149305472008-04-14T20:44:00.006-04:002008-04-16T13:26:22.194-04:00Integrating Spring and HibernateThis post applies to integrating<span style="font-weight: bold;"> Spring framework 2.5.3 and Hibernate 3.0</span>.<br /><br />The Spring framework provides extensive support for data access through the use of support classes (JdbcDaoSupport, JdbcTemplate etc.), and extensive exception hierarchy to wrap any platform specific SQLException into an exception in the spring exception hierarchy. Additionally Spring framework also provides good support for integrating with ORM technologies like Hibernate and iBatis etc. This post will show how to integrate Spring framework with Hibernate ORM. <a href="http://java-x.blogspot.com/2008/04/integrating-spring-and-hibernate.html">There's more ...</a><span class="fullpost"><ol><li><span style="font-weight: bold;">Create the bean</span>: The bean here represents a simple stock quote<pre>package beans;<br /><br />public class StockQuoteBean {<br />private String quoteId;<br /><br />private String stockSymbol;<br /><br />private String name;<br /><br />public String getQuoteId() {<br />return quoteId;<br />}<br /><br />public void setQuoteId(String quoteId) {<br />this.quoteId = quoteId;<br />}<br /><br />public String getStockSymbol() {<br />return stockSymbol;<br />}<br /><br />public void setStockSymbol(String stockSymbol) {<br />this.stockSymbol = stockSymbol;<br />}<br /><br />public String getName() {<br />return name;<br />}<br /><br />public void setName(String name) {<br />this.name = name;<br />}<br />}</pre><center>StockQuoteBean.java</center></li><li><span style="font-weight: bold;">Create a Hibernate Mapping</span> file for the bean: <pre>&lt;?xml version="1.0"?&gt;<br />&lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"<br />"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;<br />&lt;hibernate-mapping&gt;<br />&lt;class name="beans.StockQuoteBean" table="STOCK_QUOTES" <span style="font-weight: bold;">lazy="false"</span>&gt;<br />&lt;id name="quoteId" column="quote_id"&gt;<br /> &lt;generator class="assigned" /&gt;<br />&lt;/id&gt;<br /><br />&lt;property name="stockSymbol"&gt;<br /> &lt;column name="stock_symbol" /&gt;<br />&lt;/property&gt;<br />&lt;property name="name"&gt;<br /> &lt;column name="name" /&gt;<br />&lt;/property&gt;<br />&lt;/class&gt;<br />&lt;/hibernate-mapping&gt;</pre><center>stockquote.hbm.xml</center><br />The one important thing to note here is that in the declaration, a [<span style="font-weight: bold;">lazy="false"</span>] has been added to the mapping for the stockquote bean. The reason for this is that in hibernate 3, <span style="font-weight: bold;">lazy initialization is turned on by default</span>. This raises a problem when used with spring's HibernateCallback. The spring <span style="font-weight: bold;">HibernateTemplate.execute() by default closes any open sessions upon completion</span>. When used with lazy initialization you may get a <span style="font-weight: bold;">LazyInitializationException </span>like the following<pre style="font-weight: bold;">org.hibernate.LazyInitializationException: could not initialize proxy - no Session</pre>If you want to use lazy initialization with HibernateCallback, you will have to use this within a transaction context. The javadoc for HibernateTemplate specifies this explicitly<pre>Note that operations that return an Iterator (i.e. iterate) are supposed<br />to be used within Spring-driven or JTA-driven transactions (with<br />HibernateTransactionManager, JtaTransactionManager, or EJB CMT). Else, the<br />Iterator won't be able to read results from its ResultSet anymore, as the<br />underlying Hibernate Session will already have been closed.<br /><br />Lazy loading will also just work with an open Hibernate Session, either within a<br />transaction or within OpenSessionInViewFilter/Interceptor. Furthermore, some<br />operations just make sense within transactions, for example: contains, evict,<br />lock, flush, clear.</pre></li><li><span style="font-weight: bold;">The service class</span>: The service class simply acts as an intermediary between the client and the DAO classes.<pre>package springhibernate;<br /><br />import beans.StockQuoteBean;<br />import dao.PortfolioDAO;<br /><br />public class PortfolioService {<br />private PortfolioDAO portfolioDAO;<br /><br />public StockQuoteBean getStockQuote(String id) {<br />StockQuoteBean result = portfolioDAO.getStockQuote(id);<br />return result;<br />}<br /><br />public void updateStockQuote(StockQuoteBean stockQuoteBean) {<br />portfolioDAO.updateStockQuote(stockQuoteBean);<br />}<br /><br />public PortfolioDAO getPortfolioDAO() {<br />return portfolioDAO;<br />}<br /><br />public void setPortfolioDAO(PortfolioDAO portfolioDAO) {<br />this.portfolioDAO = portfolioDAO;<br />System.out.println("Setting portfolio DAO to : " + portfolioDAO.getClass());<br />}<br /><br />}</pre><center>PortfolioService.java</center></li><li>The DAO interface: <pre>package dao;<br /><br />import beans.StockQuoteBean;<br /><br />public interface PortfolioDAO {<br />public StockQuoteBean getStockQuote(String id);<br />public void updateStockQuote(StockQuoteBean bean);<br />public StockQuoteBean getStockQuote_hibernateTemplate(String id);<br />public void updateStockQuote_hibernateTemplate(StockQuoteBean bean);<br />}<br /></pre><center>PortfolioDAO.java</center></li><li><span style="font-weight: bold;">The DAO Classes</span>: The DAO classes shows the different ways in which the Hibernate calls can be made using the Spring support classes. There are three primary ways in which these calls can be made<ol><li>Using the HibernateCallback</li><li>Using the HibernateTemplate directly</li><li>Using the hibernate native calls using Session</li></ol>Spring also provides two different ways to create the Data access objects that interact with Hibernate.<ol><li>Using Composition, with <span style="font-weight: bold;">HibernateTemplate</span></li><li>Using Inheritance by extending <span style="font-weight: bold;">HibernateDaoSupport</span></li></ol>All these methods will be explained when used in the following sections.<ol><li><span style="font-weight: bold;">Using HibernateTemplate</span><pre>package dao;<br /><br />import java.sql.SQLException;<br />import java.util.List;<br /><br />import org.hibernate.HibernateException;<br />import org.hibernate.Session;<br />import org.springframework.orm.hibernate3.HibernateCallback;<br />import org.springframework.orm.hibernate3.HibernateTemplate;<br /><br />import beans.StockQuoteBean;<br /><br />public class PortfolioDAOTemplate implements PortfolioDAO{<br />private HibernateTemplate hibernateTemplate;<br /><br />public PortfolioDAOTemplate() {<br />System.out.println("Init transaction dao");<br />}<br /><br /><br />public StockQuoteBean getStockQuote(<span style="font-weight: bold;">final String id</span>) {<br /><br /><span style="font-weight: bold;">HibernateCallback callback = new HibernateCallback() {</span><br /><span style="font-weight: bold;"> public Object doInHibernate(Session session) throws HibernateException, SQLException {</span><br /><span style="font-weight: bold;"> return session.load(StockQuoteBean.class, id);</span><br /><span style="font-weight: bold;"> }</span><br /><span style="font-weight: bold;"> };</span><br />return (StockQuoteBean) hibernateTemplate.execute(callback);<br />}<br /><br /><span style="font-weight: bold;"> public void updateStockQuote(final StockQuoteBean StockQuoteBean) {</span><br /><span style="font-weight: bold;"> HibernateCallback callback = new HibernateCallback() {</span><br /><span style="font-weight: bold;"> public Object doInHibernate(Session session) throws HibernateException, SQLException {</span><br /><span style="font-weight: bold;"> session.saveOrUpdate(StockQuoteBean);</span><br /><span style="font-weight: bold;"> return null;</span><br /><span style="font-weight: bold;"> }</span><br /><span style="font-weight: bold;"> };</span><br /><span style="font-weight: bold;"> hibernateTemplate.execute(callback);</span><br /><span style="font-weight: bold;"> </span><br /><span style="font-weight: bold;"> }</span><br /><br /><span style="font-weight: bold;"> public void updateStockQuote_hibernateTemplate(StockQuoteBean StockQuoteBean) {</span><br /><span style="font-weight: bold;"> hibernateTemplate.update(StockQuoteBean);</span><br /><br /><span style="font-weight: bold;"> }</span><br /><span style="font-weight: bold;"> public StockQuoteBean getStockQuote_hibernateTemplate(String id) {</span><br /><span style="font-weight: bold;"> List&lt;StockQuoteBean&gt; transactions = hibernateTemplate.find("from beans.StockQuoteBean stockQuoteBean where stockQuoteBean.quoteId=?", id);</span><br /><span style="font-weight: bold;"> return transactions.get(0);</span><br /><span style="font-weight: bold;"> }</span><br /><br /><br />public HibernateTemplate getHibernateTemplate() {<br />return hibernateTemplate;<br />}<br /><br /><br />public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {<br />this.hibernateTemplate = hibernateTemplate;<br />}<br /><br /><br /><br />}</pre><center>PortfolioDAOTemplate</center><br />This class shows how to use the HibernateTemplate to make calls to Hibernate. The getStockQuote() and updateStockQuote() methods use HibernateCallback class, note that when using HibernateCallback, it is necessary to either do it in a transactional context or turn off lazy initialization. While the getStockQuote_hibernateTemplate() and updateStockQuote_hibernateTemplate() make calls using hibernateTemplate directly. Also note that the parameters to the getStockQuote(), and updateStockQuote() methods are marked final.<br /></li><li><span style="font-weight: bold;">Using HibernateDaoSupport</span><pre>package dao;<br /><br />import java.util.List;<br /><br />import org.hibernate.Query;<br />import org.springframework.orm.hibernate3.support.HibernateDaoSupport;<br /><br />import beans.StockQuoteBean;<br /><br /><span style="font-weight: bold;">public class PortfolioDAOSupport extends HibernateDaoSupport implements PortfolioDAO {</span><br /><br /><span style="font-weight: bold;">public void updateStockQuote(StockQuoteBean stockQuoteBean) {</span><br /><span style="font-weight: bold;"> Query query = getSession().createQuery("update beans.StockQuoteBean set stockSymbol=? where quoteId=?");</span><br /><span style="font-weight: bold;"> query.setString(0, stockQuoteBean.getStockSymbol());</span><br /><span style="font-weight: bold;"> query.setString(1, stockQuoteBean.getQuoteId());</span><br /><span style="font-weight: bold;"> query.executeUpdate();</span><br /><span style="font-weight: bold;"> }</span><br /><span style="font-weight: bold;"> public StockQuoteBean getStockQuote(String id) {</span><br /><span style="font-weight: bold;"> Query query = getSession().createQuery("from beans.StockQuoteBean stockQuoteBean where stockQuoteBean.quoteId=?");</span><br /><span style="font-weight: bold;"> query.setString(0, id);</span><br /><span style="font-weight: bold;"> List results = query.list();</span><br /><span style="font-weight: bold;"> if(results == null || results.size() == 0) {</span><br /><span style="font-weight: bold;"> throw new RuntimeException("No result");</span><br /><span style="font-weight: bold;"> }</span><br /><span style="font-weight: bold;"> return (StockQuoteBean)results.get(0);</span><br /><span style="font-weight: bold;"> }</span><br /><br />public void updateStockQuote_hibernateTemplate(StockQuoteBean StockQuoteBean) {<br />getHibernateTemplate().update(StockQuoteBean);<br /><br />}<br />public StockQuoteBean getStockQuote_hibernateTemplate(String id) {<br />List&lt;StockQuoteBean&gt; transactions = getHibernateTemplate().find("from beans.StockQuoteBean stockQuoteBean where stockQuoteBean.quoteId=?", id);<br />return transactions.get(0);<br />}<br /><br />}</pre><center>PortfolioDAOSupport</center><br />This class uses HibernateDaoSupport to get instances of HibernateTemplate, and the Hibernate Session. The getStockQuote() and updateStockQuote() in this class make calls to hibernate session directly.</li></ol></li><li><span style="font-weight: bold;">The application context</span><pre>&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />&lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br />xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"<br />xsi:schemaLocation="<br />http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd<br />http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd<br />http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"&gt;<br /><br /><span style="font-weight: bold;"> &lt;bean id="portfolioDAOTemplate" class="dao.PortfolioDAOTemplate"&gt;</span><br /><span style="font-weight: bold;"> &lt;property name="hibernateTemplate" ref="hibernateTemplate" /&gt;</span><br /><span style="font-weight: bold;"> &lt;/bean&gt;</span><br /><span style="font-weight: bold;"> </span><br /><span style="font-weight: bold;"> &lt;bean id="portfolioDAOSupport" class="dao.PortfolioDAOSupport"&gt;</span><br /><span style="font-weight: bold;"> &lt;property name="hibernateTemplate" ref="hibernateTemplate" /&gt;</span><br /><span style="font-weight: bold;"> &lt;/bean&gt;</span><br /><br />&lt;bean id="portfolioService" class="springhibernate.PortfolioService"&gt;<br />&lt;property name="portfolioDAO" ref="portfolioDAOSupport"&gt;&lt;/property&gt;<br />&lt;/bean&gt;<br /><br />&lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;<br />&lt;property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /&gt;<br />&lt;property name="url" value="jdbc:oracle:thin:@localhost:1521/xe" /&gt;<br />&lt;property name="username" value="appUser" /&gt;<br />&lt;property name="password" value="password" /&gt;<br />&lt;/bean&gt;<br /><span style="font-weight: bold;">&lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;</span><br /><span style="font-weight: bold;"> &lt;property name="dataSource" ref="dataSource" /&gt;</span><br /><span style="font-weight: bold;"> &lt;property name="mappingResources"&gt;</span><br /><span style="font-weight: bold;"> &lt;list&gt;</span><br /><span style="font-weight: bold;"> &lt;value&gt;stockquote.hbm.xml&lt;/value&gt;</span><br /><span style="font-weight: bold;"> &lt;/list&gt;</span><br /><span style="font-weight: bold;"> &lt;/property&gt;</span><br /><br /><span style="font-weight: bold;"> &lt;property name="hibernateProperties"&gt;</span><br /><span style="font-weight: bold;"> &lt;props&gt;</span><br /><span style="font-weight: bold;"> &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.Oracle9Dialect&lt;/prop&gt;</span><br /><span style="font-weight: bold;"> &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;</span><br /><span style="font-weight: bold;"> &lt;prop key="hibernate.generate_statistics"&gt;true&lt;/prop&gt;</span><br /><span style="font-weight: bold;"> &lt;/props&gt;</span><br /><span style="font-weight: bold;"> &lt;/property&gt;</span><br /><span style="font-weight: bold;"> &lt;/bean&gt;</span><br /><span style="font-weight: bold;"> &lt;bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"&gt;</span><br /><span style="font-weight: bold;"> &lt;property name="sessionFactory" ref="sessionFactory" /&gt;</span><br /><span style="font-weight: bold;"> &lt;/bean&gt;</span><br />&lt;/beans&gt;</pre><center>applicationContext.xml</center><ul><li>The SessionFactory is defined with the datasource and mapping-resources. The hibernate specific properties are defined under the hibernateProperties property.</li><li>The HibernateTemplate uses are reference to the SessionFactory.</li><li>The HibernateTemplate is used as a reference to the DAO classes.</li><li>The porfolioService bean in uses a reference to the PortfolioDAO, which can be switched between the dao.PortfolioDAOSupport and dao.PortfolioDAOTemplate beans</li></ul></li><li>The main class<pre>package springhibernate;<br /><br /><br />import org.springframework.beans.factory.BeanFactory;<br />import org.springframework.beans.factory.xml.XmlBeanFactory;<br />import org.springframework.core.io.FileSystemResource;<br />import org.springframework.core.io.Resource;<br /><br />import beans.StockQuoteBean;<br /><br /><br />public class SpringHibernateTest {<br /><br /><br />public static void main(String[] args) {<br />Resource resource = new FileSystemResource("applicationContext.xml");<br />BeanFactory factory = new XmlBeanFactory(resource);<br /><br />PortfolioService portfolioService = (PortfolioService) factory.getBean("portfolioService");<br /><br />StockQuoteBean result = portfolioService.getStockQuote("123");<br />System.out.println(result.getStockSymbol());<br /><br />empResult.setStockSymbol("GOOG");<br />portfolioService.updateStockQuote(result);<br />}<br /><br /><br />}</pre><center>SpringHibernateTest.java</center></li><li>Necessary JAR files: <ul><li>commons-logging-1.1.1.jar</li><li>hibernate3.jar</li><li>dom4j-1.6.1.jar</li><li>ojdbc14.jar</li><li>commons-collections-3.2.jar</li><li>log4j-1.2.15.jar</li><li>commons-dbcp.jar</li><li>commons-pool.jar</li><li>spring.jar</li><li>cglib-nodep-2.1_3.jar</li><li>antlr-2.7.6.jar</li><li>jta.jar</li></ul></li></ol></span>Abhihttp://www.blogger.com/profile/05788965755655733389noreply@blogger.comtag:blogger.com,1999:blog-18313336.post-46722414945408436622008-04-11T14:18:00.003-04:002008-04-11T14:52:36.569-04:00Struts 2 Custom ValidatorsStruts 2 allows the use of Custom validators through the <span style="font-weight: bold;">@CustomValidator</span> annotation. The @CustomValidator annotation takes two mandatory parameters, type and message<ul><li><span style="font-weight: bold;">type</span>: Refers to the "name" given to the validator in the validators.xml file.</li><li><span style="font-weight: bold;">message</span>: Message to be displayed when this validator fails.</li></ul> A custom validator can be implemented by extending the <span style="font-weight: bold;">FieldValidatorSupport</span>, or alternatively the <span style="font-weight: bold;">ValidatorSupport </span>classes. The following sample builds on the example built in the previous post, <a href="http://java-x.blogspot.com/2008/04/struts-2-validation-annotations.html">Struts 2 Validaton: Annotations</a>.<br /><br />To run this sample, follow these steps... <a href="http://java-x.blogspot.com/2008/04/struts-2-custom-validators.html">There's More</a><br /><span class="fullpost"><br /><ol><li>Create a simple struts project as described in the previous example, <a href="http://java-x.blogspot.com/2008/04/struts-2-validation-annotations.html">Struts 2 Validation : Annotations</a></li><li>Create the new validator by extending the FieldValidatorSupport class<pre>package validators;<br /><br />import com.opensymphony.xwork2.validator.ValidationException;<br />import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;<br /><br />public class NumberFieldValidator extends FieldValidatorSupport {<br /><br /> public void validate(Object object) throws ValidationException {<br /><span style="font-weight: bold;"> String fieldName = getFieldName();</span><br /><span style="font-weight: bold;"> Object value = this.getFieldValue(fieldName, object);</span><br /><br /> if (!(value instanceof String)) {<br /> return;<br /> }<br /> <br /> String str = ((String) value).trim();<br /> if (str.length() == 0) {<br /> return;<br /> }<br /> <br /> try {<br /><span style="font-weight: bold;"> Double.parseDouble(str);</span><br /> }catch(NumberFormatException nfe) {<br /><span style="font-weight: bold;"> addFieldError(fieldName, object);</span><br /> return;<br /> }<br /> try {<br /><span style="font-weight: bold;"> Integer.parseInt(str);</span><br /> }catch(NumberFormatException nfe) {<br /><span style="font-weight: bold;"> addFieldError(fieldName, object);</span><br /> return;<br /> }<br /> <br /> }<br />}<br /></pre><center>NumberFieldValidator.java</center><ul><li>The custom validator may extend the FieldValidatorSupport or the ValidatorSupport classes.</li><li>The FieldValidatorSupport class extends ValidatorSupport to add field specific information to the Field.</li><li>The is numeric check is performed by trying to convert the input string to Integer or Double and catching any exception.</li><li>The <span style="font-weight: bold;">addFieldError </span>method is used add any failed validations to the list of errors to be displayed.</li><li>The getFieldName and getFieldValue methods are implemented in the superclasses to retrieve the field name and field value for the field beign validated.</li></ul></li><li>Declare the new custom validator in the validators.xml file. The validators.xml file must be in the classpath of the application.<pre>&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />&lt;!DOCTYPE validators PUBLIC<br /> "-//OpenSymphony Group//XWork Validator Config 1.0//EN"<br /> "http://www.opensymphony.com/xwork/xwork-validator-config-1.0.dtd"&gt;<br />&lt;validators&gt;<br /><span style="font-weight: bold;"> &lt;validator name="numericField" class="validators.NumberFieldValidator"/&gt;</span><br />&lt;/validators&gt;</pre><center>validators.xml</center><br /><span style="font-weight: bold;">Note: The name attribute of the validator must match the type attribute of the @CustomValidator Annotation used in the Action class.</span></li><li>Add the additional check to the Action class setPrice() method.<pre> @RequiredStringValidator(type = ValidatorType.FIELD, message = "Price Required")<br /><span style="font-weight: bold;">@CustomValidator(type = "numericField", message = "Price must be a number")</span><br />public void setPrice(String price) {<br /> this.price = price;<br />}</pre><center>AddTransactionAction.java</center><br />Note: the type attribute of the @CustomValidator Annotation must match the name of the validator as defined in the validators.xml file.</li></ol><br /></span>Abhihttp://www.blogger.com/profile/05788965755655733389noreply@blogger.comtag:blogger.com,1999:blog-18313336.post-20820690397963162922008-04-11T13:08:00.006-04:002008-04-11T13:24:39.738-04:00Struts 2 Validation : AnnotationsIn a previous post, I described how to use validations in Struts 2, using XML validation rules. This post will show how to use Annotation based validation in Struts 2. For this example I used the add transaction part of google's portfolio manager (noticed that they do not have validations over there). Struts 2 provides a number of validators for XML based validation rules. All of them have respective annotations defined and can be used in place of XML validation rules. In the example, we will use the <span style="font-weight:bold;">@RequiredStringValidator</span>, <span style="font-weight:bold;">@RegexFieldValidator</span> and also see how to <span style="font-weight:bold;">parameterize messages</span> when using annotations.<br /><br />Follow these steps to implement the example ... <a href="http://java-x.blogspot.com/2008/04/struts-2-validation-annotations.html">There's more</a><br /><span class="fullpost"><br /><ol><li>Create a dynamic web project in Eclipse.</li><li>Copy the following jar files into the WEB-INF/lib directory, all these files are available with sturts download.<ul><li>struts2-core-2.0.11.1.jar</li><li>xwork-2.0.4.jar</li><li>freemarker-2.3.8.jar</li><li>commons-logging-1.1.1.jar</li><li>ognl-2.6.11.jar</li></ul></li><li>Update your web deployment desciptor to include the sturts filter dispatcher.<pre>&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />&lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br /> xmlns="http://java.sun.com/xml/ns/javaee"<br /> xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"<br /> xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"<br /> id="WebApp_ID" version="2.5"&gt;<br /> &lt;display-name&gt;struts2Validation&lt;/display-name&gt;<br /> &lt;filter&gt;<br /> &lt;filter-name&gt;struts2&lt;/filter-name&gt;<br /> &lt;filter-class&gt;<br /> org.apache.struts2.dispatcher.FilterDispatcher<br /> &lt;/filter-class&gt;<br /> &lt;/filter&gt;<br /> &lt;filter-mapping&gt;<br /> &lt;filter-name&gt;struts2&lt;/filter-name&gt;<br /> &lt;url-pattern&gt;/*&lt;/url-pattern&gt;<br /> &lt;/filter-mapping&gt;<br /><br /> &lt;welcome-file-list&gt;<br /> &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;<br /> &lt;/welcome-file-list&gt;<br />&lt;/web-app&gt;<br /></pre><center>WEB-INF/web.xml</center></li><li>Create the input JSP <pre>&lt;%@ taglib prefix="s" uri="/struts-tags"%&gt;<br />&lt;html&gt;<br />&lt;head&gt;<br />&lt;title&gt;Add Transaction&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />&lt;s:form action="addTransaction" validate="true" method="post"&gt;<br />&lt;s:fielderror /&gt;<br />&lt;table&gt;<br /> &lt;tr&gt;<br /> &lt;td&gt;Symbol&lt;/td&gt;<br /> &lt;td&gt;&lt;s:textfield name="symbol"&gt;&lt;/s:textfield&gt;&lt;/td&gt;<br /> &lt;/tr&gt;<br /> &lt;tr&gt;<br /> &lt;td&gt;Type&lt;/td&gt;<br /> &lt;td&gt;&lt;s:select name="type" list="#{'':'Select One', 'BUY':'Buy','SELL':'Sell','BUY_COVER':'Buy to Cover','SELL_SHORT':'Sell Short'}"&gt;<br /> &lt;/s:select&gt;&lt;/td&gt;<br /> &lt;/tr&gt;<br /> &lt;tr&gt;<br /> &lt;td&gt;Date&lt;/td&gt;<br /> &lt;td&gt;&lt;s:textfield name="date" &gt;&lt;/s:textfield&gt;&lt;/td&gt;<br /> &lt;/tr&gt;<br /> &lt;tr&gt;<br /> &lt;td&gt;Number of Shares&lt;/td&gt;<br /> &lt;td&gt;&lt;s:textfield name="numberOfShares"&gt;&lt;/s:textfield&gt;&lt;/td&gt;<br /> &lt;/tr&gt;<br /> &lt;tr&gt;<br /> &lt;td&gt;Price&lt;/td&gt;<br /> &lt;td&gt;&lt;s:textfield name="price"&gt;&lt;/s:textfield&gt;&lt;/td&gt;<br /> &lt;/tr&gt;<br /> &lt;tr&gt;<br /> &lt;td&gt;Commission&lt;/td&gt;<br /> &lt;td&gt;&lt;s:textfield name="comission"&gt;&lt;/s:textfield&gt;&lt;/td&gt;<br /> &lt;/tr&gt;<br /> &lt;tr&gt;<br /> &lt;td&gt;Notes&lt;/td&gt;<br /> &lt;td&gt;&lt;s:textfield name="notes"&gt;&lt;/s:textfield&gt;&lt;/td&gt;<br /> &lt;/tr&gt;<br /> &lt;tr&gt;<br /><span style="font-weight: bold;"> &lt;td colspan="2"&gt; &lt;s:submit name="submit" value="submit"&gt;&lt;/s:submit&gt;</span><br /><span style="font-weight: bold;"> &lt;s:submit name="submitNoValidate" value="submit without validation" method="noValidation"&gt;&lt;/s:submit&gt; &lt;/td&gt;</span><br /> <br /> &lt;/tr&gt;<br />&lt;/table&gt;<br />&lt;/s:form&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;</pre><center>transactions.jsp</center><br /><span style="font-weight: bold;">Note</span>: The method="noValidation" indicates to struts that on submission, the noValidation() method will be invoked on the AddTransactionAction class.</li><li>Create the output JSP<pre>&lt;%@ taglib prefix="s" uri="/struts-tags"%&gt;<br />&lt;html&gt;<br />&lt;head&gt;<br />&lt;title&gt;Transaction Added&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />&lt;s:bean name="actions.AddTransactionAction" id="addTransaction" &gt;&lt;/s:bean&gt;<br />&lt;table&gt;<br /> &lt;tr&gt;<br /> &lt;td colspan="2"&gt;The following transaction has been added to your portfolio&lt;/td&gt;<br /> &lt;/tr&gt;<br /> &lt;tr&gt;<br /> &lt;td&gt;Symbol&lt;/td&gt;<br /> &lt;td&gt;&lt;s:label name="symbol" value="%{symbol}" /&gt;&lt;/td&gt;<br /> &lt;/tr&gt;<br /> &lt;tr&gt;<br /> &lt;td&gt;Type&lt;/td&gt;<br /> &lt;td&gt;&lt;s:label name="type" value="%{type}" /&gt;<br />&lt;/td&gt;<br /> &lt;/tr&gt;<br /> &lt;tr&gt;<br /> &lt;td&gt;Date&lt;/td&gt;<br /> &lt;td&gt;&lt;s:label name="date" value="%{date}"/&gt;&lt;/td&gt;<br /> &lt;/tr&gt;<br /> &lt;tr&gt;<br /> &lt;td&gt;Number of Shares&lt;/td&gt;<br /> &lt;td&gt;&lt;s:label name="numberOfShares" value="%{numberOfShares}"&gt;&lt;/s:label&gt;&lt;/td&gt;<br /> &lt;/tr&gt;<br /> &lt;tr&gt;<br /> &lt;td&gt;Price&lt;/td&gt;<br /> &lt;td&gt;&lt;s:label name="price" value="%{price}"&gt;&lt;/s:label&gt;&lt;/td&gt;<br /> &lt;/tr&gt;<br /> &lt;tr&gt;<br /> &lt;td&gt;Commission&lt;/td&gt;<br /> &lt;td&gt;&lt;s:label name="comission" value="%{comission}"&gt;&lt;/s:label&gt;&lt;/td&gt;<br /> &lt;/tr&gt;<br /> &lt;tr&gt;<br /> &lt;td&gt;Notes&lt;/td&gt;<br /> &lt;td&gt;&lt;s:label name="notes" value="%{notes}"&gt;&lt;/s:label&gt;&lt;/td&gt;<br /> &lt;/tr&gt;<br />&lt;/table&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;</pre><center>done.jsp</center></li><li>Create the Action class <pre>package actions;<br /><br />import org.apache.struts2.interceptor.validation.SkipValidation;<br /><br />import com.opensymphony.xwork2.ActionSupport;<br />import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;<br />import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;<br />import com.opensymphony.xwork2.validator.annotations.ValidatorType;<br />import com.opensymphony.xwork2.validator.annotations.Validation;<br /><br /><span style="font-weight: bold;">@Validation</span><br />public class AddTransactionAction extends ActionSupport {<br /> <br /> private String symbol;<br /> private String type;<br /> private String date;<br /> private String numberOfShares;<br /> private String price;<br /> private String comission;<br /> private String notes;<br /><br /> <br /> public String execute() throws Exception {<br /> System.out.println("In Execute");<br /> <br /> return SUCCESS;<br /> }<br /> <br /> <span style="font-weight: bold;" title="validations can be be skipped using the @SkipValidation annotation on a method.">@SkipValidation</span><br /> public String noValidation() throws Exception {<br /> System.out.println("In Novalidation");<br /> return SUCCESS;<br /> }<br /><br /><br /> public String getSymbol() {<br /> return symbol;<br /> }<br /><br /> <span style="font-weight: bold;">@RequiredStringValidator(type = ValidatorType.FIELD, message = "Symbol Required")</span><br /> public void setSymbol(String symbol) {<br /> this.symbol = symbol;<br /> }<br /><br /><br /> public String getType() {<br /> return type;<br /> }<br /><br /> <span style="font-weight: bold;">@RequiredStringValidator(type = ValidatorType.FIELD, message = "Type Required")</span><br /> public void setType(String type) {<br /> this.type = type;<br /> }<br /><br /><br /> public String getDate() {<br /> return date;<br /> }<br /><br /><span style="font-weight: bold;"> @RequiredStringValidator(type = ValidatorType.FIELD, message = "Date Required")</span><br /><span style="font-weight: bold;" title="the key attribute is used to denote the message key from the properties files."> @RegexFieldValidator(type=ValidatorType.FIELD, message="",key="date.error.message", expression = "[0-9][0-9]/[0-9][0-9]/[1-9][0-9][0-9][0-9]")</span><br /> public void setDate(String date) {<br /> this.date = date;<br /> }<br /><br /><br /> public String getNumberOfShares() {<br /> return numberOfShares;<br /> }<br /><br /><br /><span style="font-weight: bold;"> @RequiredStringValidator(type = ValidatorType.FIELD, message = " Number of Shares Required")</span><br /> public void setNumberOfShares(String numberOfShares) {<br /> this.numberOfShares = numberOfShares;<br /> }<br /><br /><br /> public String getPrice() {<br /> return price;<br /> }<br /><br /><span style="font-weight: bold;"> @RequiredStringValidator(type = ValidatorType.FIELD, message = "Price Required")</span><br /> public void setPrice(String price) {<br /> this.price = price;<br /> }<br /><br /><br /> public String getComission() {<br /> return comission;<br /> }<br /><br /><span style="font-weight: bold;"> @RequiredStringValidator(type = ValidatorType.FIELD, message = "Comission Required")</span><br /> public void setComission(String comission) {<br /> this.comission = comission;<br /> }<br /><br /><br /> public String getNotes() {<br /> return notes;<br /> }<br /><br /><br /> public void setNotes(String notes) {<br /> this.notes = notes;<br /> }<br />}</pre><center>AddTransactionAction.java</center><br />Note:<ul><li>The annotation <span style="font-weight: bold;">@Validation</span> is used to indicate that the current action might need validation. The validations on a method level can be skipped using the <span style="font-weight: bold;">@SkipValidation</span> annotation on the method.</li><li>The method noValidations() uses the <span style="font-weight: bold;">@SkipValidation</span> annotation, you can see this when you click on "Submit without validation" in the JSP</li><li>The <span style="font-weight: bold;">@RequiredStringValidator</span> annotation is used to indicate a Required Strint similar to the following xml rule<pre>&lt;validators&gt;<br />&lt;field name="numberOfShares"&gt;<br />&lt;field-validator type="requiredstring"&gt;<br /> &lt;message&gt;Number of Share is required&lt;/message&gt;<br />&lt;/field-validator&gt;<br />&lt;/field&gt;<br />&lt;/validators&gt;</pre></li><li>On the date field, I used a <span style="font-weight: bold;">@RegexFieldValidator</span> annotation, so that the date field will be mandated to have a given format.</li><li><span style="font-weight: bold;">Parameterized messages</span>: You will notice that the message attribute of the @RegexFieldValidator is set to an empty string, while the key is set to a value. This is due to the fact that the message attribute is mandatory, and the <span style="font-weight: bold;">key attribute is used to denote the message key from the properties files</span>. <span style="font-weight: bold;">The parameters can be retrieved in the properties files using the ${date} notation where the "date" variable is expected to available in the value stack</span></li></ul></li><li>Create a definition for the action in struts.xml<pre>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;<br />&lt;!DOCTYPE struts PUBLIC<br />"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"<br />"http://struts.apache.org/dtds/struts-2.0.dtd"&gt;<br /><br />&lt;struts&gt;<br />&lt;package name="struts2Validation" extends="struts-default"&gt;<br /> &lt;action name="addTransaction"<br /> class="actions.AddTransactionAction"&gt;<br /> <span style="font-weight: bold;">&lt;result name="success"&gt;done.jsp&lt;/result&gt;</span><br /> <span style="font-weight: bold;">&lt;result name="input"&gt;transactions.jsp&lt;/result&gt;</span><br /> &lt;/action&gt;<br />&lt;/package&gt;<br />&lt;/struts&gt;</pre><center>struts.xml</center><br />Note: The result with name "input" is because the validator returns the result to input when validation fails.</li><li>Create the properties file for messages<pre>date.error.message=Date <span style="font-weight: bold;">${date}</span> is not properly formatted.</pre><center>package.properties</center><br />Note: In the properties file, ${date} is used to retrieve the "date" value from the value stack, this is the way Struts 2 supports parameterization.</li><br /><li>Create the struts.properties file to set the theme to simple theme, so that you have more control how the UI components are laid out.<pre>struts.ui.theme=simple</pre><center>struts.properties</center></li></ol></span>Abhihttp://www.blogger.com/profile/05788965755655733389noreply@blogger.comtag:blogger.com,1999:blog-18313336.post-16135621077521201772008-04-01T14:48:00.003-04:002008-04-01T14:56:40.221-04:00Integrating Struts 2.0 and tilesI am currently evaluating some web frameworks for a pet project and was trying to implement Struts 2 with tiles. Neither the Sturts 2 website, nor the tiles website gave an easy way to integrated Struts 2 and tiles. It took me a while to get them to work together. This post describes a way I figured out how to integrate Struts 2 with tiles. Struts 2 provides a plugin for integrating tiles 2. This plugin is included in the complete bundle (struts-2.x.x.x-all.zip). The following are are the steps needed to integrate struts2 with tiles.<br /><span style="font-weight:bold;"><a href="http://java-x.blogspot.com/2008/04/integrating-struts-20-and-tiles.html">Skip to Sample Code</a></span><span class="fullpost"><ol><li>Download the struts complete bundle from the <a href="http://sturts.apache.org/2.x">struts 2 website</a></li><li>Download tiles 2 from <a href="http://tiles.apache.org/download.html">tiles 2 website</a></li><li>Download the tiles dependencies from the <a href="http://commons.apache.org/">jakarta commons site</a><ul><li>Commons BeanUtils 1.7.0 or above</li><li>Commons Digester 1.8 or above</li><li>Commons Logging 1.1 or above</li></ul></li><li>Create the layout page, and related files (except the layout, all the other files are basic jsps )<pre>&lt;%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %&gt;<br />&lt;html&gt;<br />&lt;head&gt;<br />&lt;title&gt;Insert title here&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />&lt;table width="100%" height="100%"&gt;<br /> &lt;tr height="20%"&gt;<br /> &lt;td colspan="2" align="center" bgcolor="skyblue"&gt;<br /> <span style="font-weight: bold;">&lt;tiles:insertAttribute name="header" /&gt;&lt;/td&gt;</span><br /> &lt;/tr&gt;<br /> &lt;tr&gt;<br /> <span style="font-weight: bold;">&lt;td bgcolor="cyan" width="75%"&gt;&lt;tiles:insertAttribute name="body" /&gt;&lt;/td&gt;</span><br /> &lt;/tr&gt;<br /> &lt;tr height="20%"&gt;<br /> <span style="font-weight: bold;">&lt;td colspan="2" align="center" bgcolor="skyblue"&gt;&lt;tiles:insertAttribute name="footer" /&gt;&lt;/td&gt;</span><br /> &lt;/tr&gt;<br />&lt;/table&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;</pre></li><li>Create the HelloWorld Action class<pre>package example;<br /><br />import com.opensymphony.xwork2.ActionSupport;<br /><br /><span style="font-weight: bold;">public class HelloWorld extends ActionSupport {</span><br /><br /> public String execute() throws Exception {<br /> System.out.println("Hello World");<br /> return SUCCESS;<br /> }<br />}</pre></li><li>Configure the Web Deployment descriptor by adding a <span style="font-weight: bold;">tiles listener</span> to the web.xml file of your web application.<pre>&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />&lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br /> xmlns="http://java.sun.com/xml/ns/javaee"<br /> xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"<br /> xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"<br /> id="WebApp_ID" version="2.5"&gt;<br /> &lt;display-name&gt;tilesTest&lt;/display-name&gt;<br /><span style="font-weight: bold;"> &lt;listener&gt;</span><br /><span style="font-weight: bold;"> &lt;listener-class&gt;</span><br /><span style="font-weight: bold;"> org.apache.struts2.tiles.StrutsTilesListener</span><br /><span style="font-weight: bold;"> &lt;/listener-class&gt;</span><br /><span style="font-weight: bold;"> &lt;/listener&gt;</span><br /> &lt;filter&gt;<br /> &lt;filter-name&gt;struts2&lt;/filter-name&gt;<br /> &lt;filter-class&gt;<br /> org.apache.struts2.dispatcher.FilterDispatcher<br /> &lt;/filter-class&gt;<br /> &lt;/filter&gt;<br /> &lt;filter-mapping&gt;<br /> &lt;filter-name&gt;struts2&lt;/filter-name&gt;<br /> &lt;url-pattern&gt;/*&lt;/url-pattern&gt;<br /> &lt;/filter-mapping&gt;<br /> &lt;welcome-file-list&gt;<br /> &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;<br /> &lt;/welcome-file-list&gt;<br />&lt;/web-app&gt;</pre></li><li>Configure struts to work with tiles, this can be done by either<ol><li>Extending the sturts package from "tiles-default"<pre style="font-weight: bold;">&lt;package name="tilesTest" extends="tiles-default"&gt;</pre></li><span style="font-weight: bold;">OR</span><li>Declaring a new "result-type", tiles, that will map to "<span style="font-weight: bold;">org.apache.struts2.views.tiles.TilesResult</span>"<pre style="font-weight: bold;">&lt;result-types&gt;<br />&lt;result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" /&gt;<br />&lt;/result-types&gt;</pre></li></ol></li><li>Set the type of the results in the package to "tiles"<pre style="font-weight: bold;">&lt;result name="success" type="tiles"&gt;helloworld.home&lt;/result&gt;</pre><br /> struts.xml<pre>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;<br />&lt;!DOCTYPE struts PUBLIC<br /> "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"<br /> "http://struts.apache.org/dtds/struts-2.0.dtd"&gt;<br /><br />&lt;struts&gt;<br /> &lt;package name="tilesTest" extends="struts-default"&gt;<br /><span style="font-weight: bold;"> &lt;result-types&gt;</span><br /><span style="font-weight: bold;"> &lt;result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" /&gt;</span><br /><span style="font-weight: bold;"> &lt;/result-types&gt;</span><br /> &lt;action name="helloWorld" class="example.HelloWorld"&gt;<br /> <span style="font-weight: bold;">&lt;result name="success" type="tiles"&gt;helloworld.home&lt;/result&gt;</span><br /> &lt;/action&gt;<br /> &lt;/package&gt;<br />&lt;/struts&gt;</pre><span style="font-weight: bold;">Note that the result "helloword.home" must match the definition name in tiles.xml file.</span></li><br /> <li>Create definitions for tiles in WEB-INF/tiles.xml file.<pre>&lt;!DOCTYPE tiles-definitions PUBLIC<br /> "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"<br /> "http://tiles.apache.org/dtds/tiles-config_2_0.dtd"&gt;<br />&lt;tiles-definitions&gt;<br /><span style="font-weight: bold;"> &lt;definition name="helloworld.home" template="/layouts/layout.jsp"&gt;</span><br /><span style="font-weight: bold;"> &lt;put-attribute name="header" value="/layouts/header.jsp" /&gt;</span><br /><span style="font-weight: bold;"> &lt;put-attribute name="body" value="/index.html" /&gt;</span><br /><span style="font-weight: bold;"> &lt;put-attribute name="footer" value="/layouts/footer.jsp" /&gt;</span><br /><span style="font-weight: bold;"> &lt;/definition&gt;</span><br />&lt;/tiles-definitions&gt;</pre></li><li>The following is a list of jar files used for this example (copied to the WEB-INF/lib directory)<ul><li>commons-beanutils.jar</li><li>commons-digester-1.8.jar</li><li>commons-logging-1.1.1.jar</li><li>freemarker-2.3.8.jar</li><li>ognl-2.6.11.jar</li><li>struts2-core-2.0.11.1.jar</li><li>struts2-tiles-plugin-2.0.11.1.jar</li><li>tiles-api-2.0.5.jar</li><li>tiles-core-2.0.5.jar</li><li>tiles-jsp-2.0.5.jar</li><li>xwork-2.0.4.jar</li></ul></li><li>This example was implemented on<span style="font-weight: bold;"> tomcat 6.0.16, with Java 5 update 11</span></li></ol></span>Abhihttp://www.blogger.com/profile/05788965755655733389noreply@blogger.comtag:blogger.com,1999:blog-18313336.post-56181759435205439442008-03-28T14:59:00.005-04:002008-03-28T15:05:43.677-04:00BlazeDS for Java-Flex communicationBlazeDS is a server-based Java remoting and web messaging technology that enables communication between back-end Java applications and Adobe Flex applications running in the browser. In this post, I describe a way (may not be the best) I was able to successfully to build a simple application using BlazeDS and Flex. The application is build using eclipse and ant, rather than using FlexBuilder. Following are the main steps that you have to follow to implement the example.<ol><li>Install tomcat</li><li>Install Flex sdk</li><li>Download BlazeDS web application.</li><li>Create tomcat user with manager permisison</li><li>Create dynamic web project in eclipse</li><li>Create java file</li><li>Create mxml file</li><li>Update the config files in the WEB-INF/flex directory of the web application</li><li>Copy flexTasks.tasks to the root directory.</li><li>Create build file</li><li>Copy catalina-ant.jar and flextasks.jar into ant lib directory, add them to ant runtime in eclipse.</li><li>Build application using ant</li></ol><a href="http://java-x.blogspot.com/2008/03/blazeds-for-java-flex-communication.html#sampleCode">Skip to Sample Code</a>: Moving the mouse over the bolded code parts shows additional information<span class="fullpost"><a name="sampleCode"></a><ol><li><u>Install tomcat</u>: This example was implemented on Tomcat 6.0.16</li><li><u>Install Flex sdk</u>: You can download the flex sdk from <a href="http://opensource.adobe.com/flex/">here</a> The example was implemented on Flex 3.0.0</li><li><u>Download BlazeDS</u>:Download the BlazeDS web application from <a href="http://opensource.adobe.com/wiki/display/blazeds/BlazeDS">here</a></li><li><u>Create tomcat user with manager permisison</u>: In the <i>TOMCAT_HOME</i>/tomcat-users.xml, add the following line<pre>&lt;role rolename="manager"/&gt;<br />&lt;user username="abhi" password="abhi" roles="manager"/&gt;</pre></li><li><u>Create dynamic web project in eclipse by importing the BlazeDS Web application war file.</u></li><li><u>Create java file</u>The java class simply echoes the data input in the textbox shown in the browser.<pre>package hello;<br />public class HelloWorld {<br /> <br /> public String sayHelloTo(String str) {<br /> System.out.println("Hello " + str);<br /> return "Hello " + str;<br /> }<br />}</pre></li><li><u>Create mxml file</u>The mxml file simply shows a text box and a submit button. A label will be displayed below the textbox with the word "Hello" appended to the input text.<pre>&lt;?xml version="1.0" encoding="utf-8"?&gt; <br />&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" viewSourceURL="srcview/index.html"&gt;<br /> &lt;mx:Script&gt;<br /> &lt;![CDATA[<br /><br /> import mx.rpc.events.FaultEvent;<br /> import mx.rpc.events.ResultEvent;<br /> <br /><br /> [Bindable]<br /> private var helloResult:String;<br /> private function sayHelloTo():void {<br /> ro.sayHelloTo(inputText.text);<br /> }<br /> private function resultHandler(event:ResultEvent):void<br /> {<br /> <br /> helloResult = event.result as String;<br /> }<br /><br /> ]]&gt;<br /> &lt;/mx:Script&gt; <br /> <b title="The destination name should match the destination defined in the remoting-config.xml file shown below">&lt;mx:RemoteObject id="ro" destination="helloworld" result="resultHandler(event)" /&gt;</b> <br /> &lt;mx:HBox width="100%"&gt;<br /> &lt;mx:TextInput id="inputText"/&gt;<br /> &lt;mx:Button label="Submit" click="sayHelloTo()"/&gt;<br /> &lt;/mx:HBox&gt;<br /> &lt;mx:Label text="{helloResult}"/&gt; <br />&lt;/mx:Application&gt;</pre><b>Note</b>: Defining the remote object enables you to make calls to the remote Java classes as if they are local action script classes.</li><li><u>Update the configuration files</u> For this example, you only have to update one config file, WEB-INF/flex/remoting-config.xml, to add a new destination, the HelloWorld class.<pre>&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />&lt;service id="remoting-service"<br /> class="flex.messaging.services.RemotingService"&gt;<br /><br /> &lt;adapters&gt;<br /> &lt;adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/&gt;<br /> &lt;/adapters&gt;<br /><br /> &lt;default-channels&gt;<br /> &lt;channel ref="my-amf"/&gt;<br /> &lt;/default-channels&gt;<br /> <br /> <b>&lt;destination id="helloworld"&gt;<br /> &lt;properties&gt;<br /> &lt;source&gt;hello.HelloWorld&lt;/source&gt;<br /> &lt;/properties&gt;<br /> &lt;/destination&gt;</b><br /><br /> <br /> &lt;/service&gt;<br /></pre></li><li><u>Copy flexTasks.tasks to the root directory.</u></li><li><u>Create build file</u>The ant build file uses two flex specific tasks, <code>mxmlc</code> to compile the mxml file and <code>html-wrapper</code> to create a html wrapper that contains the created swf file.<pre>&lt;project name="helloWorldServer" default="build" basedir="."&gt;<br /><br /> &lt;!-- ===================== Property Definitions =========================== --&gt;<br /><br /> &lt;property file="build.properties" /&gt;<br /> &lt;property file="${user.home}/build.properties" /&gt;<br /><br /><br /> &lt;!-- ==================== File and Directory Names ======================== --&gt;<br /><br /> &lt;property name="app.name" value="helloWorldServer" /&gt;<br /> &lt;property name="app.path" value="/${app.name}" /&gt;<br /> &lt;property name="app.version" value="0.1-dev" /&gt;<br /> &lt;property name="build.home" value="${basedir}/build" /&gt;<br /> &lt;property name="catalina.home" value="C:/unzipped/apache-tomcat-6.0.16" /&gt;<br /> &lt;property name="dist.home" value="${basedir}/dist" /&gt;<br /> &lt;property name="docs.home" value="${basedir}/docs" /&gt;<br /> &lt;property name="manager.url" value="http://localhost:8080/manager" /&gt;<br /> &lt;property name="src.home" value="${basedir}/src" /&gt;<br /> &lt;property name="web.home" value="${basedir}/WebContent" /&gt;<br /> &lt;property name="manager.username" value="abhi" /&gt;<br /> &lt;property name="manager.password" value="abhi" /&gt;<br /> <b>&lt;property name="FLEX_HOME" value="C:/Adobe/Flex" /&gt;<br /> &lt;property name="APP_ROOT" value="${basedir}/WebContent" /&gt;</b><br /><br /><br /> &lt;!-- ==================== External Dependencies =========================== --&gt;<br /><br /> &lt;!-- ==================== Compilation Classpath =========================== --&gt;<br /><br /> &lt;path id="compile.classpath"&gt;<br /> &lt;fileset dir="${catalina.home}/bin"&gt;<br /> &lt;include name="*.jar" /&gt;<br /> &lt;/fileset&gt;<br /> &lt;pathelement location="${catalina.home}/lib" /&gt;<br /> &lt;fileset dir="${catalina.home}/lib"&gt;<br /> &lt;include name="*.jar" /&gt;<br /> &lt;/fileset&gt;<br /><br /> &lt;/path&gt;<br /><br /> &lt;!-- ================== Custom Ant Task Definitions ======================= --&gt;<br /><br /><br /> &lt;taskdef resource="org/apache/catalina/ant/catalina.tasks" classpathref="compile.classpath" /&gt;<br /> <b title="flexTasks.tasks file should be in the root folder of the application as is the build file.">&lt;taskdef resource="flexTasks.tasks" classpath="c:/ant/lib/flexTasks.jar" /&gt;</b><br /><br /> &lt;!-- ==================== Compilation Control Options ==================== --&gt;<br /><br /> &lt;property name="compile.debug" value="true" /&gt;<br /> &lt;property name="compile.deprecation" value="false" /&gt;<br /> &lt;property name="compile.optimize" value="true" /&gt;<br /><br /><br /><br /> &lt;!-- ==================== All Target ====================================== --&gt;<br /><br /> &lt;target name="all" depends="clean,build" description="Clean build and dist directories, then compile" /&gt;<br /><br /><br /><br /> &lt;!-- ==================== Clean Target ==================================== --&gt;<br /><br /> &lt;target name="clean" description="Delete old build and dist directories"&gt;<br /> &lt;delete dir="${build.home}" /&gt;<br /> &lt;delete dir="${dist.home}" /&gt;<br /> &lt;/target&gt;<br /><br /><br /><br /> &lt;!-- ==================== Compile Target ================================== --&gt;<br /><br /> &lt;target name="build" depends="prepare" description="Compile Java sources"&gt;<br /><br /> &lt;!-- Compile Java classes as necessary --&gt;<br /> &lt;mkdir dir="${build.home}/WEB-INF/classes" /&gt;<br /> &lt;javac srcdir="${src.home}" destdir="${build.home}/WEB-INF/classes" debug="${compile.debug}" deprecation="${compile.deprecation}" optimize="${compile.optimize}"&gt;<br /> &lt;classpath refid="compile.classpath" /&gt;<br /> &lt;/javac&gt;<br /><br /> &lt;!-- Copy application resources --&gt;<br /> &lt;copy todir="${build.home}/WEB-INF/classes"&gt;<br /> &lt;fileset dir="${src.home}" excludes="**/*.java" /&gt;<br /> &lt;/copy&gt;<br /><br /> &lt;/target&gt;<br /><br /><br /><br /> &lt;!-- ==================== Dist Target ===================================== --&gt;<br /><br /> &lt;target name="dist" depends="build,javadoc" description="Create binary distribution"&gt;<br /><br /> &lt;!-- Copy documentation subdirectories --&gt;<br /> &lt;mkdir dir="${dist.home}/docs" /&gt;<br /> &lt;copy todir="${dist.home}/docs"&gt;<br /> &lt;fileset dir="${docs.home}" /&gt;<br /> &lt;/copy&gt;<br /><br /> &lt;!-- Create application JAR file --&gt;<br /> &lt;jar jarfile="${dist.home}/${app.name}-${app.version}.war" basedir="${build.home}" /&gt;<br /><br /> &lt;!-- Copy additional files to ${dist.home} as necessary --&gt;<br /><br /> &lt;/target&gt;<br /><br /> &lt;!-- ==================== Flex targets ==================================== --&gt;<br /><br /> <b>&lt;target name="appcompile" depends="install"&gt;</b><br /> <b>&lt;mxmlc file="${APP_ROOT}/helloWorld.mxml" context-root="/helloWorldServer" keep-generated-actionscript="true" services="${web.home}/WEB-INF/flex/services-config.xml" output="${catalina.home}/webapps/${app.name}/helloWorld.swf"&gt;<br /> &lt;compiler.library-path dir="${FLEX_HOME}/frameworks" append="true"&gt;<br /> &lt;include name="${web.home}/WEB-INF/lib/" /&gt;<br /> &lt;/compiler.library-path&gt;<br /> &lt;load-config filename="${FLEX_HOME}/frameworks/flex-config.xml" /&gt;<br /> &lt;source-path path-element="${FLEX_HOME}/frameworks" /&gt;<br /> &lt;/mxmlc&gt;<br /><br /> &lt;/target&gt;<br /><br /> &lt;target name="createHtmlWrapper" depends="appcompile"&gt;<br /> &lt;html-wrapper application="${APP_ROOT}/helloWorld.mxml" output="${catalina.home}/webapps/${app.name}" swf="helloWorld" /&gt;<br /> &lt;/target&gt;</b><br /> <br /><br /> &lt;!-- ==================== Install Target ================================== --&gt;<br /><br /> &lt;target name="install" depends="build" description="Install application to servlet container"&gt;<br /><br /> &lt;deploy url="${manager.url}" username="${manager.username}" password="${manager.password}" path="${app.path}" localWar="file://${build.home}" /&gt;<br /><br /> &lt;/target&gt;<br /><br /><br /> &lt;!-- ==================== Javadoc Target ================================== --&gt;<br /><br /> &lt;target name="javadoc" depends="build" description="Create Javadoc API documentation"&gt;<br /><br /> &lt;mkdir dir="${dist.home}/docs/api" /&gt;<br /> &lt;javadoc sourcepath="${src.home}" destdir="${dist.home}/docs/api" packagenames="*"&gt;<br /> &lt;classpath refid="compile.classpath" /&gt;<br /> &lt;/javadoc&gt;<br /><br /> &lt;/target&gt;<br /><br /><br /><br /> &lt;!-- ====================== List Target =================================== --&gt;<br /><br /> &lt;target name="list" description="List installed applications on servlet container"&gt;<br /><br /> &lt;list url="${manager.url}" username="${manager.username}" password="${manager.password}" /&gt;<br /><br /> &lt;/target&gt;<br /><br /><br /> &lt;!-- ==================== Prepare Target ================================== --&gt;<br /><br /> &lt;target name="prepare"&gt;<br /><br /> &lt;!-- Create build directories as needed --&gt;<br /> &lt;mkdir dir="${build.home}" /&gt;<br /> &lt;mkdir dir="${build.home}/WEB-INF" /&gt;<br /> &lt;mkdir dir="${build.home}/WEB-INF/classes" /&gt;<br /><br /><br /> &lt;!-- Copy static content of this web application --&gt;<br /> &lt;copy todir="${build.home}"&gt;<br /> &lt;fileset dir="${web.home}" /&gt;<br /> &lt;/copy&gt;<br /><br /> &lt;!-- Copy external dependencies as required --&gt;<br /> &lt;mkdir dir="${build.home}/WEB-INF/lib" /&gt;<br /><br /> &lt;!-- Copy static files from external dependencies as needed --&gt;<br /> &lt;!-- *** CUSTOMIZE HERE AS REQUIRED BY YOUR APPLICATION *** --&gt;<br /><br /> &lt;/target&gt;<br /><br /><br /> &lt;!-- ==================== Reload Target =================================== --&gt;<br /><br /> &lt;target name="reload" depends="build" description="Reload application on servlet container"&gt;<br /><br /> &lt;reload url="${manager.url}" username="${manager.username}" password="${manager.password}" path="${app.path}" /&gt;<br /><br /> &lt;/target&gt;<br /><br /><br /> &lt;!-- ==================== Remove Target =================================== --&gt;<br /><br /> &lt;target name="remove" description="Remove application on servlet container"&gt;<br /><br /> &lt;undeploy url="${manager.url}" username="${manager.username}" password="${manager.password}" path="${app.path}" /&gt;<br /><br /> &lt;/target&gt;<br />&lt;/project&gt;</pre></li><li><u>Addtional ant configuration: </u>Copy catalina-ant.jar and flextasks.jar into ant lib directory, add them to ant runtime in eclipse.</li><li>To build and install the file, simply run the createHtmlWrapper target in ant</li></ol>References<ul><li>The ant build file was created using the base build.xml file available on <a href="http://tomcat.apache.org/tomcat-6.0-doc/appdev/build.xml.txt">apache tomcat website</a>.</li><li>The config files are used as is available with the BlazeDS service.</li></ul></span>Abhihttp://www.blogger.com/profile/05788965755655733389noreply@blogger.comtag:blogger.com,1999:blog-18313336.post-12110084638934269302007-08-23T12:40:00.000-04:002007-08-23T12:51:33.221-04:00Star Office now freeGoogle is going to offer Sun's Star Office for free as a part of <a href="http://pack.google.com">Google Pack</a>. Also, Sun will now be traded under the Symbol JAVA replacing the old symbol SUNW.<br /><br /><ul><li><a href="http://bits.blogs.nytimes.com/2007/08/14/google-goes-after-microsoft-again/">Google Goes After Microsoft Again</a></li><li><a href="http://blogs.sun.com/jonathan/entry/java_is_everywhere">The Rise of JAVA - The Retirement of SUNW</a></li><li><a href="http://www.theregister.com/2007/08/23/sun_no_sunw_java/">Sun boots workstation roots in favor of JAVA ticker</a></li><li>On Slashdot - <a href="http://slashdot.org/articles/07/08/12/1634208.shtml">Google Pack Adds StarOffice</a></li></ul>Abhihttp://www.blogger.com/profile/05788965755655733389noreply@blogger.comtag:blogger.com,1999:blog-18313336.post-76639015025591468212007-08-20T15:26:00.000-04:002007-08-20T15:29:05.527-04:00Handling Security with Ajax, DWR and AcegiThis is an extension of a previous post that described how to secure your method calls using Acegi security. Here, I will go through how to secure your Asynchronous calls, using the same example with some modifications to include Ajax calls using Direct Web Remoting (DWR).<span class="fullpost"><ol><li>Create the example project as shown in "<a href="http://java-x.blogspot.com/2006/12/spring-security-with-acegi-security.html">Spring security with Acegi Security Framework</a>". This will be the starting point.</li><li>Create the SecureDAO object.<pre>package test;<br /><br />public class SecureDAO {<br /> public String create() {<br /> System.out.println("Create");<br /> return "create";<br /> }<br /><br /> public String read() {<br /> System.out.println("read");<br /> return "read";<br /> }<br /><br /> public String update() {<br /> System.out.println("update");<br /> return "update";<br /> }<br />}</pre></li><li>Update the applicationContext.xml file to include the security definitions by adding the following bean definitions as shown below<pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br />&lt;!DOCTYPE beans PUBLIC &quot;-//SPRING//DTD BEAN//EN&quot; &quot;http://www.springframework.org/dtd/spring-beans.dtd&quot;&gt;<br /><br />&lt;beans&gt;<br /><br />&lt;bean id=&quot;filterChainProxy&quot; class=&quot;org.acegisecurity.util.FilterChainProxy&quot;&gt;<br /> &lt;property name=&quot;filterInvocationDefinitionSource&quot;&gt;<br /> &lt;value&gt;<br /> CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON<br /> PATTERN_TYPE_APACHE_ANT<br /> /**=httpSessionContextIntegrationFilter,logoutFilter,authenticationProcessingFilter,securityContextHolderAwareRequestFilter,rememberMeProcessingFilter,anonymousProcessingFilter,exceptionTranslationFilter,filterInvocationInterceptor<br /> &lt;/value&gt;<br /> &lt;/property&gt;<br />&lt;/bean&gt;<br /><br />&lt;bean id=&quot;httpSessionContextIntegrationFilter&quot; class=&quot;org.acegisecurity.context.HttpSessionContextIntegrationFilter&quot;/&gt;<br /><br />&lt;bean id=&quot;logoutFilter&quot; class=&quot;org.acegisecurity.ui.logout.LogoutFilter&quot;&gt;<br /> &lt;constructor-arg value=&quot;/index.jsp&quot;/&gt;<br /> &lt;constructor-arg&gt;<br /> &lt;list&gt;<br /> &lt;ref bean=&quot;rememberMeServices&quot;/&gt;<br /> &lt;bean class=&quot;org.acegisecurity.ui.logout.SecurityContextLogoutHandler&quot;/&gt;<br /> &lt;/list&gt;<br /> &lt;/constructor-arg&gt;<br />&lt;/bean&gt;<br /><br />&lt;bean id=&quot;authenticationProcessingFilter&quot; class=&quot;org.acegisecurity.ui.webapp.AuthenticationProcessingFilter&quot;&gt;<br /> &lt;property name=&quot;authenticationManager&quot; ref=&quot;authenticationManager&quot;/&gt;<br /> &lt;property name=&quot;authenticationFailureUrl&quot; value=&quot;/login.jsp?errorId=1&quot;/&gt;<br /> &lt;property name=&quot;defaultTargetUrl&quot; value=&quot;/&quot;/&gt;<br /> &lt;property name=&quot;filterProcessesUrl&quot; value=&quot;/j_acegi_security_check&quot;/&gt;<br /> &lt;property name=&quot;rememberMeServices&quot; ref=&quot;rememberMeServices&quot;/&gt;<br />&lt;/bean&gt;<br /><br />&lt;bean id=&quot;securityContextHolderAwareRequestFilter&quot; class=&quot;org.acegisecurity.wrapper.SecurityContextHolderAwareRequestFilter&quot;/&gt;<br /><br />&lt;bean id=&quot;rememberMeProcessingFilter&quot; class=&quot;org.acegisecurity.ui.rememberme.RememberMeProcessingFilter&quot;&gt;<br /> &lt;property name=&quot;authenticationManager&quot; ref=&quot;authenticationManager&quot;/&gt;<br /> &lt;property name=&quot;rememberMeServices&quot; ref=&quot;rememberMeServices&quot;/&gt;<br />&lt;/bean&gt;<br /><br />&lt;bean id=&quot;anonymousProcessingFilter&quot; class=&quot;org.acegisecurity.providers.anonymous.AnonymousProcessingFilter&quot;&gt;<br /> &lt;property name=&quot;key&quot; value=&quot;changeThis&quot;/&gt;<br /> &lt;property name=&quot;userAttribute&quot; value=&quot;anonymousUser,ROLE_ANONYMOUS&quot;/&gt;<br />&lt;/bean&gt;<br /><br />&lt;bean id=&quot;exceptionTranslationFilter&quot; class=&quot;org.acegisecurity.ui.ExceptionTranslationFilter&quot;&gt;<br /> &lt;property name=&quot;authenticationEntryPoint&quot;&gt;<br /> &lt;bean class=&quot;org.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint&quot;&gt;<br /> &lt;property name=&quot;loginFormUrl&quot; value=&quot;/login.jsp&quot;/&gt;<br /> &lt;property name=&quot;forceHttps&quot; value=&quot;false&quot;/&gt;<br /> &lt;/bean&gt;<br /> &lt;/property&gt;<br /> &lt;property name=&quot;accessDeniedHandler&quot;&gt;<br /> &lt;bean class=&quot;org.acegisecurity.ui.AccessDeniedHandlerImpl&quot;&gt;<br /> &lt;property name=&quot;errorPage&quot; value=&quot;/denied.jsp&quot;/&gt;<br /> &lt;/bean&gt;<br /> &lt;/property&gt;<br />&lt;/bean&gt;<br /><br />&lt;bean id=&quot;filterInvocationInterceptor&quot; class=&quot;org.acegisecurity.intercept.web.FilterSecurityInterceptor&quot;&gt;<br /> &lt;property name=&quot;authenticationManager&quot; ref=&quot;authenticationManager&quot;/&gt;<br /> &lt;property name=&quot;accessDecisionManager&quot;&gt;<br /> &lt;bean class=&quot;org.acegisecurity.vote.AffirmativeBased&quot;&gt;<br /> &lt;property name=&quot;allowIfAllAbstainDecisions&quot; value=&quot;false&quot;/&gt;<br /> &lt;property name=&quot;decisionVoters&quot;&gt;<br /> &lt;list&gt;<br /> &lt;bean class=&quot;org.acegisecurity.vote.RoleVoter&quot;/&gt;<br /> &lt;bean class=&quot;org.acegisecurity.vote.AuthenticatedVoter&quot;/&gt;<br /> &lt;/list&gt;<br /> &lt;/property&gt;<br /> &lt;/bean&gt;<br /> &lt;/property&gt;<br /> &lt;property name=&quot;objectDefinitionSource&quot;&gt;<br /> &lt;value&gt;<br /> CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON<br /> PATTERN_TYPE_APACHE_ANT<br /> /secure/admin/**=ROLE_ADMIN<br /> /secure/**=IS_AUTHENTICATED_REMEMBERED<br /> /**=IS_AUTHENTICATED_ANONYMOUSLY<br /> &lt;/value&gt;<br /> &lt;/property&gt;<br />&lt;/bean&gt;<br /><br />&lt;bean id=&quot;rememberMeServices&quot; class=&quot;org.acegisecurity.ui.rememberme.TokenBasedRememberMeServices&quot;&gt;<br /> &lt;property name=&quot;userDetailsService&quot; ref=&quot;userDetailsService&quot;/&gt;<br /> &lt;property name=&quot;tokenValiditySeconds&quot; value=&quot;1800&quot;&gt;&lt;/property&gt;<br /> &lt;property name=&quot;key&quot; value=&quot;changeThis&quot;/&gt;<br />&lt;/bean&gt;<br /><br />&lt;bean id=&quot;authenticationManager&quot; class=&quot;org.acegisecurity.providers.ProviderManager&quot;&gt;<br /> &lt;property name=&quot;providers&quot;&gt;<br /> &lt;list&gt;<br /> &lt;ref local=&quot;daoAuthenticationProvider&quot;/&gt;<br /> &lt;bean class=&quot;org.acegisecurity.providers.anonymous.AnonymousAuthenticationProvider&quot;&gt;<br /> &lt;property name=&quot;key&quot; value=&quot;changeThis&quot;/&gt;<br /> &lt;/bean&gt;<br /> &lt;bean class=&quot;org.acegisecurity.providers.rememberme.RememberMeAuthenticationProvider&quot;&gt;<br /> &lt;property name=&quot;key&quot; value=&quot;changeThis&quot;/&gt;<br /> &lt;/bean&gt;<br /> &lt;/list&gt;<br /> &lt;/property&gt;<br />&lt;/bean&gt;<br /><br />&lt;bean id=&quot;daoAuthenticationProvider&quot; class=&quot;org.acegisecurity.providers.dao.DaoAuthenticationProvider&quot;&gt;<br /> &lt;property name=&quot;userDetailsService&quot; ref=&quot;userDetailsService&quot;/&gt;<br /> &lt;property name=&quot;userCache&quot;&gt;<br /> &lt;bean class=&quot;org.acegisecurity.providers.dao.cache.EhCacheBasedUserCache&quot;&gt;<br /> &lt;property name=&quot;cache&quot;&gt;<br /> &lt;bean class=&quot;org.springframework.cache.ehcache.EhCacheFactoryBean&quot;&gt;<br /> &lt;property name=&quot;cacheManager&quot;&gt;<br /> &lt;bean class=&quot;org.springframework.cache.ehcache.EhCacheManagerFactoryBean&quot;/&gt;<br /> &lt;/property&gt;<br /> &lt;property name=&quot;cacheName&quot; value=&quot;userCache&quot;/&gt;<br /> &lt;/bean&gt;<br /> &lt;/property&gt;<br /> &lt;/bean&gt;<br /> &lt;/property&gt;<br />&lt;/bean&gt;<br /><br />&lt;bean id=&quot;userDetailsService&quot; class=&quot;org.acegisecurity.userdetails.memory.InMemoryDaoImpl&quot;&gt;<br /> &lt;property name=&quot;userProperties&quot;&gt;<br /> &lt;bean class=&quot;org.springframework.beans.factory.config.PropertiesFactoryBean&quot;&gt;<br /> &lt;property name=&quot;location&quot; value=&quot;/WEB-INF/users.properties&quot;/&gt;<br /> &lt;/bean&gt;<br /> &lt;/property&gt;<br />&lt;/bean&gt;<br /><br />&lt;bean id=&quot;loggerListener&quot; class=&quot;org.acegisecurity.event.authentication.LoggerListener&quot;/&gt;<br /><br /> &lt;bean id=&quot;methodSecurityInterceptor&quot; class=&quot;org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor&quot;&gt;<br /> &lt;property name=&quot;authenticationManager&quot;&gt;<br /> &lt;ref bean=&quot;authenticationManager&quot; /&gt;<br /> &lt;/property&gt;<br /> &lt;property name=&quot;accessDecisionManager&quot;&gt;<br /> &lt;bean class=&quot;org.acegisecurity.vote.AffirmativeBased&quot;&gt;<br /> &lt;property name=&quot;allowIfAllAbstainDecisions&quot; value=&quot;false&quot; /&gt;<br /> &lt;property name=&quot;decisionVoters&quot;&gt;<br /> &lt;list&gt;<br /> &lt;bean class=&quot;org.acegisecurity.vote.RoleVoter&quot; /&gt;<br /> &lt;bean class=&quot;org.acegisecurity.vote.AuthenticatedVoter&quot; /&gt;<br /> &lt;/list&gt;<br /> &lt;/property&gt;<br /> &lt;/bean&gt;<br /> &lt;/property&gt;<br /><b> &lt;property name=&quot;objectDefinitionSource&quot;&gt;<br /> &lt;value&gt;<br /> test.SecureDAO.*=IS_AUTHENTICATED_REMEMBERED <br /> test.SecureDAO.u=ROLE_ADMIN<br /> &lt;/value&gt;<br /> &lt;/property&gt;<br /> </b>&lt;/bean&gt;<br /><br /><br /><b> &lt;bean id=&quot;autoProxyCreator&quot; class=&quot;org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator&quot;&gt;<br /> &lt;property name=&quot;interceptorNames&quot;&gt;<br /> &lt;list&gt;<br /> &lt;value&gt;methodSecurityInterceptor&lt;/value&gt;<br /> &lt;/list&gt;<br /> &lt;/property&gt;<br /> &lt;property name=&quot;beanNames&quot;&gt;<br /> &lt;list&gt;<br /> &lt;value&gt;secureDAO&lt;/value&gt;<br /> &lt;/list&gt;<br /> &lt;/property&gt;<br /> &lt;/bean&gt;<br /> <br /> &lt;bean id=&quot;secureDAO&quot; class=&quot;test.SecureDAO&quot; /&gt;<br /></b>&lt;/beans&gt;</pre></li><li>Update the Web deployment descriptor to forward DWR requests to the DWR Servlet<pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br />&lt;web-app xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot;<br /> xmlns:web=&quot;http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;<br /> xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot; id=&quot;WebApp_ID&quot; version=&quot;2.5&quot;&gt;<br /> &lt;display-name&gt;DWRSpring&lt;/display-name&gt;<br /> &lt;context-param&gt;<br /> &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;<br /> &lt;param-value&gt;/WEB-INF/applicationContext.xml&lt;/param-value&gt;<br /> &lt;/context-param&gt;<br /> &lt;listener&gt;<br /> &lt;display-name&gt;SpringListener&lt;/display-name&gt;<br /> &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;<br /> &lt;/listener&gt;<br /> &lt;filter&gt;<br /> &lt;filter-name&gt;Acegi Filter Chain Proxy&lt;/filter-name&gt;<br /> &lt;filter-class&gt;org.acegisecurity.util.FilterToBeanProxy&lt;/filter-class&gt;<br /> &lt;init-param&gt;<br /> &lt;param-name&gt;targetClass&lt;/param-name&gt;<br /> &lt;param-value&gt;org.acegisecurity.util.FilterChainProxy&lt;/param-value&gt;<br /> &lt;/init-param&gt;<br /> &lt;/filter&gt;<br /><br /> &lt;filter-mapping&gt;<br /> &lt;filter-name&gt;Acegi Filter Chain Proxy&lt;/filter-name&gt;<br /> &lt;url-pattern&gt;/*&lt;/url-pattern&gt;<br /> &lt;/filter-mapping&gt;<br /><b> &lt;servlet&gt;<br /> &lt;servlet-name&gt;dwr-invoker&lt;/servlet-name&gt;<br /> &lt;servlet-class&gt;uk.ltd.getahead.dwr.DWRServlet&lt;/servlet-class&gt;<br /> &lt;init-param&gt;<br /> &lt;param-name&gt;debug&lt;/param-name&gt;<br /> &lt;param-value&gt;true&lt;/param-value&gt;<br /> &lt;/init-param&gt;<br /> &lt;/servlet&gt;<br /><br /> &lt;servlet-mapping&gt;<br /> &lt;servlet-name&gt;dwr-invoker&lt;/servlet-name&gt;<br /> &lt;url-pattern&gt;/dwr/*&lt;/url-pattern&gt;<br /> &lt;/servlet-mapping&gt;<br /> </b>&lt;welcome-file-list&gt;<br /> &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;<br /> &lt;welcome-file&gt;default.jsp&lt;/welcome-file&gt;<br /> &lt;/welcome-file-list&gt;<br />&lt;/web-app&gt;</pre></li><br /><li>In the DWR configuration file, set the creator to Spring<pre>&lt;!DOCTYPE dwr PUBLIC<br /> &quot;-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN&quot;<br /> &quot;http://www.getahead.ltd.uk/dwr/dwr10.dtd&quot;&gt;<br />&lt;dwr&gt;<br /> &lt;allow&gt;<br /><b> &lt;convert match=&quot;java.lang.Exception&quot; converter=&quot;exception&quot;/&gt;<br /> &lt;create creator=&quot;spring&quot; javascript=&quot;secureDAO&quot;&gt;<br /> </b>&lt;param name=&quot;beanName&quot; value=&quot;secureDAO&quot;/&gt;<br /> &lt;/create&gt;<br /> &lt;/allow&gt;<br />&lt;/dwr&gt;</pre>Note:<ul><li>The converter is used to convert the Java exception to Javascript exception.</li></ul></li><li>The main change will be made to the secure/authenticatedusers.jsp file. This will use DWR to make Ajax requests. Here is the code for the file<pre>&lt;%@ page import=&quot;org.acegisecurity.context.SecurityContextHolder&quot;%&gt;<br />&lt;html&gt;<br />&lt;head&gt;<br /><br /><b>&lt;script type='text/javascript' src='/DWRSpring/dwr/interface/secureDAO.js'&gt;&lt;/script&gt;<br />&lt;script type=&quot;text/javascript&quot; src=&quot;../dwr/engine.js&quot;&gt; &lt;/script&gt;<br />&lt;script type=&quot;text/javascript&quot; src=&quot;../dwr/util.js&quot;&gt; &lt;/script&gt;<br /></b>&lt;script&gt;<br /> <b>dwr.engine.setErrorHandler(errorHandlerFn);<br /> </b>function update() {<br /> var name = dwr.util.getValue(&quot;method&quot;);<br /> switch(name) {<br /> case &quot;create&quot;: <br /> secureDAO.create(callBackFn)<br /> break;<br /> case &quot;read&quot;: <br /> secureDAO.read(callBackFn);<br /> break;<br /> case &quot;update&quot;: <br /> secureDAO.update(callBackFn);<br /> break; <br /> }<br /> <br /> }<br /> <br /> function callBackFn(str) { <br /> dwr.util.setValue(&quot;selectedAction&quot;,&quot;Server Returned : &quot; + str); <br /> }<br /> <br /> function errorHandlerFn(message, exception) {<br /> dwr.util.setValue(&quot;selectedAction&quot;, &quot;Error : &quot; + message);<br /> }<br />&lt;/script&gt;<br /><br />&lt;/head&gt;<br />&lt;body&gt;<br />&lt;h1&gt;Welcome: &lt;%=SecurityContextHolder.getContext().getAuthentication().getName()%&gt;&lt;/h1&gt;<br />&lt;p&gt;&lt;a href=&quot;../&quot;&gt;Home&lt;/a&gt;<br />&lt;form name=&quot;testForm&quot; action=&quot;&quot;&gt;&lt;select name=&quot;method&quot; onchange=&quot;update()&quot;&gt;<br /> &lt;option value=&quot;&quot;&gt;&lt;/option&gt;<br /> &lt;option value=&quot;create&quot;&gt;create&lt;/option&gt;<br /> &lt;option value=&quot;read&quot;&gt;read&lt;/option&gt;<br /> &lt;option value=&quot;update&quot;&gt;update&lt;/option&gt;<br />&lt;/select&gt;&lt;/form&gt;<br /><br />&lt;div id=&quot;selectedAction&quot;&gt;&lt;/div&gt;<br />&lt;p&gt;&lt;a href=&quot;../j_acegi_logout&quot;&gt;Logout&lt;/a&gt;&lt;/p&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;</pre>Note:<ul><li>The classes that are exposed through DWR will be available through the /WEB_APP_NAME/dwr/interface/JAVASCRIPT_NAME.js files.<pre>&lt;script type='text/javascript' src='/DWRSpring/dwr/interface/secureDAO.js'&gt;&lt;/script&gt;</pre> </li><li>The setErrorhandler call sets the global error handling function.<pre>dwr.engine.setErrorHandler(errorHandlerFn);</pre>Alternatively, the error handling function can be set for individual method calls (as described in <a href="http://getahead.org/dwr/other/errors">DWR documentation</a>a<pre>Remote.method(params, {<br /> callback:function(data) { ... },<br /> errorHandler:function(errorString, exception) { ... }<br />});</pre></li><li>util.js file contains utility functions for getting and setting values for the document elements.</li></ul></li><li>Make sure you have the following Jar files in your classpath:<ul><li>acegi-security-1.0.3.jar</li><li>ant-junit.jar</li><li>cglib-nodep-2.1_3.jar</li><li>commons-codec-1.3.jar</li><li>commons-logging.jar</li><li>dwr.jar</li><li>ehcache-1.2.3.jar</li><li>jstl.jar</li><li>spring.jar</li><li>standard.jar</li></ul></li></ol></span>Abhihttp://www.blogger.com/profile/05788965755655733389noreply@blogger.comtag:blogger.com,1999:blog-18313336.post-68073367218802894842007-05-09T08:11:00.000-04:002007-05-09T08:50:18.139-04:00OpenJDK and JavaFXJava is now completely open source. The JDK source code is now available through the <a href="http://openjdk.java.net/">OpenJDK</a> project. According to the marketing manager of the OpenJDK project Rich Sands, Developers can,<pre>... learn how the JDK is put together, fix that bug that's been <br />driving you nuts, join the conversations in the mailing lists, <br />start or participate in projects to improve the implementation.</pre>It's good to see that the governing body consists of not just Sun employees.<br /><br />On a related note, <span class="fullpost">Sun has announced the new <a href="ht