博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pluto.ctl_Apache Pluto,Portlet Bridge和JSF 2.0集成示例教程
阅读量:2532 次
发布时间:2019-05-11

本文共 21100 字,大约阅读时间需要 70 分钟。

pluto.ctl

In the previous  we clarified you how can we use a Portlets, JSPs & Servlets for creating an MVC architectual application that’s easy to maintain, easy to debug and easy to develop.

在先前的我们阐明了如何使用Portlet,JSP和Servlet创建易于维护,易于调试和易于开发的MVC体系结构应用程序。

But why we would waste a lot of time in developing such that orchestration while others were already did. JSF framework are MVC compliant framework, as its components contained the concept of Separation of concern (SoC).

但是为什么我们要花很多时间来开发这种编排,而其他编排却已经做了呢。 JSF框架是MVC兼容框架 ,因为其组件包含关注分离(SoC)的概念。

Facelets technology are used for handling the view purposes, its backing bean act as controllers and you can use its Expression Language for glue your views with the proposed models.

Facelets技术用于处理视图,它的支持bean充当控制器,您可以使用其Expression Language将您的视图与建议的模型粘合在一起。

It’s elegant framework as you can use it within your Portlets, that will make them JSF-based Portlets. For this purpose, Java Community (JC) has proposed two different JSR (Java Specification Request) to simplify integrating your Portals with the well-know JSF framework.

这是一个优雅的框架,您可以在Portlet中使用它,从而使它们成为基于JSF的Portlet 。 为此,Java Community(JC)提出了两种不同的JSR(Java Specification Request),以简化将Portal与知名的JSF框架集成的过程。

JSR-301 is a Portlet container specification that defines the required behavior of a JSR-168,286 (Portlet Specification) that proxies for JSF artifacts. Meanwhile, JSR-329 is for Portlet container 2.0 bridge for JavaServer Faces (JSF) 1.2 Specification that defines the required behavior for allowing the JSF Application/View to be accessed as a Java Portlet.

JSR-301是Portlet容器规范,它定义了代理JSF工件的JSR-168,286(Portlet规范)的必需行为。 同时, JSR-329用于JavaServer Faces(JSF)1.2规范的Portlet容器2.0桥,该规范定义了允许JSF Application / View作为Java Portlet访问的必需行为。

Until writing these lines, no specification has been released for JavaServer Faces 2.0, though the implementation is already provided. This tutorial will provide you detailed information that help you getting all these technologies integrated each together as they work efficiently.

在编写这些行之前,尽管已经提供了实现,但尚未发布JavaServer Faces 2.0的规范。 本教程将为您提供详细的信息,以帮助您将所有这些技术有效地结合在一起。

  • Any Portal (Portlet container 2.0 compliance) Like Apache Pluto.

    任何门户网站(符合Portlet容器2.0),例如Apache Pluto。
  • JSF 2.0 (Either by using MyFaces 2.0 or by using Reference Implementation JSF Mojarra 2.0).

    JSF 2.0(使用MyFaces 2.0或使用参考实现JSF Mojarra 2.0)。
  • Any Portlet Bridge that’s compliant with JSR-329 like MyFaces 2.0 Portlet Bridge.

    任何与JSR-329兼容的Portlet Bridge,例如MyFaces 2.0 Portlet Bridge。

Employee registration operation is the process for which the integration will be achieved, as no need for using of Standard Java Portlet, Java Server Pages and Servlet for doing so.

员工注册操作是实现集成的过程,因为不需要使用Standard Java Portlet,Java Server Pages和Servlet。

数据库设计和所需模型 (Database Design & Required Model)

For explaining purposes, it’s good for you to know what’s the form of the database table that we would use for retaining the registered employees.

为了说明目的,对您而言,我们将用来保留已注册员工的数据库表的形式对您有好处。

Employee.sql

Employee.sql

