프로그래밍/JSP Servlet

JSP/Servlet – JSTL 프로퍼티 파일에 변수 포함하기

가카리 2016. 2. 10. 20:20
반응형

 

JSP/Servlet – JSTL 프로퍼티 파일에 변수를 사용하기

 

프로퍼티 파일의 데이터 값을 읽을 때 사용하는 <fmt:message>액션을 시작 태그와 끝 태그로 부리하고, 그 사이에 <fmt:param> 이라는 커스텀 액션을 사용하면

 

프로퍼티 파일에 나열된 순서에 따라 {0}, {1}.. 위치에 자동으로 대입됩니다.

    프로퍼티 파일

GREETING = {0}님 {1}번째 방문이시군요.

    

 

    JSP파일

    <fmt:message var="greeting" key="GREETING">

        <fmt:param>Gakari</fmt:param>

        <fmt:param>3</fmt:param>

    </fmt:message>

 

 

예제는 프로퍼티 파일 2개와 jsp파일 2개로 구성됩니다. Welcome.jsp파일에서 forward메소드를 이용해서 WelcomeView.jsp로 넘어갑니다.

 

Welcome.jsp

 

<%@ page language="java" contentType="text/html; charset=EUC-KR"

pageEncoding="EUC-KR"%>

<%

    request.setAttribute("ID", "Gakari");

    request.setAttribute("NUM", new Integer(3));

%>

<jsp:forward page="WelcomeView.jsp" />

 

 

WelcomeView.jsp

 

<%@ page language="java" contentType="text/html; charset=EUC-KR"

pageEncoding="EUC-KR"%>

<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<fmt:bundle basename="Intro"><!-- Intro 프로퍼티 파일 가져옴 -->

    <fmt:message var="title" key="TITLE" />

    <fmt:message var="greeting" key="GREETING" >

    <!-- Welcome에서 정의한 값을 가져와서 프로퍼티 GREETING 변수에 넣음 -->

        <fmt:param>${ID }</fmt:param>

        <fmt:param>${NUM }</fmt:param>

    </fmt:message>

</fmt:bundle>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">

<title>Insert title here</title>

</head>

<body>

    ${title } <BR>

    ${greeting} <BR>

</body>

</html>

 

 

실행 화면

한글 언어로 실행하면 다음과 같습니다.

영어로 실행하면 다음과 같습니다.

 

반응형