jsf 自定义属性

In my earlier posts, I explained how to get started with JSF2 and work with JSF View pages by adding HTML components such as labels, text fields etc. Today we will look into one of the most important component of any web framework i.e Resource Bundles and Custom Messages.

在我以前的文章中,我解释了如何通过添加标签,文本字段等HTML组件来开始使用JSF2和使用JSF View页面 。今天,我们将研究任何Web框架中最重要的组件之一,即Resource Bundles和自定义消息。

资源包 (Resource Bundle)

The phenomenon of storing the UI labels, date, status messages and other UI textual elements in a separate properties file instead of hardcoding these in a page is called resource bundling. It is the most convenient way of handling these kinds of resources as one can change these texts in the properties file instead of tinkering the xhtml file at a later stage.

将UI标签,日期,状态消息和其他UI文本元素存储在单独的属性文件中,而不是在页面中对其进行硬编码的现象称为资源捆绑。 这是处理这类资源的最方便的方法,因为可以在属性文件中更改这些文本,而不是在以后修改xhtml文件。

Creating and invoking resource bundle involves the following steps

创建和调用资源包涉及以下步骤

Create a properties file called resourcebundle.properties. You can check the property file location from the project structure image given below or by downloading the project.

创建一个名为resourcebundle.properties的属性文件。 您可以从下面给出的项目结构图像中或通过下载项目来检查属性文件的位置。

resourcebundle.properties"

resourcebundle.properties"

name= Car name:
Id = Car Id:
color = Color:
model = Model:
regno = Registration Number:
clear = Reset Details

Create the JSF page by using the resource bundle to display the labels and error messages.

通过使用资源包显示标签和错误消息来创建JSF页面。

resourcebundle.xhtml

resourcebundle.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml"xmlns:h="https://java.sun.com/jsf/html"xmlns:c="https://java.sun.com/jsf/core">
<h:head><title>Resource Bundle</title>
</h:head>
<h:body><h3>Resource Bundle Example</h3><c:view><c:loadBundle basename="com.journaldev.messages.resourcebundle" var="res" /><h:form><h:panelGrid columns="3"><h:outputLabel value="#{res.name}"></h:outputLabel><h:inputText value="#{car.cname}" id="cname"></h:inputText><br /><br /><h:outputLabel value="#{res.Id}"></h:outputLabel><h:inputText value="#{car.id}" id="Id"></h:inputText><br /><br /><h:outputLabel value="#{res.color}"></h:outputLabel><h:inputText value="#{car.color}" id="color"></h:inputText><br /><br /><h:outputLabel value="#{res.model}"></h:outputLabel><h:inputText value="#{car.model}" id="model"></h:inputText><br /><br /><h:outputLabel value="#{res.regno}"></h:outputLabel><h:inputText value="#{car.regno}" id="regno"></h:inputText><br /><br /><h:commandButton action="#{car.clearAll()}" value="#{res.clear}"></h:commandButton></h:panelGrid></h:form></c:view></h:body>
</html>

To render the individual xhtml files, right click on the jsf/xhtml file in NetBeans IDE and click on Run. Or, run the project and point the browser to the desired xhtml page. Notice that I am loading the resource bundle file, it’s similar to importing a java class.

要呈现单个xhtml文件,请在NetBeans IDE中右键单击jsf / xhtml文件,然后单击“运行”。 或者,运行项目并将浏览器指向所需的xhtml页面。 请注意,我正在加载资源包文件,这与导入Java类类似。

Now create a managed bean named Car.java as

现在,创建一个名为Car.java的托管bean,如下所示:

