找了一篇ibatis+spring+jsf 集成的稿子

找了一篇ibatis+spring+jsf 集成的文章
http://mail-archives.apache.org/mod_mbox/ibatis-user-java/200811.mbox/%3Cgei0hd$plj$1@ger.gmane.org%3E

From user-java-return-15506-apmail-ibatis-user-java-archive=ibatis.apache.org@ibatis.apache.org Sat Nov 01 16:36:49 2008
Return-Path: <user-java-return-15506-apmail-ibatis-user-java-archive=ibatis.apache.org@ibatis.apache.org>
Delivered-To: apmail-ibatis-user-java-archive@www.apache.org
Received: (qmail 39394 invoked from network); 1 Nov 2008 16:36:49 -0000
Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2)
  by minotaur.apache.org with SMTP; 1 Nov 2008 16:36:49 -0000
Received: (qmail 32133 invoked by uid 500); 1 Nov 2008 16:36:49 -0000
Delivered-To: apmail-ibatis-user-java-archive@ibatis.apache.org
Received: (qmail 32119 invoked by uid 500); 1 Nov 2008 16:36:48 -0000
Mailing-List: contact user-java-help@ibatis.apache.org; run by ezmlm
Precedence: bulk
List-Help: <mailto:user-java-help@ibatis.apache.org>
List-Unsubscribe: <mailto:user-java-unsubscribe@ibatis.apache.org>
List-Post: <mailto:user-java@ibatis.apache.org>
List-Id: <user-java.ibatis.apache.org>
Reply-To: user-java@ibatis.apache.org
Delivered-To: mailing list user-java@ibatis.apache.org
Received: (qmail 32108 invoked by uid 99); 1 Nov 2008 16:36:48 -0000
Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136)
    by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 01 Nov 2008 09:36:48 -0700
X-ASF-Spam-Status: No, hits=-1.0 required=10.0
tests=RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,SPF_PASS
X-Spam-Check-By: apache.org
Received-SPF: pass (athena.apache.org: domain of gcjiu-ibatis-user-java@m.gmane.org designates 80.91.229.2 as permitted sender)
Received: from [80.91.229.2] (HELO ciao.gmane.org) (80.91.229.2)
    by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 01 Nov 2008 16:35:30 +0000
Received: from list by ciao.gmane.org with local (Exim 4.43)
id 1KwJSK-0006VX-Jv
for user-java@ibatis.apache.org; Sat, 01 Nov 2008 16:36:08 +0000
Received: from e176190046.adsl.alicedsl.de ([85.176.190.46])
        by main.gmane.org with esmtp (Gmexim 0.1 (Debian))
        id 1AlnuQ-0007hv-00
        for <user-java@ibatis.apache.org>; Sat, 01 Nov 2008 16:36:08 +0000
Received: from andreas.niemeyer by e176190046.adsl.alicedsl.de with local (Gmexim 0.1 (Debian))
        id 1AlnuQ-0007hv-00
        for <user-java@ibatis.apache.org>; Sat, 01 Nov 2008 16:36:08 +0000
X-Injected-Via-Gmane: http://gmane.org/
To: user-java@ibatis.apache.org
From:  Andreas Niemeyer <andreas.niemeyer@gutzmann.com>
Subject:  Re: JSF configuration with Spring?
Date:  Sat, 01 Nov 2008 17:35:57 +0100
Lines: 276
Message-ID: <gei0hd$plj$1@ger.gmane.org>
References:  <geer6d$54s$1@ger.gmane.org>
Mime-Version:  1.0
Content-Type:  text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding:  7bit
X-Complaints-To: usenet@ger.gmane.org
X-Gmane-NNTP-Posting-Host: e176190046.adsl.alicedsl.de
User-Agent: Thunderbird 2.0.0.17 (Macintosh/20080914)
In-Reply-To: <geer6d$54s$1@ger.gmane.org>
Sender: news <news@ger.gmane.org>
X-Virus-Checked: Checked by ClamAV on apache.org

Hi,

I got everything working now after a few hours investigation.


The motivation was to get a basic setup working within a portlet AND a
servlet environment with a simple implemented service layer in context
of JSF.


The promised simplicity of iBatis seemed to be ideal for the
requirements: getting a loosly coupled DAO service, model undriven and
flexible in configuration, extensibility and usage. The excellent SPRING
  support was not least the key between everything.


Therefore a small summerize at this point how to glue JSF with Spring
and iBatis.


Some links helped me:

http://ibatisnet.sourceforge.net/DevGuide.html
http://www.developersbook.com/ibatis/iBatis-tutorials/iBatis-tutorials.php
http://forum.springframework.org/showthread.php?t=19032&highlight=iBatis
http://opensource.atlassian.com/confluence/spring/display/JSR168/Home
http://svn.apache.org/repos/asf/ibatis/trunk/java/ibatis-2/ibatis-2-docs/en/iBATIS-SqlMaps-2-Tutorial_en.pdf
http://svn.apache.org/repos/asf/ibatis/trunk/java/ibatis-2/ibatis-2-docs/en/iBATIS-SqlMaps-2_en.pdf


