I tried my hands on it see how fast it helpful to create quick simple login application
This is my flow on my mind it starts with table in mysql
mysql> show columns from user;
+-------------------------+--------------- +------+-----
| Field | Type | Null | Key
+-------------------------+--------------- +------+-----
| UID | bigint(20) | NO | PRI
| USERNAME | varchar(255) | YES |
| USERPWD | varchar(255) | YES |
So I open the oepe and create an EAR (JSFEAR) followed by an EJB project in it
Add a user library named eclipselink into it , this should have two jar’s
$WEBLOGIC_HOME/modules/org.eclipse.persistence_*.jar(comes with weblogic installation)
eclipselink.jar (download from eclipse plugin site)
Now let us start creating the bean
The JPA Entity Bean
package com.model;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity()
public class User implements Serializable{
protected long uid;
protected String userName;
protected String userPwd;
@Id
public long getUid() {
return uid;
}
public void setUid(long uid) {
this.uid = uid;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
}
Persistance,xml
xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="ItemPu" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProviderprovider>
<jta-data-source>mysqldsjta-data-source>
<class>com.model.Userclass>
<properties>
<property name="eclipselink.target-server" value="WebLogic_10"/>
<property name="eclipselink.logging.level" value="FINEST"/>
<property name="eclipselink.ddl-generation" value="create-tables–create" />
properties>
persistence-unit>
persistence>
Note here I have used a jta-data-source mysqlds this has to be defined in the weblgoic via admin console
Now the session bean
package com.ssn;
import java.io.Serializable;
import javax.ejb.Remote;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import com.model.User;
@Stateful(mappedName = "UBean")
@Remote
public class UBean implements IUManager, Serializable {
@PersistenceContext(unitName="ItemPu",type=PersistenceContextType.TRANSACTION)
private EntityManager em;
public void addUser(User user) throws Exception {
em.persist(user);
}
}
And a interface for that
package com.ssn;
import com.model.User;
public interface IUManager {
public void addUser(User user) throws Exception;
}
Once you have you have your bean ready…
Now let us create another jsf web project under the same ear the structure of your application will be like this
JSFEAR
HELLOEJB
HELLOJSF
Jsf flow is have a greeting page which has a submit button if success will show a greeting page for a failure will go to a error page
Now the task is to inject the stateless session bean into our jsf , now the way to do this is via listener class since the beans or visible only to servlets (AFAIK)
Listener class
package svlt;
import javax.ejb.EJB;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.ssn.IUManager;
/**
* Servlet implementation class InitializeEJBReferences
*/
public class InitializeEJBReferences implements ServletContextListener {
private ServletContext context = null;
@EJB private IUManager IUManager_1;
public void contextInitialized(ServletContextEvent event) {
System.out.println("contextInitialized for bean");
context = event.getServletContext();
context.setAttribute("IUFacade", IUManager_1 );
}
public void contextDestroyed(ServletContextEvent event) {
context = event.getServletContext();
}
}
Reference in web.xml
<listener>
<listener-class>svlt.InitializeEJBReferenceslistener-class>
listener>
We will retrieve this attribute in the managed bean
Managed Bean
package foo;
import javax.servlet.ServletContext;
import com.ssn.IUManager;
import com.model.User;
public class UMBean {
protected String userName;
protected String userPwd;
private IUManager IUManager_1;
public UMBean(){
javax.faces.context.ExternalContext ex = javax.faces.context.FacesContext.getCurrentInstance().getExternalContext();
ServletContext sc = (ServletContext)ex.getContext();
IUManager_1 = (IUManager)sc.getAttribute("IUFacade");
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
public String loginAction() throws Exception {
String action = null;
try {
if ( IUManager_1.checkUser(userName,userPwd) )
action = "greeting";
else
action = "nogreeting";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return action;
}
public String registerAction() throws Exception {
String action = null;
try {
User user = new User();
user.setUserName(userName);
user.setUserPwd(userPwd);
IUManager_1.addUser(user) ;
action = "greeting";
} catch (Exception e) {
action = "nogreeting";
// TODO Auto-generated catch block
e.printStackTrace();
}
return action;
}
}
Faces-config.xml
xml version="1.0" encoding="UTF-8"?>
DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
"http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
<faces-config>
<managed-bean>
<managed-bean-name>UMBeanmanaged-bean-name>
<managed-bean-class>foo.UMBeanmanaged-bean-class>
<managed-bean-scope>requestmanaged-bean-scope>
managed-bean>
<navigation-rule>
<from-view-id>/register.jspfrom-view-id>
<navigation-case>
<from-outcome>greetingfrom-outcome>
<to-view-id>/greeting.jspto-view-id>
navigation-case>
<navigation-case>
<from-outcome>nogreetingfrom-outcome>
<to-view-id>/error.jspto-view-id>
navigation-case>
navigation-rule>
faces-config>
JSF Pages
Retister.jsp
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<f:loadBundle basename="foo.messages" var="msg"/>
<html>
<head>
<title>enter your name pagetitle>
head>
<body>
<f:view>
<h1>
<h:outputText value="#{msg.register_header}"/>
h1>
<h:form id="helloForm">
<h:outputText value="#{msg.username}"/>
<h:inputText value="#{UMBean.userName}" />
<h:outputText value="#{msg.userpwd}"/>
<h:inputText value="#{UMBean.userPwd}" />
<h:commandButton action="#{UMBean.registerAction}" value="#{msg.button_text}" />
h:form>
f:view>
body>
html>
Greeting.jsp
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<f:loadBundle basename="foo.messages" var="msg"/>
<html>
<head>
<title>greeting pagetitle>
head>
<body>
<f:view>
<h3>
<h:outputText value="#{msg.greeting_text}" />,
<h:outputText value="#{UMBean.userName}" />
<h:outputText value="#{msg.sign}" />
h3>
f:view>
body>
html>
error.jsp
<html>
<head>
<title>jsf-logintitle>
head>
<body>
<h2>Error!h2>
The user-id invalid. Please try again.
body>
html>
Okie now we are all set , create a server in the ide pointing to wls server which has data source configured and see if it works
This is not a great articulated blog the intent is to help developers to save their google time for source code email to
No comments:
Post a Comment