>  기사  >  Java  >  JSF 변환기

JSF 변환기

WBOY
WBOY원래의
2024-08-30 15:14:11536검색

JSF(JavaServer Faces) 애플리케이션에서는 클라이언트 요청으로 http 요청을 사용하여 사용자 입력이 서버로 전송됩니다. 이러한 사용자 입력을 값이라고 합니다. 이러한 요청 값은 문자열 형식으로 서버에 전송됩니다. 그러나 JSF 애플리케이션은 int, float, double, String, Boolean, date 등과 같은 다양한 데이터 유형을 사용합니다. 따라서 요청 값을 처리하기 전에 적절한 데이터 유형으로 변환해야 합니다. 이 변환 프로세스를 변환이라고 합니다. 이번 주제에서는 JSF 변환기에 대해 알아보겠습니다.

광고 이 카테고리에서 인기 있는 강좌 JSF Java 서버 얼굴 - 학습 경로 | 6개 코스 시리즈

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

JSF 애플리케이션에서 변환을 수행하기 위해 JSF 프레임워크는 표준 변환기를 제공합니다. 이러한 변환기는 JSF의 핵심 태그 라이브러리에 태그로 제공됩니다. 또한 애플리케이션 요구 사항을 충족하기 위해 자체 변환기를 만들 수도 있습니다.  이러한 변환기를 사용자 정의 변환기라고 합니다.

변환기 태그

JSF 애플리케이션에서 사용자가 UI 구성 요소에 입력한 데이터는 애플리케이션에서 진행되기 전에 적절한 형식으로 변환되어야 합니다. 다음 표에는 변환 수행을 위해 JSF 코어 태그 라이브러리에서 제공하는 표준 태그가 나열되어 있습니다.

데이터 변환을 위한 JSF 핵심 태그 –

f:converter – 이 태그는 인스턴스 상위 구성요소에 임의 변환기를 추가하는 데 사용됩니다.

예시

<f:converter converterId = "javax.faces.Integer"/>

f:convertNumber – 이 태그는 상위 구성요소에 'NumberConverter' 인스턴스를 추가하는 데 사용됩니다.

예시

<c:convertNumber type = "javax.faces.Integer"/>

f:convertDateTime – 이 태그는 'DateTimeConverter' 인스턴스를 상위 구성요소에 추가하는 데 사용됩니다.

예시

<f:convertDateTime pattern = "dd/mm/yyyy"/>

JSF 프레임워크는 숫자와 날짜에 대한 표준 변환기를 제공합니다. 때로는 애플리케이션에서 사용자 입력을 숫자 및 날짜 데이터 유형으로 변환해야 하므로 JSF 프레임워크를 사용하여 표준 변환기를 제공할 수 있습니다. 모든 표준 변환기는 javax.faces.convert 패키지 JSF 프레임워크에 포함되어 있습니다. 모든 변환기는 구성 요소의 값을 기반으로 암시적으로 적용되며 이러한 변환기에 액세스하려면 변환기 ID로 액세스할 수 있습니다.

JSF 프레임워크 변환기

사용자 입력 문자열 값을 java.lang.Integer 유형의 값으로 변환하는 데 사용되는 IntegerConverter 클래스와 해당 변환기 ID는 javax.faces.Integer입니다.

<h:inputText id="age" converter="javax.faces.Integer" />

사용자 입력 문자열 값을 java.lang.BigInteger 유형의 값으로 변환하는 데 사용되는 BigIntegerConverter 클래스와 해당 변환기 ID는 javax.faces.BigInteger입니다.

<h:inputText id="age" converter="javax.faces.Integer" />

다른 정수 유형에도 동일한 방법을 사용할 수 있습니다.

사용자 입력 문자열 값을 java.lang.Short 유형의 값으로 변환하는 데 사용되는 ShortConverter 클래스와 해당 변환기 ID는 javax.faces.Short입니다.

사용자 입력 문자열 값을 java.lang.Short 유형의 값으로 변환하는 데 사용되는 LongConverter 클래스와 해당 변환기 ID는 javax.faces.Long입니다.

사용자 입력 문자열 값을 java.lang.Number 유형의 값으로 변환하는 데 사용하는 NumberConverter 클래스와 해당 변환기 ID는 javax.faces.Number입니다.

<h:outputText value="#{userBean.height}">
<f:convertNumber maxFractionDigits="2" />
</h:outputText>

사용자 입력 문자열 값을 java.lang.Float 유형의 값으로 변환하는 데 사용하는 FloatConverter 클래스와 해당 변환기 ID는 javax.faces.Float입니다.

