?
1. 通過EL表達(dá)式直接在頁面中使用Spring Bean。(這一點(diǎn)稱之為:SpringBean被當(dāng)作LiteBean直接使用)
由于AOM是和Spring緊密集成的,因此,當(dāng)我們要結(jié)合Spring使用的時候,只需要向工程中添加Spring 2.5的一些基本jar包,然后把operamasks-spring.jar這個jar添加到工程中就可以實(shí)現(xiàn)在xhtml或者jsp中通過EL表達(dá)式來訪問Spring Bean。在該方式中,SpringBean可以被注入到LiteBean中。
package com.vv.aom.numbertest; import java.util.Random; public class NumberBean { private int num; public int getNum() { num = new Random().nextInt(10); return num; } public void setNum(int num) { this.num = num; } } |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" > <beans> <bean id="numberBean" class="com.vv.aom.numbertest.NumberBean"/> </beans> |
<f:view xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:w="http://www.apusic.com/jsf/widget" xmlns:h="http://java.sun.com/jsf/html" xmlns:ajax="http://www.apusic.com/jsf/ajax" renderKitId="AJAX"> <h:form> <p>Generate a number between 0 to 10</p> <h:inputText value="#{numberBean.num}"/><br/> <h:outputText value="Generate" style="text-decoration:underline;cursor:pointer;"> <ajax:action event="onclick"/> </h:outputText> </h:form> </f:view> |
?
?
需要注意:如果你需要在頁面中通過EL表達(dá)式直接使用LiteBean的時候,需要注意你的所有路徑下不能出現(xiàn)兩個及以上的同名LiteBean,否則系統(tǒng)會自動為你選擇路徑最短的一個LiteBean。所以仍然推薦使用OperaMask的IoVC來使用LiteBean。
2. 在AOM中定義的LiteBean都可以當(dāng)作是Spring Bean被使用。 如果希望從Spring上下文中獲得一個LiteBean,則必須在Spring配置文件中配置下面的Bean:
<bean class="org.operamasks.faces.spring.ManagedBeanConfigurer"/> |
上面的 org.operamasks.faces.spring.ManagedBeanConfigurer是 AOM 默認(rèn)提供的一個 Spring Bean 定義,必須在Spring中配置此Bean,它的作用是:讓 AOM 中的 LiteBean 同樣能夠被 Spring 所感應(yīng)到。
如果你使用 Apusic 應(yīng)用服務(wù)器,我們還建議你正確配置 Apusic 應(yīng)用服務(wù)器的 TransactionManager:
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
???? <property name="transactionManagerName"> ?????? <value>java:/TransactionManager</value> ???? </property> </bean> |
這樣,我們在AOM中定義的LiteBean既可以通過applicationContext.getBean(“”)方法來得到,也可以在Spring配置文件中使用LiteBean。
(1)通過spring的applicationContext的getBean方法得到LiteBean:
WebApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(session.getServletContext());
NumberService mySpringBean = (NumberService) appContext.getBean("numberService");
out.print("The bean defined in Spring: " + mySpringBean);
out.print("<br/>");
Object myLiteBean = (Object) appContext.getBean("GenerateNumberBean");
out.print("The bean defined in AOM: " + myLiteBean); |
(2)通過spring-faces-config.xml配置文件使SpringBean和LiteBean協(xié)同工作,當(dāng)然如果要指定該配置文件的個性化位置和名稱,可以在web.xml中添加:
<context-param> ??? <param-name>spring.faces.ConfiguarionFiles</param-name> ??? <param-value>/WEB-INF/user-module.xml, /WEB-INF/role-module.xml</param-value> </context-param> |
之后在里面可以這樣定義:(完全與Spring IOC的配置無異)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> ? <beans> ??? <bean id="compositeBeanAdvice" class="demo.spring.bean.CompositeBeanAdvice"/> ??? <bean id="compositeBeanTarget" class="demo.spring.bean.CompositeBeanImpl" scope="session"> ??????? <property name="service" ref="numberService"/> ??????? <property name="numberBean" ref="GenerateNumberBean"/> ??? </bean> ? ??? <bean id="compositeBean" class="org.springframework.aop.framework.ProxyFactoryBean" scope="session"> ??????? <property name="proxyInterfaces" value="demo.spring.bean.CompositeBean"/> ??????? <property name="interceptorNames"> ??????? <list> ??????????? <value>compositeBeanAdvice</value> ??????? </list> ??????? </property> ??????? <property name="target" ref="compositeBeanTarget"/> ??? </bean> </beans> |
3. SpringBean在被當(dāng)作LiteBean使用的時候,依然可以獲得IoVC支持。這一點(diǎn)是通過在SpringBean中添加@Bind和@Action注解來實(shí)現(xiàn)的。例如:
SayHelloBean.java:
package com.vv.aom.numbertest;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.ManagedBean;
//@ManagedBean
public class SayHelloBean {
@Bind
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Action
public String sayHello() {
System.out.println("hello " + name);
return null;
}
@Override
public String toString() {
return "MySpringBean";
}
} |
sayHello.xhtml
<f:view xmlns=http://www.w3.org/1999/xhtml xmlns:f="http://java.sun.com/jsf/core"
?? xmlns:w=http://www.apusic.com/jsf/widget
xmlns:layout="http://www.apusic.com/jsf/layout" ?? renderKitId="AJAX"> ? <w:page title="spring"> ???? <w:form> ?????? <layout:panelGrid columns="2"> ???????? <w:textField id="name"/> ???????? <w:button id="sayHello"/> ?????? </layout:panelGrid> ???? </w:form> ? </w:page>
</f:view> |
Spring-bean.xml:
<bean id="SayHelloBean" class="com.vv.aom.numbertest.SayHelloBean">
<property name="name" value="Kevin"/>
</bean> |
文章來源:
http://x-spirit.spaces.live.com/Blog/cns!CC0B04AE126337C0!736.entry