JSF资源包,自定义消息示例教程
在我以前的文章中,我解释了如何通过添加标签,文本字段等HTML组件来开始使用JSF2和使用JSF View页面。
今天,我们将研究任何Web框架中最重要的组件之一,即Resource Bundles和自定义消息。
资源包
将UI标签,日期,状态消息和其他UI文本元素存储在单独的属性文件中,而不是在页面中对其进行硬编码的现象称为资源捆绑。
这是处理这类资源的最方便的方法,因为可以在属性文件中更改这些文本,而不用在以后修改xhtml文件。
创建和调用资源包涉及以下步骤
创建一个名为" resourcebundle.properties"的属性文件。
您可以从下面给出的项目结构图像中或者通过下载项目来检查属性文件的位置。
`resourcebundle.properties""
name= Car name: Id = Car Id: color = Color: model = Model: regno = Registration Number: clear = Reset Details
通过使用资源包显示标签和错误消息来创建JSF页面。
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.theitroad.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>
要呈现单个xhtml文件,请在NetBeans IDE中右键单击jsf/xhtml文件,然后单击"运行"。
或者,运行项目并将浏览器指向所需的xhtml页面。
请注意,我正在加载资源包文件,这与导入Java类类似。
现在创建一个名为" Car.java"的托管bean,如下所示:
package com.theitroad.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 = "";
}
}
完成这些更改后,运行项目以呈现JSF页面,其外观应如下图所示。
如您所见,标签是从属性文件中拾取的。
现在,如果我们要更改"颜色"标签以更清晰地显示"汽车颜色",我们只需要编辑" resourcebundle.properties"文件,比在xhtml文件中进行更改更容易。
当代码在生产中时,这具有明显的优势,我们不希望混入已经很好用的xhtml东西。
自定义消息
JSF中的自定义消息使用户可以方便地定义自己的错误消息。
默认情况下,JSF根据上下文提供一些标准错误消息,例如字段类型或者字段长度验证。
JSF字段验证是检查某些常见UI数据正确性方案的简便方法,而不是编写JavaScript代码甚至JSP/java验证。
我们将在其他地方讨论有关JSF默认错误消息的信息。
自定义消息会用更清晰的用户定义消息覆盖这些隐秘的默认消息。
在下面的示例中,让我们了解如何配置自己的错误消息。
创建一个名为customerror.html的JSF页面,如下所示:
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>
" validateLength"方法用于通过接受最小值和最大值来检查字符长度,在此处我们可以指定字符数,然后使用" h:message"标记来打印错误消息。
同样,'required =" true""表示该字段是必填字段,并且requiredMessage用于打印用户指定的消息。
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
现在,如果您想知道JSF如何知道其中可以找到我们的自定义错误消息,请在faces-config.xhtml文件中对其进行配置,如下所示。
faces-config.xhtml
<?xml version="1.0" encoding="UTF-8"?> <faces-config xmlns="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.theitroad.messages.resourcebundle </message-bundle> </application> </faces-config>
完成这些更改后,运行将显示以下输出的应用程序。
如我们所见,由于我们使用内置的JSF验证和属性文件来提取文本,因此不需要另外的代码来抛出自己的自定义错误消息。

