JSP/Servlet - 익스프레션 언어 javabean의 프로퍼티 가져오기
자바 빈에 대한 설명은 다음 링크에 잘 나와있다.( 자바 빈이란?)
결국 다음 예제와 같이 어떠한 규약에 따른 클래스이다.
/***********************************
* *
* PersonBean.java *
* *
************************************/
public class PersonBean implements java.io.Serializable
{
private String name;
private boolean coding;
// 기본 생성자 (인자가 없는).
public PersonBean()
{
}
public String getName()
{
return this.name;
}
public void setName(String name)
{
this.name = name;
}
// Different semantics for a boolean field (is vs. get)
public boolean isCoding()
{
return this.coding;
}
public void setCoding(boolean coding)
{
this.coding = coding;
}
}
출처 : https://ko.wikipedia.org/wiki/%EC%9E%90%EB%B0%94%EB%B9%88%EC%A6%88
여기서 private 멤버 변수의 경우 프로퍼티(Property)라고 하는데 익스프레션 언어는 다음과 같이 이를 쉽게 접근 할 수 있다.
${자바빈객체.프로퍼티이름} 또는 ${자바빈객체["프로퍼티이름"]}
이번 예제를 생성하려면 일단 WEB-INF 아래에 classes라는 폴더를 만든다.
그 다음 mall이라는 폴더를 만들면 자동적으로 패키지가 생성되며 그 아래에 ProductInfo.java라는 클래스를 생성하면 된다.
jsp파일은 동일하므로 그냥 만들어서 작성하면 된다.
ProductInfo.java
package mall;
public class ProductInfo {
private String name;
private int value;
public ProductInfo(){
}
public void setName(String name)
{
this.name = name;
}
public String getName(){
return name;
}
public void setPrice(int price){
value = price;
}
public int getPrice(){
return value;
}
}
ProductInfo.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@page import="mall.ProductInfo" %>
<%
ProductInfo product = new ProductInfo();//자바빈 클래스 생성
product.setName("베스트셀러");//값 넣어줌
product.setPrice(10000);
request.setAttribute("PRODUCT", product);//애트리뷰트 설정
RequestDispatcher dispatcher =
request.getRequestDispatcher("ProductInfoView.jsp");
dispatcher.forward(request, response);//다음 페이지로 넘어감
%>
<!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>
</body>
</html>
ProductInfoView.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!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>
상품 : ${PRODUCT.name } <BR> <!-- 프로퍼티 이름만으로 접근 가능하다 -->
가격 : ${PRODUCT.price} <BR> <!-- 역시 프로퍼티 이름만으로 접근 가능!! -->
</body>
</html>
실행 화면
다음과 같이 프로퍼티 값이 제대로 출력됨을 알 수 있습니다.
'프로그래밍 > JSP Servlet' 카테고리의 다른 글
JSP/Servlet - 자바빈 관련 표준액션 사용 방법 (0) | 2016.01.17 |
---|---|
JSP/Servlet - <jsp:forward> 표준 액션의 사용 방법 (0) | 2016.01.14 |
JSP/Servlet - <jsp:include> 표준액션의 사용 방법 (0) | 2016.01.14 |
JSP/Servlet - 자바의 정적 메소드를 EL 함수로 등록 및 사용 (0) | 2016.01.13 |
JSP/Servlet - 익스프레션 언어 Hash Map 객체를 출력하기 (0) | 2016.01.11 |
JSP/Servlet - 익스프레션 언어 List 객체 사용 방법 (0) | 2016.01.10 |
JSP/Servlet - 익스프레션 언어 initParam 내장 객체 사용 방법 (0) | 2016.01.10 |
JSP/Servlet - 익스프레션 언어 cookie 내장 객체 사용 방법 (0) | 2016.01.10 |