CREATE TABLE `employee` (  `EMP_ID` int(11) NOT NULL AUTO_INCREMENT,  `EMP_NAME` varchar(45) DEFAULT NULL,  `EMP_JOB` varchar(45) DEFAULT NULL,  `EMP_SALARY` int(11) DEFAULT NULL,  PRIMARY KEY (`EMP_ID`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

Employee entity (Model) that would be created according for employee Table figured above will be:

将根据上图的员工表创建的Employee实体(模型)为:

package com.journaldev.data;public class Employee { private String id; private String name; private String job; private String salary; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getSalary() { return salary; } public void setSalary(String salary) { this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getJob() { return job; } public void setJob(String job) { this.job = job; }}

设计图 (Design View)

The design view for the operation of employee registration is just as JSF framework has done, the process of bridging has eliminated the need of walking through Portlet container accompanies.

员工注册操作的设计视图就像JSF框架所做的那样,桥接过程消除了遍历Portlet容器伴随的需求。

The figured design depicts you for how the flow of execution have to proceed, it’s just like any JSF application you may be made in the past.

上图的设计描述了执行流程如何进行,就像您过去制作的任何JSF应用程序一样。

注册员工XHTML (Register Employee XHTML)

JSF 2.0 has used a Facelets as view technology instead of JSP that’s supported by JSF 1.x. Registration employee page would look like below:

JSF 2.0使用Facelets作为视图技术,而不是JSF 1.x支持的JSP。 注册员工页面如下所示:

register.xhtml

register.xhtml

	
Register Employee

Here’s detailed explanation for the code listed above:

这是上面列出的代码的详细说明:

  • Registration page has used the XHML format. Facelets has supported the XHML as a format of its view.

    注册页面已使用XHML格式。 Facelets支持XHML作为其视图格式。
  • The user need to fill in all required information fields like identity and salary, in case you’ve missed them out, an error message would be flagged. These error messages are shown by the JSF framework itself as we’ve used a <f:validateRegex/> and required attributes.

    用户需要填写所有必填信息字段,例如身份和薪水,以防万一您错过了它们,将标记一条错误消息。 这些错误消息由JSF框架本身显示,因为我们使用了<f:validateRegex />和必需的属性。
  • In case you’ve entered a Not Number value within identity or salary fields, a JSF error message would be displayed tell you that you’re entering an invalid value.

    如果您在身份或薪水字段中输入了“非数字”值,则会显示一条JSF错误消息,提示您输入的是无效值。
  • All fields are associated with their respective attributes against employee property that’s already defined in the RegisterEmployeeManagedBean. As so, no submitted values are extracted from the request object.

    所有字段都与各自的属性相关联,该属性与RegisterEmployeeManagedBean中已定义的雇员属性有关。 因此,不会从请求对象中提取任何提交的值。

注册员工托管的Bean (Register Employee Managed Bean)

As JSF promised, everything will get retained into your bean, no need for orchestrating a lot of components that you’ve used before for extracting the user submitted parameters as no need for handling much issues you may do in case you’ve decided to not use a JSF framework like messages, locales and so on.

正如JSF所承诺的那样,所有内容都将保留在您的bean中,无需编排以前提取用户提交的参数时使用过的许多组件,因为不需要处理很多事情,以防万一您决定不这样做。使用诸如消息,语言环境等的JSF框架。

package com.journaldev.beans;import java.io.IOException;import java.sql.SQLException;import javax.faces.application.FacesMessage;import javax.faces.bean.ManagedBean;import javax.faces.bean.SessionScoped;import javax.faces.context.FacesContext;import javax.faces.event.ActionEvent;import org.apache.log4j.Logger;import com.journaldev.dao.EmployeeDAO;import com.journaldev.data.Employee;@ManagedBean@SessionScopedpublic class RegisterEmployeeManagedBean {	private static Logger logger = Logger.getLogger(RegisterEmployeeManagedBean.class);	private Employee employee = new Employee();	public Employee getEmployee() {		return employee;	}	public void setEmployee(Employee employee) {		this.employee = employee;	}	public void registerListener(ActionEvent event){		try {			// Register Employee			this.employee = EmployeeDAO.getInstance().createEmployee(employee);			logger.debug("Employee Has Registered");			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,					"Employee has regisered successfully !",""));			// Reset employee			this.employee = new Employee();		} catch (IllegalAccessException | ClassNotFoundException | SQLException | IOException e) {			logger.debug("Registration Process Has Failed",e);			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,					"Unforunately!, employee hasn't registered cause to : "+e,""));		}	}}

Here’s detailed explanation for the bean code figured out:

这里是对得出的Bean代码的详细说明:

  • A plain old Java object (POJO) employee is used for binding purpose. In JSF, each component may be associated with a value provided by the Managed Bean. Once the form has been submitted, JSF framework has been started and the submitted values are transferred magically into the binded bean.

    一个普通的旧Java对象(PO​​JO)雇员用于绑定。 在JSF中,每个组件都可以与受管Bean提供的值相关联。 提交表单后,便会启动JSF框架,并将提交的值神奇地传输到绑定的bean中。
  • As we’ve JSF 2.0, no need for faces configuration file (faces-config.xml), and so, we used the newly added annotations for declaring the managed bean and its scope.

    在JSF 2.0中,不需要人脸配置文件(faces-config.xml),因此,我们使用了新添加的注释来声明托管bean及其作用域。
  • Remember as no JSR proposed for JSF 2.0, but it’s already implemented and you may get used in a lot of Portlet container that’s supported JSR-286.

    请记住,因为没有为JSF 2.0提出JSR提议,但是它已经实现,并且您可能会在许多受JSR-286支持的Portlet容器中使用它。
  • You may notice using of EmployeeDAO and ConnectionUtility, if you remember what we have discussed at the  where in, your good design lead you for easy-to-maintain code, easy-to-debug and easy-to-develop (i.e. Separation of concern). Look at now, you have the ability to re-use the code you made. Really !! nothing touched with these two utilities.

    您可能会注意到使用EmployeeDAO和ConnectionUtility,如果您还记得我们在“ 讨论的内容,那么良好的设计将引导您轻松维护代码,易于调试和易于实现。发展(即关注分离)。 现在看,您可以重用您编写的代码。 真的! 这两个实用程序没有任何关系。
  • All of these classes that are relevant for the database communication have been got isolated to be used later on by any presentation layer. So, you either use the JSF, default JSP/Servlet, normal Java Desktop application or you may wrap them up to be SOAP-based service or Restful-based service.

    与数据库通信相关的所有这些类均已隔离,以后可用于任何表示层。 因此,您可以使用JSF,默认的JSP / Servlet,普通的Java Desktop应用程序,也可以将它们包装为基于SOAP的服务或基于Restful的服务。
  • The user will get a confirmation messages either the registration process has been finished successfully or it’s being failed.

    注册过程已成功完成或失败,用户将收到一条确认消息。
  • In case the registration process has failed, the confirmation message would show you the main cause for this failure.

    如果注册过程失败,则确认消息将向您显示该失败的主要原因。

JSF-Portlet桥依赖性 (JSF-Portlet Bridge Dependencies)

As we’ve mentioned before in that the Portlet Bridge is just a specification, so a lot of vendors have implemented it. We will use the MyFaces 3.0.0-alpha version that’s provided for Portlet Container 2.0. Two dependencies listed below must be added into your Maven build file to get Portlet Bridge implementation involved and used.

正如我们之前提到的,Portlet Bridge只是一个规范,因此许多供应商已经实现了它。 我们将使用为Portlet Container 2.0提供的MyFaces 3.0.0-alpha版本。 必须将以下列出的两个依赖项添加到您的Maven构建文件中,才能涉及和使用Portlet Bridge实施。

MyFaces-PortletBridge

MyFaces-PortletBridge

org.apache.myfaces.portlet-bridge
portlet-bridge-api
3.0.0-alpha
org.apache.myfaces.portlet-bridge
portlet-bridge-impl
3.0.0-alpha

JSF实施和部署描述符 (JSF Implementation & Deployment Descriptor)

JSF is a standard specification, that’s mean different vendors have already provided their own implementation. However, this part of tutorial is very important one as we would show you how can use two different implementations for getting JSF implementation used and what’s the impact of that usage on the deployment descriptor file.

JSF是一个标准规范,这意味着不同的供应商已经提供了自己的实现。 但是,本教程的这一部分非常重要,因为我们将向您展示如何使用两种不同的实现来使用JSF实现,以及该用法对部署描述符文件的影响。

Let’s firstly choose the most standard JSF implementation, it’s for sure a JSF Mojarra 2.0. You must add these below dependencies into you own maven for make use of JSF Mojarra.

首先让我们选择最标准的JSF实现,可以肯定的是JSF Mojarra 2.0 。 您必须将以下这些依赖项添加到您自己的Maven中,才能使用JSF Mojarra。

JSF - Reference Implementation - Mojaraa Dependencies

JSF - Reference Implementation - Mojaraa Dependencies

com.sun.faces
jsf-impl
2.1.15
com.sun.faces
jsf-api
2.1.15

And so, your web.xml file should be like this:

因此,您的web.xml文件应如下所示:

web.xml

web.xml

Employee Registration
Faces Servlet
javax.faces.webapp.FacesServlet
1
Faces Servlet
*.xhtml
State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2
javax.faces.STATE_SAVING_METHOD
server
com.sun.faces.config.ConfigureListener

But let’s see now what are these dependencies required for making MyFaces 2.0 get used instead of Reference Implementation JSF Mojarra 2.0.

但是,现在让我们看看使用MyFaces 2.0代替Reference Implementation JSF Mojarra 2.0需要哪些依赖关系。

JSF - MyFaces Dependencies

JSF - MyFaces Dependencies

org.apache.myfaces.core
myfaces-api
2.1.15
org.apache.myfaces.core
myfaces-impl
2.1.15

And so, you must have the same entries listed above in the web.xml but with major change, in that the listener mentioned must be removed.

因此,您必须在web.xml中具有上面列出的相同条目,但要进行重大更改,因为必须删除提到的侦听器。

web.xml

web.xml

Employee Registration
Faces Servlet
javax.faces.webapp.FacesServlet
1
Faces Servlet
*.xhtml
State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2
javax.faces.STATE_SAVING_METHOD
server

完整的Maven构建文件 (Full Maven Build File)

Find below full maven build file that will be used for implementing a JSF/Mojarra Portlet.

在下面找到完整的maven构建文件,该文件将用于实现JSF / Mojarra Portlet。

pom.xml

pom.xml

4.0.0
com.journaldev
EmployeeRegistration-JSFBridge
war
0.0.1-SNAPSHOT
EmployeeRegistration-JSFBridge
https://maven.apache.org
D:/Apache Pluto/pluto-2.0.3/webapps
org.apache.portals
portlet-api_2.0_spec
1.0
provided
log4j
log4j
1.2.17
org.apache.pluto
pluto-taglib
1.1.7
mysql
mysql-connector-java
5.1.32
org.apache.myfaces.portlet-bridge
portlet-bridge-api
3.0.0-alpha
org.apache.myfaces.portlet-bridge
portlet-bridge-impl
3.0.0-alpha
com.sun.faces
jsf-impl
2.1.15
com.sun.faces
jsf-api
2.1.15
${project.artifactId}
org.apache.portals.pluto
maven-pluto-plugin
2.1.0-M3
generate-resources
assemble
maven-war-plugin
2.1.1
${project.build.directory}/pluto-resources/web.xml
maven-antrun-plugin
copy
integration-test
run
delete
clean
true
run
org.apache.maven.plugins
maven-compiler-plugin
3.1
1.7
1.7

员工DAO和ConnectionUtility (EmployeeDAO & ConnectionUtility)

Now, it’s time to make a fast look at the EmployeeDAO and ConnectionUtility classes. These classes are used to handle all of these issues that are relevant for Database connectivity.

现在,是时候快速查看EmployeeDAO和ConnectionUtility类了。 这些类用于处理所有与数据库连接有关的问题。

package com.journaldev.dao;import java.io.IOException;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;import com.journaldev.dao.utility.ConnectionUtility;import com.journaldev.data.Employee;public class EmployeeDAO {	public static EmployeeDAO employeeDAO = null;	private EmployeeDAO(){	}	public static EmployeeDAO getInstance(){		synchronized(EmployeeDAO.class){			if(employeeDAO == null){				employeeDAO = new EmployeeDAO();			}		}		return employeeDAO;	}	public Employee createEmployee(Employee employee) throws SQLException, IllegalAccessException, IOException, ClassNotFoundException{		// Get connection instance		Connection connection = ConnectionUtility.getInstance().getConnection();		// Create Prepared Statement		PreparedStatement query = connection.prepareStatement("INSERT INTO EMPLOYEE VALUES (?,?,?,?)");		// Set variables		query.setInt(1, Integer.parseInt(employee.getId()));		query.setString(2, employee.getName());		query.setString(3, employee.getJob());		query.setInt(4, Integer.parseInt(employee.getSalary()));		try {			// Execute			query.execute();			// Return employee instance			return employee;		}		catch(Exception e){			// Close statement			query.close();			// Close connection			connection.close();			// Throw another exception for notifying the Servlet			throw new SQLException(e);		}	}	public boolean deleteEmployee(Employee employee){		return false;	}	public boolean updateEmployee(Employee employee, int employeeId){		return false;	}}
package com.journaldev.dao.utility;import java.io.IOException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.util.Properties;public class ConnectionUtility {	private static ConnectionUtility connectionUtiliy = null;	private Connection connection = null;	private ConnectionUtility() {	}	public static ConnectionUtility getInstance() throws IOException, IllegalAccessException, SQLException, ClassNotFoundException{		// Synchronized against connectionUtility instance		synchronized(ConnectionUtility.class){			// Check whether the connectionUtility is null or not			if(connectionUtiliy == null){				// Create a properties instance				Properties properties = new Properties();				// Load properties from classpath				properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("connection.properties"));				// Set connection with connectionUtility				connectionUtiliy = new ConnectionUtility();				// Load driver class				Class.forName("com.mysql.jdbc.Driver");				// Create connection				connectionUtiliy.setConnection(DriverManager.getConnection("jdbc:mysql://localhost:3306/journaldev", properties));			}			return connectionUtiliy;		}	}	public Connection getConnection() throws ClassNotFoundException, SQLException, IOException {		if(connection.isClosed()){			// Create a properties instance			Properties properties = new Properties();			// Load properties from classpath			properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("connection.properties"));			// Load driver class			Class.forName("com.mysql.jdbc.Driver");			// Create connection			connectionUtiliy.setConnection(DriverManager.getConnection("jdbc:mysql://localhost:3306/journaldev", properties));		}		return connection;	}	public void setConnection(Connection connection) {		this.connection = connection;	}}

注册员工演示 (Register Employee Demo)

The demonstration starts by deploying your developed WAR into your Apache Pluto Portlet container 2.0. Log in into your Apache Pluto and navigating into JournalDev page that’s already implemented in the previous introduced tutorials.

演示首先将开发的WAR部署到Apache Pluto Portlet容器2.0中。 登录到您的Apache Pluto,然后导航到先前介绍的教程中已实现的JournalDev页面。

Let’s see how JSF served you in case you’ve entered an invalid values, for that just put a character values inside both of id and salary fields.

让我们看看在输入无效值的情况下JSF是如何为您服务的,因为您只需将一个字符值放在id和salary字段中即可。

But what’s happened if we’ve tried to register already registered employees.

但是,如果我们尝试注册已经注册的员工,会发生什么情况。

What’s happened if we register a new employee.

如果我们注册新员工会发生什么。

摘要 (Summary)

This tutorial is a unique one as it’s summarized the all required information for getting used a JSF-based application onto Portlet container. The all required configuration, dependencies, accompanies classes and much more, they are listed completely here. Contribute us by commenting below and find downloaded source code.

本教程是一个独特的教程,它总结了将基于JSF的应用程序用于Portlet容器所需的所有信息。 所有必需的配置,依赖项,类以及更多内容,都在此处完整列出。 通过在下面评论来贡献我们,并找到下载的源代码。

翻译自:

pluto.ctl

转载地址:http://vyqzd.baihongyu.com/

你可能感兴趣的文章
小D课堂 - 新版本微服务springcloud+Docker教程_4-02 微服务调用方式之ribbon实战 订单调用商品服务...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-03 高级篇幅之Ribbon负载均衡源码分析实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-06 Feign核心源码解读和服务调用方式ribbon和Feign选择...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-05 微服务调用方式之feign 实战 订单调用商品服务...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-02 Netflix开源组件断路器
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-01分布式核心知识之熔断、降级
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-04 feign结合hystrix断路器开发实战下...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-03 feign结合hystrix断路器开发实战上...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_6-01 微服务网关介绍和使用场景
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-05熔断降级服务异常报警通知
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_6-03 高级篇幅之zuul常用问题分析
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-08 断路器监控仪表参数
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_6-02 springcloud网关组件zuul
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-1.快速搭建SpringBoot项目,采用Eclipse...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_1-4.在线教育后台数据库设计...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-3.热部署在Eclipse和IDE里面的使用...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_1-3.在线教育站点需求分析和架构设计...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-4.后端项目分层分包及资源文件处理...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-2.快速搭建SpringBoot项目,采用IDEA...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-5.PageHelper分页插件使用
查看>>