Computer >> 컴퓨터 >  >> 프로그램 작성 >> 프로그램 작성

JSP에서 리소스 번들을 사용하는 방법은 무엇입니까?

<시간/>

태그는 지정된 번들을 모든 경계 사이에 발생하는태그 태그. 이렇게 하면 각 에 대한 리소스 번들을 지정할 필요가 없습니다. 태그.

예를 들어, 다음 두 블록은 동일한 출력을 생성합니다 -

<fmt:bundle basename = "com.tutorialspoint.Example">
   <fmt:message key = "count.one"/>
</fmt:bundle>
<fmt:bundle basename = "com.tutorialspoint.Example" prefix = "count.">
   <fmt:message key = "title"/>
</fmt:bundle>

속성

태그에는 다음과 같은 속성이 있습니다. -

에서 각 키 이름 앞에 추가할 값
속성 설명 필수 기본값
기본 이름 로드될 리소스 번들의 기본 이름을 지정합니다. 없음
접두어 하위 태그아니요 없음

예시

리소스 번들은 로케일별 개체를 포함합니다. 리소스 번들은 키/값을 포함합니다. 한 쌍. 프로그램에 로케일별 리소스가 필요한 경우 모든 로케일에 공통적인 모든 키를 유지하지만 로케일에 특정한 변환된 값을 가질 수 있습니다. 리소스 번들은 로케일에 특정 콘텐츠를 제공하는 데 도움이 됩니다.

Java 리소스 번들 파일에는 일련의 키-문자열 매핑이 포함되어 있습니다. . 우리가 집중하는 방법은 java.util.ListResourceBundle을 확장하는 컴파일된 Java 클래스를 만드는 것입니다. 수업. 이러한 클래스 파일을 컴파일하고 웹 응용 프로그램의 클래스 경로에서 사용할 수 있도록 해야 합니다.

다음과 같이 기본 리소스 번들을 정의합시다 -

package com.tutorialspoint;
import java.util.ListResourceBundle;
public class Example_En extends ListResourceBundle {
   public Object[][] getContents() {
      return contents;
   }
   static final Object[][] contents = {
      {"count.one", "One"},
      {"count.two", "Two"},
      {"count.three", "Three"},
   };
}

위의 클래스 Example.class를 컴파일해 보겠습니다. 웹 응용 프로그램의 CLASSPATH에서 사용할 수 있도록 합니다. 이제 다음 JSTL 태그를 사용하여 다음과 같이 세 개의 숫자를 표시할 수 있습니다. -

<%@ taglib uri = "https://java.sun.com/jsp/jstl/core" prefix = "c" %>
<%@ taglib uri = "https://java.sun.com/jsp/jstl/fmt" prefix = "fmt" %>
<html>
   <head>
      <title>JSTL fmt:bundle Tag</title>
   </head>
   <body>
      <fmt:bundle basename = "com.tutorialspoint.Example" prefix = "count.">
         <fmt:message key = "one"/><br/>
         <fmt:message key = "two"/><br/>
         <fmt:message key = "three"/><br/>
      </fmt:bundle>
   </body>
</html>

위의 코드는 다음 결과를 생성합니다 -

One
Two
Three

다음과 같이 접두사 없이 위의 예를 시도하십시오 -

<%@ taglib uri = "https://java.sun.com/jsp/jstl/core" prefix = "c" %>
<%@ taglib uri = "https://java.sun.com/jsp/jstl/fmt" prefix = "fmt" %>
<html>
   <head>
      <title>JSTL fmt:bundle Tag</title>
   </head>
   <body>
      <fmt:bundle basename = "com.tutorialspoint.Example">
         <fmt:message key = "count.one"/><br/>
         <fmt:message key = "count.two"/><br/>
         <fmt:message key = "count.three"/><br/>
      </fmt:bundle>
   </body>
</html>

위의 코드는 다음 결과를 생성합니다 -

One
Two
Three