package com.journaldev.jsf.beans;import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;@ManagedBean(name="car")
@SessionScoped
public class Car implements Serializable {private static final long serialVersionUID = 1409682048108139241L;private String cname;private String color;private String Id;private String model;private String regno;public Car() {}public Car(String cname, String color, String Id, String model, String regno) {this.cname = cname;this.color = color;this.Id = Id;this.model = model;this.regno = regno;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public String getCname() {System.out.println("car name is" + cname);return cname;}public void setCname(String cname) {this.cname = cname;}public String getRegno() {return regno;}public void setRegno(String regno) {this.regno = regno;}public String getModel() {return model;}public void setModel(String model) {this.model = model;}public String getId() {return Id;}public void setId(String Id) {this.Id = Id;}public void clearAll() {this.Id = "";this.cname = "";this.color = "";this.regno = "";this.model = "";}
}

Once done with these changes run the project to render the JSF page and it should look as shown below.

完成这些更改后,运行项目以呈现JSF页面,其外观应如下图所示。

As you can notice the labels are picked up from the properties file. Now if we want to change say ‘Color’ label to more clear ‘Car Color’, we just have to edit the resourcebundle.properties file which is easier than to change it in the xhtml file. This has clear advantages when the code is in production and we don’t want to meddle with the xhtml stuff which is already working very well.

如您所见,标签是从属性文件中拾取的。 现在,如果要更改“颜色”标签以更清晰地显示“汽车颜色” ,我们只需要编辑resourcebundle.properties文件,这比在xhtml文件中进行更改更容易。 当代码在生产中时,这具有明显的优势,我们不希望混入已经很好运行的xhtml东西。

自定义消息 (Custom Messages)

Custom messages in JSF allows users to define their own error messages conveniently. By default JSF provides some standard error messages depending on the context like say field type or field length validations. JSF field validations are an easier way of checking some common UI data correctness scenarios instead of writing JavaScript code or even JSP/java validations. We will discuss about the JSF default error messages elsewhere. Custom messages override these cryptic default messages with a more clear user defined one. Let’s understand how to configure our own error messages in the below example.

JSF中的自定义消息使用户可以方便地定义自己的错误消息。 默认情况下,JSF根据上下文提供一些标准错误消息,例如字段类型或字段长度验证。 JSF字段验证是检查某些常见UI数据正确性方案的简便方法,而不是编写JavaScript代码甚至JSP / java验证。 我们将在其他地方讨论有关JSF默认错误消息的信息。 自定义消息会用更清晰的用户定义消息覆盖这些隐秘的默认消息。 在下面的示例中,让我们了解如何配置自己的错误消息。

Create a JSF page named customerror.html as

创建一个名为customerror.html的JSF页面,如下所示:

customerror.html

customerror.html

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml"   xmlns:h="https://java.sun.com/jsf/html"xmlns:c="https://java.sun.com/jsf/core"
>
<h:body><h3>Custom messages</h3><h:form>
<h:panelGrid columns="3">
<h:outputLabel value="Car Id"></h:outputLabel>
<h:inputText value="#{car.id}" id="Id" required="true" requiredMessage="Car Id is mandatory"></h:inputText>
<br /><br />
<h:outputLabel value="Car Name"></h:outputLabel>
<h:inputText value="#{car.cname}" id="cname">
<c:validateLength minimum="5" maximum="10"  for="cname" id="cname"/>
</h:inputText>
<h:message for="cname" style="color: red"></h:message>
<h:commandButton action="#{car.id}" value="Submit"></h:commandButton>
<br /><br />
</h:panelGrid>
</h:form></h:body>
</html>

The validateLength method is used for checking the length of characters by accepting minimum and maximum where we can specify the number of characters and then the h:message tag is used to print the error message. Also the required="true" indicates that the field is mandatory and the requiredMessage is used to print the message as specified by the user.

validateLength方法用于通过接受最小值和最大值来检查字符长度,在此处我们可以指定字符数,然后使用h:message标记来打印错误消息。 同样, required="true"表示该字段是必填字段,并且requiredMessage用于打印用户指定的消息。

JSF framework has standard keys for length check minimum and maximum error message, we can override them by adding below properties to our resource bundle file.

JSF框架具有用于最小和最大错误消息长度检查的标准密钥,我们可以通过在资源束文件中添加以下属性来覆盖它们。

javax.faces.validator.LengthValidator.MINIMUM=Please enter a minimum of 5 characters
javax.faces.validator.LengthValidator.MAXIMUM=Maximum length is exceeded. Please enter a string below 10 characters

Now if you are wondering how JSF will know where to look for our custom defined error messages, it’s configured in faces-config.xhtml file as shown below.

现在,如果您想知道JSF如何知道在何处查找我们的自定义错误消息,它是在faces-config.xhtml文件中配置的,如下所示。

faces-config.xhtml

faces-config.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<faces-configxmlns="https://java.sun.com/xml/ns/javaee"xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"version="2.0"><application><message-bundle>com.journaldev.messages.resourcebundle</message-bundle></application>
</faces-config>

Once done with these changes run the application which displays the following output.

完成这些更改后,运行将显示以下输出的应用程序。

As we can see, there is no need of extra code to throw our own custom error messages since we are using built in JSF validations and the properties file to pick up the text.

如我们所见,由于我们使用内置的JSF验证和属性文件来提取文本,因此不需要额外的代码来抛出我们自己的自定义错误消息。

The project structure is as shown below:

项目结构如下图:

This post is all about handling JSF Resources and Custom Messages. We will be looking into JSF Error Messages in the coming tutorial. In the mean time, you can download the project from below link and play around with it to learn more.

这篇文章全部关于处理JSF资源和定制消息。 在接下来的教程中,我们将研究JSF错误消息。 同时,您可以从下面的链接下载该项目并进行试用以了解更多信息。

Download JSF Resource Bundle Project下载JSF资源捆绑项目

翻译自: https://www.journaldev.com/6694/jsf-resource-bundle-custom-messages-example-tutorial

jsf 自定义属性

jsf 自定义属性_JSF资源包,自定义消息示例教程相关推荐

