Intro
In this demo, we are using JSF1.2+EJB1.2, this demo is base of the article "
EJB files of EJB1.2
", here we mian show how to use JSF's managed-bean and jsf tag to call ejbs, this demo include two case, one is use javabean to return your name, and show by jsf tag, other one will call ejbs directly, ejb comp which do some job and give a return, then jsf get it and show us, here ejb comp biz is super simple, not do something with DB, and return your enter from Mbean directly.
The ejbs work with DB demo, I will show next time.
Mian Code
JSF Project
faces-config.xml
<?
xml
version
="1.0"
encoding
="UTF-8"
?>
< faces-config version ="1.2" xmlns ="http://java.sun.com/xml/ns/javaee"
xmlns:xi ="http://www.w3.org/2001/XInclude"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd" >
<!-- managed-beans -->
< managed-bean >
< managed-bean-name >user </ managed-bean-name >
< managed-bean-class >jsf.bean.UserBean </ managed-bean-class >
< managed-bean-scope >session </ managed-bean-scope >
</ managed-bean >
< managed-bean >
< managed-bean-name >loginMean </ managed-bean-name >
< managed-bean-class >jsf.bean.LoginMBean </ managed-bean-class >
< managed-bean-scope >session </ managed-bean-scope >
</ managed-bean >
<!-- navigation-rule -->
< navigation-rule >
< from-view-id >/index.jsp </ from-view-id >
< navigation-case >
< from-outcome >login </ from-outcome >
< to-view-id >/loginOk.jsp </ to-view-id >
</ navigation-case >
</ navigation-rule >
</ faces-config >
< faces-config version ="1.2" xmlns ="http://java.sun.com/xml/ns/javaee"
xmlns:xi ="http://www.w3.org/2001/XInclude"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd" >
<!-- managed-beans -->
< managed-bean >
< managed-bean-name >user </ managed-bean-name >
< managed-bean-class >jsf.bean.UserBean </ managed-bean-class >
< managed-bean-scope >session </ managed-bean-scope >
</ managed-bean >
< managed-bean >
< managed-bean-name >loginMean </ managed-bean-name >
< managed-bean-class >jsf.bean.LoginMBean </ managed-bean-class >
< managed-bean-scope >session </ managed-bean-scope >
</ managed-bean >
<!-- navigation-rule -->
< navigation-rule >
< from-view-id >/index.jsp </ from-view-id >
< navigation-case >
< from-outcome >login </ from-outcome >
< to-view-id >/loginOk.jsp </ to-view-id >
</ navigation-case >
</ navigation-rule >
</ faces-config >
LoginMBean.java
/**
*
*/
package jsf.bean;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import ejbBeans.IUserAdminSessionHome;
import ejbBeans.IUserAdminSessionRemote;
/**
* @author daniel
*
*/
public class LoginMBean {
private String loginname;
private static IUserAdminSessionHome useradminhome = null;
public String getLoginname() {
setLoginNamebyEJB();
return loginname;
}
public void setLoginNamebyEJB(){
InitialContext ctx;
try {
ctx = new InitialContext();
useradminhome = (IUserAdminSessionHome) PortableRemoteObject.narrow(
ctx.lookup(IUserAdminSessionHome.JNDI_NAME), IUserAdminSessionHome.class);
IUserAdminSessionRemote adminsession = useradminhome.create();
loginname=adminsession.setString("Call EJB CMP");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CreateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
*
*/
package jsf.bean;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import ejbBeans.IUserAdminSessionHome;
import ejbBeans.IUserAdminSessionRemote;
/**
* @author daniel
*
*/
public class LoginMBean {
private String loginname;
private static IUserAdminSessionHome useradminhome = null;
public String getLoginname() {
setLoginNamebyEJB();
return loginname;
}
public void setLoginNamebyEJB(){
InitialContext ctx;
try {
ctx = new InitialContext();
useradminhome = (IUserAdminSessionHome) PortableRemoteObject.narrow(
ctx.lookup(IUserAdminSessionHome.JNDI_NAME), IUserAdminSessionHome.class);
IUserAdminSessionRemote adminsession = useradminhome.create();
loginname=adminsession.setString("Call EJB CMP");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CreateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
index.jsp
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@page contentType="text/html; charset=utf-8" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head><%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<title> 第一个JSF程序 </title>
</head>
<body>
<f:view>
<h:outputText value="含简单交互的登录例子" />
<h:form >
<h4>您的名字:</h4>
<h:inputText value="#{user.name}"/>
<h:commandButton action="login" value="提交"/>
</h:form>
<h:outputText value="直接读取EJB CMP获取的信息:#{loginMean.loginname}"/>
</f:view>
</body>
</html>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@page contentType="text/html; charset=utf-8" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head><%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<title> 第一个JSF程序 </title>
</head>
<body>
<f:view>
<h:outputText value="含简单交互的登录例子" />
<h:form >
<h4>您的名字:</h4>
<h:inputText value="#{user.name}"/>
<h:commandButton action="login" value="提交"/>
</h:form>
<h:outputText value="直接读取EJB CMP获取的信息:#{loginMean.loginname}"/>
</f:view>
</body>
</html>
EJB Project
This part your can see my article "
EJB files of EJB1.2
".
LocalUserAdminSessionBean.java
package ejbBeans;
import javax.ejb.CreateException;
import base.BaseSessionBean;
public class LocalUserAdminSessionBean extends BaseSessionBean{
/**
*
*/
private static final long serialVersionUID = 5028163905841527500L;
/**
* Default create for SessionBean.
*
* @throws CreateException if bean instance can't be created
* @see org.ejbca.core.model.log.Admin
*/
public void ejbCreate() throws CreateException {
}
public String setString(java.lang.String str ){
return "Do some job and return your enter str as:"+str;
}
}
import javax.ejb.CreateException;
import base.BaseSessionBean;
public class LocalUserAdminSessionBean extends BaseSessionBean{
/**
*
*/
private static final long serialVersionUID = 5028163905841527500L;
/**
* Default create for SessionBean.
*
* @throws CreateException if bean instance can't be created
* @see org.ejbca.core.model.log.Admin
*/
public void ejbCreate() throws CreateException {
}
public String setString(java.lang.String str ){
return "Do some job and return your enter str as:"+str;
}
}
Deploy
In here i deploy them as an EAR package, your can do like me, also your can deploy them separate.
Source Code DownLoad
附件:http://down.51cto.com/data/2353209
本文转自danni505 51CTO博客,原文链接:http://blog.51cto.com/danni505/171110,如需转载请自行联系原作者