The configuration based on facelets 1.1.14, myfaces 1.1.6, ibatis
2.3.4.726, spring-framework 2.5.5 (spring.jar, spring-webmvc.jar,
spring-webmvc-portlet.jar), the portal environment is liferay x.


Only the relevant fragments are listed below portal type is independent.

The approach was to simply call a db sequence as a first step, no
classes of properties or resultsets mapped here. (shouldn't be a problem
  anymore)


Kind regards,
Andreas


1) faces-config.xml

...

<application>
<view-handler>com.sun.facelets.FaceletPortletViewHandler</view-handler> <variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>

</application>

...


2) web.xml

...

<context-param>
  <param-name>javax.faces.application.CONFIG_FILES</param-name>
  <param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>

<context-param>
  <param-name>contextConfigLocation</param-name>
   <param-value>
    /WEB-INF/classes/applicationContext.xml
  </param-value>
</context-param>

...

<listener>   
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>   
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

...


3) applicationContext.xml

...

<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/XXX"/>
</bean>

<bean id="sqlMap"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="WEB-INF/classes/sqlmap-config.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>

<bean id="sequencesBean"
         class="com.company.portlets.xxx.galery.dao.SequenceServiceImpl">
         <property name="sqlMapClient" ref="sqlMap" />
</bean>

...

4) META-INF/context.xml

...

<Resource
        name="jdbc/XXX"
         auth="Container"
         type="javax.sql.DataSource"
         driverClassName="org.postgresql.Driver"

url="jdbc:postgresql://localhost:5432/xxx?useUnicode=true&amp;characterEncoding=UTF-8"
         username="foo"
         password="foopass"
         maxActive="100"
         maxIdle="30"
         maxWait="10000"
   />

...

5) sqlmap-config.xml

...

<sqlMapConfig>
     <settings useStatementNamespaces="true"/>
     <sqlMap resource="ibatis-sql/sequences.xml"/>
</sqlMapConfig>

...

6) sequences.xml

...

<sqlMap namespace="sequences">
   <select id="getUploadSequence" resultClass="long">
     select nextval('galery_s_upload_id')
   </select>
</sqlMap>

...

7) ISequenceService class

package com.company.portlets.xxx.galery.dao;

public interface ISequenceService {
public long getUploadSequence() throws Exception ;
}


8) SequenceServiceImpl class

package com.company.portlets.xxx.galery.dao;

import com.ibatis.sqlmap.client.SqlMapClient;

public class SequenceServiceImpl implements ISequenceService {

protected SqlMapClient sqlMap = null;

     public void setSqlMapClient(SqlMapClient sqlMap) {
         this.sqlMap = sqlMap;
     }

     public long getUploadSequence() throws Exception {
         return (Long)sqlMap.queryForObject("sequences.getUploadSequence");
     }
}


9) a JSF bean class

...

import org.springframework.context.ApplicationContext;
import com.company.portlets.xxx.galery.dao.ISequenceService;

...

public long getServletuploadid() {

  ...
  long id_seq;

  ApplicationContext ctx = PortletJSFUtil.getApplicationContext();
  ISequenceService service = (ISequenceService)
    ctx.getBean("sequencesBean");
  try {
    id_seq = service.getUploadSequence();
  } catch (Exception e) {
   pLogger.error(e.getMessage());
  ...
}


}

10) a PortletJSFUtil class

...

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import org.springframework.context.ApplicationContext;
import
org.springframework.web.portlet.context.PortletApplicationContextUtils;

...


private static ExternalContext getExternalContext() {
  return FacesContext.getCurrentInstance().getExternalContext();
}

public static ApplicationContext getApplicationContext() {
return
PortletApplicationContextUtils.getRequiredWebApplicationContext((PortletContext)
getExternalContext().getContext());
}

...

11) in a servlet class

...

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.company.portlets.xxx.galery.dao.ISequenceService;
...

WebApplicationContext ctx =
WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
ISequenceService service=(ISequenceService)ctx.getBean("sequencesBean");

long id_seq = -1;

try {
  id_seq = service.getUploadSequence();
} catch (Exception e) {
  pLogger.error(e.toString());
}

...




Andreas Niemeyer schrieb:
> Hi,
>
> Does someone knows a basic example for this combination?
>
> I found one tiny example which is a bit incomplete. I guess for Spring a
> DelegatingVariableResolver is required for the managed beans.
>
> http://ffzhuang.blogspot.com/2007/04/jsf-spring-ibatis-integrate.html
>
> Thank you!
>
>
> Kind regards,
> Andreas
>
>