[ JSP EL(Expression Language) 표현언어 ]
EL : Expression Language 표현언어로 표현식, 액션태그를 통해 출력하는 것을 좀더 편리하게 출력할 수 있도록 해준다.
예제를 보기 전에 프로젝트 전체에서 사용할 초기화 변수를 web.xml에 등록하고
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?xml version= "1.0" encoding= "UTF-8" ?> <web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "http://java.sun.com/xml/ns/javaee" xsi:schemaLocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id= "WebApp_ID" version= "3.0" > <display-name>ex4</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file> default .html</welcome-file> <welcome-file> default .htm</welcome-file> <welcome-file> default .jsp</welcome-file> </welcome-file-list> <!-- 프로젝트 초기화 파라미터 값 세팅 --> <context-param> <description>전체 프로젝트에 적용되는 초기화 파라미터</description> <param-name>contextParamName</param-name> <param-value>contextParamValue</param-value> </context-param> <!-- end of context-param --> </web-app> |
객체를 쉽게 가져올 수 있음을 테스트하기 위해 StudentDTO 빈즈를 만들어 놓고...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | package ex4.com.java.study; public class StudentDTO { /* [ 자바 빈즈 ] : 그냥 단순히 DTO, VO 개념임... StudentDTO 이게 DTO이고 jsp단에서 useBean이 객체 생성 setProperty, getProperty를 통해 값을 가져올 수 있음. 단, 네이밍 규칙은 지켜줘야 함 */ private String id; private String name; private String address; public String getId() { return id; } public void setId(String id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public String getAddress() { return address; } public void setAddress(String address) { this .address = address; } @Override public String toString() { return "StudentDTO [id=" + id + ", name=" + name + ", address=" + address + "]" ; } } |
본격적인 EL 예제)
ELPractice.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | <%@ page language= "java" contentType= "text/html; charset=UTF-8" pageEncoding= "UTF-8" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > <head> <meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" > <title>Insert title here</title> </head> EL 이란? : EL은 표현식, 액션태그 등을 대신해서 출력을 편리하게 해주는 표현 언어입니다. 사용 예제) 정수 : ${ 10 }<br/> 실수 : ${ 10.56 }<br/> 문자열 : ${ "abcd" }<br/> boolean : ${ true }<br/> 3 항연산자 : ${( 1 < 2 ) ? 2 : 1 }<br/> <jsp:useBean id= "studentDTO" class = "ex4.com.java.study.StudentDTO" ></jsp:useBean> <jsp:setProperty property= "id" value= "류큐규" name= "studentDTO" /> getProperty액션 태그를 통해 dto 필드변수 값 가져오기 : <jsp:getProperty property= "id" name= "studentDTO" /><br/> EL을 통해 간단하게 가져오기 : ${studentDTO.id}<br/> **[EL의 내장 객체]** - applicationScope : application 내장 객체로 프로젝트 전체에서 공유된다. - sessionScope : session 객체로 브라우저 마다 생성되고 공유된다. - pageScope : 현재 페이지에서만 공유되는 pageContext 객체이다. - requestScope : request 내장 객체 - param : request로 전달한 데이터를 request.getParameter( "name" )으로 가져오는 파라미터 값을 가져오는 객체 - paramValues : 체크박스처럼 다수의 값을 전달한 값을 가져올 때 사용하는 객체 - initParam : web.xml에서 설정한 초기화 변수 값을 가져올 때 사용하는 내장객체 <% application.setAttribute( "applicationName" , "applicationVal" ); session.setAttribute( "sessionName" , "sessionVal" ); pageContext.setAttribute( "pageName" , "pageVal" ); request.setAttribute( "requestName" , "requestVal" ); // 이건 request.getParameter로 가져오는 것과 다름 // 이건 request.getAttributes로 가져와야함 // 폼이나 get방식으로 쿼리에 붙여 전달하는게 getParameter고 EL로는 param 객체를 이용함 %> <form action= "ELPractice2.jsp" method= "GET" > 이름 : <input type= "text" name= "name" > <input type= "submit" value= "전송" /> </form> |
- ELPractice2.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <%@ page language= "java" contentType= "text/html; charset=UTF-8" pageEncoding= "UTF-8" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > <head> <meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" > <title>Insert title here</title> </head> application 값 : ${applicationScope.applicationName }<br/> session 값 : ${sessionScope.sessionName }<br/> page 값 : ${pageScope.pageName }<br/> request 값 : ${requestScope.requestName }<br/> <!-- request.setAttribute로 설정한걸 가져오는 것!! --> param 값 : ${param.name } <!-- form을 통해 넘긴 데이터를 가져온다. --> initParam 값 : ${initParam.contextParamName }<!-- web.xml에 설정한 전체 프로젝트의 초기화 파라미터 값을 가져옴 --> |
'스터디 > JSP' 카테고리의 다른 글
JSP FrontController 패턴, Command 패턴, RequestDispatcher를 이용한 forward에 대하여... (0) | 2017.08.20 |
---|---|
JSP JSTL(Java Standard Tag Liarary) (0) | 2017.08.18 |
JSP 파일업로드 (2) | 2017.08.16 |
JSP 커넥션 풀(Connection Pool) (0) | 2017.08.16 |
JSP DB(데이터베이스) (0) | 2017.08.14 |