无意中看到JSF中的Managed Properties,不禁突发奇想: 用JSF能实现依赖注入吗?理论上是可以通的,于是开始了自己的尝试,只是写一个简单的Login页面.
首先,写一个IUserService的接口,接口中只有属性方法: getUsername,getPassword,setUsername,setPassword四个方法.代码如下:
package
net.moon.service;
public interface IUserService ... {
StringgetUsername();
StringgetPassword();
voidsetUsername(Stringusername);
voidsetPassword(Stringpassword);
}
public interface IUserService ... {
StringgetUsername();
StringgetPassword();
voidsetUsername(Stringusername);
voidsetPassword(Stringpassword);
}
用一个类实现该接口,代码如下:
package
net.moon.model;
import net.moon.service.IUserService;
public class UserInfo implements IUserService ... {
privateStringusername;
privateStringpassword;
/***//**
*@returntheusernmae
*/
publicStringgetUsername()...{
returnusername;
}
/***//**
*@paramusernmaetheusernmaetoset
*/
publicvoidsetUsername(Stringusernmae)...{
this.username=usernmae;
}
/***//**
*@returnthepassword
*/
publicStringgetPassword()...{
returnpassword;
}
/***//**
*@parampasswordthepasswordtoset
*/
publicvoidsetPassword(Stringpassword)...{
this.password=password;
}
}
import net.moon.service.IUserService;
public class UserInfo implements IUserService ... {
privateStringusername;
privateStringpassword;
/***//**
*@returntheusernmae
*/
publicStringgetUsername()...{
returnusername;
}
/***//**
*@paramusernmaetheusernmaetoset
*/
publicvoidsetUsername(Stringusernmae)...{
this.username=usernmae;
}
/***//**
*@returnthepassword
*/
publicStringgetPassword()...{
returnpassword;
}
/***//**
*@parampasswordthepasswordtoset
*/
publicvoidsetPassword(Stringpassword)...{
this.password=password;
}
}
写一个事务类,实现login功能,其中有一个类型为IUserService的域,代码如下:
package
net.moon.business;
import net.moon.service.IUserService;
public class UserBO ... {
IUserServiceuser;
publicStringlogin()@H_435_301@...{
Stringresult="Failed";
//System.out.println(result);
if(user.getUsername().equalsIgnoreCase("admin")
&&user.getPassword().equals("password"))...{
result="PASS";
}
returnresult;
}
/***//**
*@returntheuser
*/
publicIUserServicegetUser()...{
returnuser;
}
/***//**
*@paramusertheusertoset
*/
publicvoidsetUser(IUserServiceuser)...{
this.user=user;
}
}
import net.moon.service.IUserService;
public class UserBO ... {
IUserServiceuser;
publicStringlogin()@H_435_301@...{
Stringresult="Failed";
//System.out.println(result);
if(user.getUsername().equalsIgnoreCase("admin")
&&user.getPassword().equals("password"))...{
result="PASS";
}
returnresult;
}
/***//**
*@returntheuser
*/
publicIUserServicegetUser()...{
returnuser;
}
/***//**
*@paramusertheusertoset
*/
publicvoidsetUser(IUserServiceuser)...{
this.user=user;
}
}
注意该类中的属性方法setUser,其中的参数user作为IUserService的接口类型.然后就是用什么方法对user进行注入,这时候就要想到JSF中的Managed Properties.
首先在,faces-config中配置UserInfo类为MBean,代码如下:
<
managed-bean
>
< managed-bean-name > userInfo </ managed-bean-name >
< managed-bean-class > net.moon.model.UserInfo </ managed-bean-class >
< managed-bean-scope > session </ managed-bean-scope >
</ managed-bean >
< managed-bean-name > userInfo </ managed-bean-name >
< managed-bean-class > net.moon.model.UserInfo </ managed-bean-class >
< managed-bean-scope > session </ managed-bean-scope >
</ managed-bean >
然后将事务类UserBO也配置为MBean,代码如下:
<
managed-bean
>
< managed-bean-name > userBO </ managed-bean-name >
< managed-bean-class >
net.moon.business.UserBO
</ managed-bean-class >
< managed-bean-scope > request </ managed-bean-scope >
< managed-property >
< property-name > user </ property-name >
< property-class >
net.moon.service.IUserService
</ property-class >
< value > #{userInfo} </ value >
</ managed-property >
</ managed-bean >
< managed-bean-name > userBO </ managed-bean-name >
< managed-bean-class >
net.moon.business.UserBO
</ managed-bean-class >
< managed-bean-scope > request </ managed-bean-scope >
< managed-property >
< property-name > user </ property-name >
< property-class >
net.moon.service.IUserService
</ property-class >
< value > #{userInfo} </ value >
</ managed-property >
</ managed-bean >
注意蓝色显示部分,为userBO这个MBean配置了一个Managed Property,也就是要求JSF在实现userBO时,用userInfo这个MBean为其user这个域赋值,从而实现注入.
<%
...
@pagecontentType="text/html;charset=UTF-8"
%>
<% ... @tagliburi="http://java.sun.com/jsf/html"prefix="h" %>
<% ... @H_404_771@@tagliburi="http://java.sun.com/jsf/core"prefix="f" %>
< html >
< head >
< Meta http-equiv ="Content-Type" content ="text/html;charset=UTF-8" />
< title ></ title >
</ head >
< body >
< f:view >
< h:form >
< h:panelGrid border ="1" columns ="2" >
< h:outputText value ="UserName:" ></ h:outputText >
< h:inputText value ="#{userInfo.username}" ></ h:inputText >
< h:outputText value ="Password:" ></ h:outputText >
< h:inputText value ="#{userInfo.password}" ></ h:inputText >
</ h:panelGrid >
< h:commandButton value ="Login" action ="#{userBO.login}" ></ h:commandButton >
</ h:form >
</ f:view >
</ body >
</ html >
<% ... @tagliburi="http://java.sun.com/jsf/html"prefix="h" %>
<% ... @H_404_771@@tagliburi="http://java.sun.com/jsf/core"prefix="f" %>
< html >
< head >
< Meta http-equiv ="Content-Type" content ="text/html;charset=UTF-8" />
< title ></ title >
</ head >
< body >
< f:view >
< h:form >
< h:panelGrid border ="1" columns ="2" >
< h:outputText value ="UserName:" ></ h:outputText >
< h:inputText value ="#{userInfo.username}" ></ h:inputText >
< h:outputText value ="Password:" ></ h:outputText >
< h:inputText value ="#{userInfo.password}" ></ h:inputText >
</ h:panelGrid >
< h:commandButton value ="Login" action ="#{userBO.login}" ></ h:commandButton >
</ h:form >
</ f:view >
</ body >
</ html >
<%
...
@pagecontentType="text/html;charset=UTF-8"
%>
<% ... @tagliburi="http://java.sun.com/jsf/html"prefix="h" %>
<% ... @H_404_771@@tagliburi="http://java.sun.com/jsf/core"prefix="f" %>
< html >
< head >
< Meta http-equiv ="Content-Type" content ="text/html;charset=UTF-8" />
< title ></ title >
</ head >
< body >
< f:view >
< h:form >
< h:outputText value ="Welcome,#{userInfo.username}" ></ h:outputText >
</ h:form >
</ f:view >
</ body >
</ html >
<% ... @tagliburi="http://java.sun.com/jsf/html"prefix="h" %>
<% ... @H_404_771@@tagliburi="http://java.sun.com/jsf/core"prefix="f" %>
< html >
< head >
< Meta http-equiv ="Content-Type" content ="text/html;charset=UTF-8" />
< title ></ title >
</ head >
< body >
< f:view >
< h:form >
< h:outputText value ="Welcome,#{userInfo.username}" ></ h:outputText >
</ h:form >
</ f:view >
</ body >
</ html >
配置导航如下:
<
navigation-rule
>
< display-name > login </ display-name >
< from-view-id > /login.jsp </ from-view-id >
< navigation-case >
< from-action > #{userBO.login} </ from-action >
< from-outcome > PASS </ from-outcome >
< to-view-id > /welcome.jsp </ to-view-id >
</ navigation-case >
< navigation-case >
< from-action > #{userBO.login} </ from-action >
< from-outcome > Failed </ from-outcome >
< to-view-id > /login.jsp </ to-view-id >
</ navigation-case >
</ navigation-rule >
< display-name > login </ display-name >
< from-view-id > /login.jsp </ from-view-id >
< navigation-case >
< from-action > #{userBO.login} </ from-action >
< from-outcome > PASS </ from-outcome >
< to-view-id > /welcome.jsp </ to-view-id >
</ navigation-case >
< navigation-case >
< from-action > #{userBO.login} </ from-action >
< from-outcome > Failed </ from-outcome >
< to-view-id > /login.jsp </ to-view-id >
</ navigation-case >
</ navigation-rule >
以上代码在JDK 1.6,JSF 1.2,Tomcat 6.0下测试通过.