Web programming learning 2: Using JPA and JSF to create Web Applications

Source: Internet
Author: User

Yesterday I made a small exercise of helloworld using JSF, which did not interact with backend data.

Today, I added JPA to persist data objects, and the front-end still uses JSF to present them and build a complete Web application.

This application is used to query the background database through JPA after the user enters the user name and password for verification. If the application succeeds, the welcome page is displayed.


Development Environment:

Eclipse Juno,

JSF2.1,

JPA framework: javasselink 2.4,

Database: MySql 5.5

Web Application Server: Tomcat 7.0


1. First install the MySQL database. database information:

Log on to the database and create a table:

Mysql> create table users (id integer primary key, name varchar (120), login varchar (75) not null, password varchar (75) not null );

Enter several pieces of test user information:



2. Create a Dynamic Web project called webusers and add JPA Facet.


3. Edit the persistence. xml file

Persistence Provider:
Org. eclipse. persistence. jpa. PersistenceProvider


And database information,


Complete persistence. xml:

<?xml version="1.0" encoding="UTF-8"?><persistence version="2.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_2_0.xsd"><persistence-unit name="webusers" transaction-type="RESOURCE_LOCAL"><provider>org.eclipse.persistence.jpa.PersistenceProvider</provider><class>logon.User</class><properties><property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:8889/test" /><property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /><property name="javax.persistence.jdbc.user" value="root" /><property name="javax.persistence.jdbc.password" value="root" /></properties></persistence-unit></persistence>


4. Create an Entity User

package logon;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;@Entity@Table(name = "USERS")public class User {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private int id;String Name = null;String Login = null;String Password = null;public int get_Id() {return id;}public void set_Id(int id) {this.id = id;}public String getName() {return Name;}public void setName(String Name) {this.Name = Name;}public String getLogin() {return Login;}public void setLogin(String Login) {this.Login = Login;}public String getPassword() {return Password;}public void setPassword(String Password) {this.Password = Password;}}


5. Add the JSF facet to the Project


6. Open the faces-config.xml and add a Session Bean: logonBean on the managedBean page


7. Create SessionBean class

package logon;import javax.persistence.EntityManager;import javax.persistence.EntityManagerFactory;import javax.persistence.Persistence;import javax.persistence.Query;public class LogonBean {private static final String PERSISTENCE_UNIT_NAME = "webusers";private static EntityManagerFactory factory;private String userName;private String password;public LogonBean() {}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String validate() {String flag = "failure";factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);EntityManager em = factory.createEntityManager();Query q = em.createQuery("SELECT u FROM User u WHERE u.Login = :login AND u.Password = :pass");q.setParameter("login", userName);q.setParameter("pass", password);try {User user = (User) q.getSingleResult();if (userName.equalsIgnoreCase(user.Login)&& password.equals(user.Password)) {flag = "success";}} catch (Exception e) {return null;}return flag;}}


8.create a page template, basictemplate.xhtml, footer.xhtml, and header.xhtml.


BasicTemplate.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Header.xhtml:

<?xml version="1.0" encoding="ISO-8859-1" ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Footer. xhmtl:

<?xml version="1.0" encoding="ISO-8859-1" ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


9.at the end, create a logonpage and a welcome.xhtml page.

Logon.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Welcome.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


10. Open the face-config.xml and create the page navigation.


11. I finally got it done! Run the test on the Tomcat server and enter the user information in the users table of the background database to log on.


Summary:

This small example is completed based on the previous exercise.

Using JPA to implement operations on data objects, and then displaying data through the JSF page template, the idea is very simple and clear.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.