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

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-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.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错误消息。 同时,您可以从下面的链接下载该项目并进行试用以了解更多信息。

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

jsf 自定义属性

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值