You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Andreas Niemeyer <an...@gutzmann.com> on 2008/11/01 17:35:57 UTC
Re: JSF configuration with Spring?
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&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
>
>