사용자 입력 문자열 값을 java.lang으로 변환하는 데 사용하는 BigDecimalConverter 클래스입니다. BigDecimal 값 유형 및 해당 변환기 ID는 javax.faces.BigDecimal입니다.

사용자 입력 문자열 값을 java.lang으로 변환하는 데 사용하는 DoubleConverter 클래스입니다. Double 값 유형과 해당 변환기 ID는 javax.faces.Double입니다.

사용자 입력 문자열 값을 java.lang.Byte 유형의 값으로 변환하는 데 사용하는 ByteConverter 클래스와 해당 변환기 ID는 javax.faces.Byte입니다.

사용자 입력 문자열 값을 java.lang으로 변환하는 데 사용하는 CharacterConverter 클래스입니다. 값의 문자 유형과 해당 변환기 ID는 javax.faces.Character입니다.

사용자 입력 문자열 값을 java.lang.Boolean 유형의 값으로 변환하는 데 사용되는 BooleanConverter 클래스와 해당 변환기 ID는 javax.faces.Boolean입니다.

사용자 입력 문자열 값을 java.lang으로 변환하는 데 사용하는 DateTimeConverter 클래스입니다. DateTime 값 유형 및 해당 변환기 ID는 javax.faces.Datetime입니다.

The EnumConverter class which uses to convert user input string values into java.lang. Enum type of values and its conveter id is javax.faces.Enum.

1. convertDateTime  Tag

The JSF convertDateTime contains the following attributes to convert the Date time format.

  • dateStyle – This attribute specifies the formatting style for the date of a date string is to be formatted.
  • locale – This attribute specifies to represent a date in Locale format.
  • pattern – This attribute specifies the formatting pattern to be the format.
  • timeStyle – This attribute specifies the Predefined formatting style for the date of a date string is to be formatted.
  • timeZone – This attribute specifies the Time zone for the date of a date string is to be formatted.
  • type – This attribute specifies the date or/and time or both to be formatted.

Example

<h:inputText id="DOB" label = "Date of Birth" value="#{bean.DOB }">
<f:convertDateTime pattern="dd/mm/yyyy" />
</h:inputText>

2. convertNumber Tag

The JSF convertNumber contains the following attributes to convert Number format. currencyCode – This attribute specifies to apply the currency format.

  • currencySymbol – This attribute specifies to apply the currency format.
  • groupingUsed – This attribute specifies whether the formatted output has grouping separators or not.
  • integerOnly – This attribute specifies whether only an integer part format or not.
  • locale – This attribute specifies to represent the number in Locale format.
  • minFractionDigits – This attribute specifies the minimum number of digits in the fractional part.
  • maxFractionDigits – This attribute specifies the maximum number of digits in the fractional part.
  • minIntegerDigits – This attribute specifies the minimum number of digits in the integer part.
  • maxIntegerDigits – This attribute specifies the maximum number of digits in the integer part.
  • pattern – This attribute specifies the formatting pattern to be the format.
  • type – This attribute specifies whether the type of number, percent and currency.

Example

<h:outputText value = "#{bean.height}">
<f:convertNumber maxFractionDigits = "1" />
</h:outputText>

Let’s see an example of the JSF project.

Create index.xhtml with the following code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:form>
<h:outputLabel for="name">Name : </h:outputLabel>
<h:inputText id="name" value="#{Emp.name}"/><br/>
<h:outputLabel for="eid">Eid : </h:outputLabel>
<h:inputText id="eid" value="#{Emp.eid}">
<h:outputLabel for="sal">Salary : </h:outputLabel>
<h:inputText id="sal" value="#{Emp.sal}">
<f:converter converterId="javax.faces.Integer" />
</h:inputText><br/>
<h:commandButton action="disp.xhtml" value="Submit Query"/>
</h:form>
</html>

Create Emp.java class with the following code in the project.

package jsfp;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean
@RequestScoped
public class Emp {
String name;
String eid;
int sal;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEid() {
return eid;
}
public void setEid(String eid) {
this.eid = eid;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
}

Create disp.xhtml for the response with the following code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Reply Page</title>
</h:head>
<h:body>
<h:outputText value="Welcome #{Emp.name}. Your eid is #{Emp.eid}. Your Salary is #{Emp.sal}."/>
</h:body>
</html>

An output of the above project in the sequence is –

JSF 변환기

You fill the details as below –

JSF 변환기

Once you click the button the output is –

JSF 변환기

Conclusion

The user inputs are sent to the server using an http request in the form of the string, the request values to be processed first need to be transformed into the appropriate data types such as int, float, double, String, Boolean, date, and so on by using the JSF Converters of JSF framework.

위 내용은 JSF 변환기의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:J2EE 기술다음 기사:J2EE 기술