본문 바로가기

WEB_Programming/Struts

스트러츠 리소스 번들 이용방법

Struts: Resource Bundles in Action

원하는 에러메시지를 주어졌을때 그 메시지를 보여주고, 하드코딩하지 않고도 국제화를 지원할 수 있도록 쉬운 변경을 제공한다. (이것은 고객이 직접 사이트에 들어온경우 자동으로 변경된다.) 이러한 마술같은 일은 리소스 번들을 통해서 이루엊진다.

애플리케이션 속성

첫번째 /WEB-INF/classes/Application.properties를 지정한다.

errors.required={0} is required.

entercoupon.jsp.prompt.customer.name=Customer Name
entercoupon.jsp.prompt.customer.number=Customer Number

이것은 실제 파일의 한 부분에 대해서, 나는 많은 다른 태그와 에러메시지를 가지고 있다. 이것은 파일에 저장되어 있으며, 하드코딩 되어 있지 않다. 좋다. 이제 고객 이름과 고객 번호를 검사해서 입력하는 부분을 해 볼 것이다.

Struts config

이것은 매우 중요한 설정 부분이다. 이를 확인해 보기 바란다. /WEB-INF/struts-config.xml:

<message-resources parameter="ApplicationResources" null="false"/>

Action

The action is where all of the *cough* action takes place. Take a look at a part of our action:
 public ActionForward execute(

… snip …

ActionMessages messages = new ActionMessages();

MessageResources mResources =
MessageResources.getMessageResources( "ApplicationResources" );

boolean gotErrors = false;

if( ( null == customerName ) || ( 0 == customerName.length() ) ) {
gotErrors = true;
messages.add(
Constants.VALIDATION_RESULTS_KEY,
new ActionMessage(
"errors.required",
mResources.getMessage( "entercoupon.jsp.prompt.customer.name" )
)
);
}

if( ( null == customerNumber ) || ( 0 == customerNumber.length() ) ) {
gotErrors = true;
messages.add(
Constants.VALIDATION_RESULTS_KEY,
new ActionMessage(
"errors.required",
mResources.getMessage( "entercoupon.jsp.prompt.customer.number" )
)
);
}

if( gotErrors ) {
saveMessages( request, messages );
return ( mapping.findForward( "errors" ) );
}

…snip…
Explanation

첫번째 메시지와 리소스 번들을 획득한다. (파일명은 확장자를 제외한 이름과 같이 지정해 주면된다.) 그리고 몇개의 불리언 값을 지정했고 이는 에러가 발생하는 경우 에러 페이지로 이동하게 해 두었다. true라면 에러가 있다는 의미이다.

그리고 각 상황을 체크한다. (변수로 지정된 customerName/customerNumber은 추측컨데 폼페이지로 부터 넘어온 값일 것이다.) 만약 지정되어 있지 않으면 메시지를 생성할 것이다. Constants.VALIDATION_RESULT_KEY는 폼에서 에러 메시지가 어떤 것인지 보여주기 위해 사용된다. 실제 값은 "valresults"이다.

이부분은 흥미로운 부분이다.

  new ActionMessage(
"errors.required",
mResources.getMessage(
"entercoupon.jsp.prompt.customer.number"
)
)

"errors.required"메시지는 파일로부터 로딩되고, "entercoupon.jsp.prompt.customer.number"을 이용한다. 다음 메시지를 호출할 것이다.
errors.required={0} is required.

"{0}첫번째 파라미터 대체 명령어이다. 아마도 {1}, {2}, {3}과 같은 값을 원한다면 넣을 수 있을 것이다.

마지막으로 요청된 메시지를 저장하고, 에러 페이지로 이동시킬 것이다.

ErrorPage.jsp

 …snip…

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>

…snip…

<html:messages message="true" id="results" property="valresults">
<p><bean:write name="results" /></p>
</html:messages>

…snip…

이것은 에러 메시지를 표현하는 부분이다. 어떻게 이것이 동작하는지 속성을 "valresults"를 확인해 보고, bean:write 태그를 통해서 결과를 출력한다. html:message 아이디는 반드시 bean:write의 id태그와 동일해야 한다.

우리가 고객이름과 번호를 입력하지 않았다고 가정한다면 에러 페이지는 다음과 같은 내용이 포함된다.

  …snip…

<p>Customer Name is required.</p>
<p>Customer Number is required.</p>

…snip…

서로다른 언어를 위해서는 서로다른 언어로 작성된 속성파일이 있어야 한다. 컴퓨터는 다른 언어에 대해서 자동적으로 번역을 하지는 않는다는것을 알아야 한다.

또 한가지 재미있는 것은 유니코드를 이용하여 기술할 수 있다는 것이다.

printpick.jsp.prompt.sort.name=\u0627\u0644\u0627\u0633\u0645

You can use Richard Ishida’s Unicode Converter to make your task a bit easier. Arabic translation site. Babelfish.


* 한글 처리에 대해서 *

한글 처리의 경우 UTF-8형식의 파일을 작성하고 이를 출력해보면 한글이 깨어짐을 확인할 수 있다. 이때에는 다음과 같이 인코딩을 해 주어야 한다.

/kr/co/media/eunkong/product/resources/ApplicationResources.properties 파일

test.name=기도,로꾸만,길똥이,일찌매

-------------------------------------------------------------------------------------

MessageResources resource = MessageResources.getMessageResources("/kr/co/media/eunkong/product/resources/ApplicationResources");

System.out.println(new String(resource.getMessage("test.name").getBytes("ISO-8859-1"), "UTF-8"));
           

-------------------------------------------------------------------------------------