JSF 简单的JSF实现依赖注入的例子
来源:互联网 发布:手机网络不好怎么回事 编辑:程序博客网 时间:2024/05/04 05:42
首先,写一个IUserService的接口,接口中只有属性方法: getUsername, getPassword, setUsername, setPassword四个方法.代码如下:
package net.moon.service;
public interface IUserService ...{
String getUsername();
String getPassword();
void setUsername(String username);
void setPassword(String password);
}
用一个类实现该接口,代码如下:
package net.moon.model;
import net.moon.service.IUserService;
public class UserInfo implements IUserService...{
private String username;
private String password;
/** *//**
* @return the usernmae
*/
public String getUsername() ...{
return username;
}
/** *//**
* @param usernmae the usernmae to set
*/
public void setUsername(String usernmae) ...{
this.username = usernmae;
}
/** *//**
* @return the password
*/
public String getPassword() ...{
return password;
}
/** *//**
* @param password the password to set
*/
public void setPassword(String password) ...{
this.password = password;
}
}
写一个事务类,实现login功能,其中有一个类型为IUserService的域,代码如下:
package net.moon.business;
import net.moon.service.IUserService;
public class UserBO ...{
IUserService user;
public String login()...{
String result = "FAILED";
//System.out.println(result);
if(user.getUsername().equalsIgnoreCase("admin")
&& user.getPassword().equals("password"))...{
result = "PASS";
}
return result;
}
/** *//**
* @return the user
*/
public IUserService getUser() ...{
return user;
}
/** *//**
* @param user the user to set
*/
public void setUser(IUserService user) ...{
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>
然后将事务类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>
为userBO这个MBean配置了一个Managed Property,也就是要求JSF在实现userBO时, 用userInfo这个MBean为其user这个域赋值,从而实现注入.
接下来就是页面的实现了,首先是login页面, 代码如下:
<%...@ page contentType="text/html; charset=UTF-8" %>
<%...@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%...@ taglib uri="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="User Name:"></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>
表示登录成功的页面welcome.jsp,代码如下:
<%...@ page contentType="text/html; charset=UTF-8" %>
<%...@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%...@ taglib uri="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>
- JSF 简单的JSF实现依赖注入的例子
- 简单的jsf实现
- 一个简单的JSF例子
- 一个简单的JSF例子
- 用JSF实现依赖注入
- EJB整合JSF简单的小例子
- jsf国际化的例子
- 【JSF小记】JSF实现简单的文件下载
- 利用MyEclipse实现一简单的JSF
- JSF入门的小例子
- 最简单的JSF程序
- EJB3+JSF的简单实例
- JSF简单的ajax事件
- 简单的自定义 JSF 标签
- 简单的Spring依赖注入例子~~
- 简单的Spring依赖注入例子~~
- Spring简单的依赖注入例子
- 简单的Spring依赖注入例子~~
- 控制Windows Mobile的LED之三:SN3101芯片驱动(内核和BOOTLOADER)
- XFree86配置文件分析
- POJ 3041 Asteroids 二分图匹配 匈牙利算法
- delphi dataset 遍历所有记录的模版过程
- 移植wifi无线网卡到mini2440上全过程
- JSF 简单的JSF实现依赖注入的例子
- 使用 Raphael js 和JSON 来实现具备保存功能的简单的仿QQ秀
- 视频:连接 Android应用和DataSnap服务-巴西 2010 Delphi 大会
- 做flash后退播放遇到问题的感想
- VS2005,VS2008,VS2010工程文件和解决方案的区别
- C字符串分割(strtok和strtok_r)
- Linux 用户(user)和用户组(group)管理概述
- 如何面试程序员?
- 如何在Windows 7中安装Oracle 10g