  1. jsf 导航_JSF动作方法导航示例教程– from-action标记

    jsf 导航 Navigations can be handled in JSF by writing methods in the managed bean. These methods shoul ...

  2. jsf集成spring_JSF Spring Hibernate集成示例教程

    jsf集成spring Welcome to JSF Spring Hibernate Integration example tutorial. In our last tutorial, we s ...

  3. 我的世界javaui材质包怎么下载_我的世界材质包怎么用 资源包下载安装教程

    我的世界中的资源包和材质包其实是同一个类型的物品,资源包(Resource pack)系统为材质包系统的API代替物,允许玩家更深度地自定义自己的Minecraft体验.资源包允许玩家自定义音乐.音效 ...

  4. jsf tree组件_JSF表单组件示例教程

    jsf tree组件 JSF Form component is a collection of fields along with the data and submit functionality ...

  5. java jsf 入门_JSF入门、简单示例

    JSF入门 1. 什么是 Java Server Faces(jsf)?   JSF为JAVA的 Web应用用户界面的开发人员提供了标准的编程接口.丰富可扩展的UI组件库(一个核心的JSP标记库用来处 ...

  6. Java 对国际化 (i18n) 的支持资源包的调用

    Java 国际化 i18n 国际化信息也成为本地化信息,一般需要2个条件才能确定一个特定的国际化信息,分别是:"语言类型" 和 "国家/地区信息",Java 为 ...

  7. 小米4硬改教程_小米手环3美化/修改资源包教程(含加入二维码教程)

    小米手环3美化/修改资源包教程(含加入二维码教程) 注意:本教程侧重于讲刷资源包res,不过多的进行刷固件的讲解(并且不建议刷固件,容易砖,一般修改res已经可以达到你预期的效果了). 1.2更新 温 ...

  8. ZigBee 3.0实战教程-Silicon Labs EFR32+EmberZnet-2-02:资源包详解

    [源码.文档.软件.硬件.技术交流.技术支持,入口见文末] [所有相关IDE.SDK和例程源码均可从群文件免费获取,免安装,解压即用] 持续更新中,欢迎关注! 一.获取资源包 本教程使用的全部资源包, ...

  9. JSF Spring Hibernate集成示例教程

    JSF Spring Hibernate集成示例教程 欢迎使用JSF Spring Hibernate Integration示例教程.在上一篇教程中,我们了解了如何将JSF和Spring框架集成在一 ...

最新文章

  1. Erlang列表操作里面的变量绑定规则
  2. “月宫一号”总设计师刘红:把科幻做成科学
  3. Nginx学习之七-模块ngx_epoll_module详解(epoll机制在nginx中的实现)
  4. 深度学习行人重识别综述与展望
  5. kotlin_Kotlin弦
  6. 一网打尽 SCI、SCIE、SSCI 、EI等指标及影响因子查询
  7. 易飞erp postgre mysql_pgadmin 执行sql
  8. 32 《奇特的一生》 -豆瓣评分8.5
  9. 手把手教你安装 Fedora
  10. 微信小程序实战 (WXML:小程序版HTML)
  11. linux用vim修改字符串,阳光沙滩博客-Linux下 vi/vim的使用
  12. 疫情防控大课观后感4
  13. 教你用Axure RP 9 设计手机APP
  14. lol服务器维护得多长时间,lol维护时间
  15. 使用 scp 出现 not a regular file 错误
  16. 纸质文稿如何生成PDF
  17. layui弹窗点击确定后再执行下一步
  18. jpg格式转pdf格式转换器免费
  19. 全年财报背后:小米的命Redmi定
  20. 迷你资讯页面开发 iframe页面操作

热门文章

  1. Linux FastDFS 分布式文件系统安装
  2. Linux下测试PHP和MySQL是否正确安装
  3. java/android 做题中整理的碎片小贴士(15)
  4. 25个深度学习开源数据集
  5. 在.net core 2.0中生成exe文件
  6. 【mysql优化 2】索引条件下推优化
  7. @Scheduled(cron=) spring定时任务时间设置
  8. [导入]Nebula3学习笔记(5): IO系统
  9. 数据结构上机实践第二周项目1
  10. 【学习OpenCV4】图像像素数据类型的转换与归一