Form - action 태그
form Action Tag:
xml기반의 새로운 태그
Bean : 하나하나의 단일 객체
java에서는 javaBeans라고 해서 자바가 나타내고싶은 데이터를 객체형식으로 구성한다.
직접 클래스 파일로 다뤄보자.
java beans의 조건
1) private 멤버 필드
2) public getter/setter
3) 기본생성자
클래스파일 생성(객체)
<jsp:useBean id="ob" class="day05.Member"></jsp:useBean>
<h3>ob.name : <jsp:getProperty property="name" name="ob"/></h3>
<h3>ob.age : <jsp:getProperty property="age" name="ob"/></h3>
<jsp:setProperty property="name" name="ob" value="이지은"/>
<jsp:setProperty property="age" name="ob" value="31"/>
<h3>ob.name : <jsp:getProperty property="name" name="ob"/></h3>
<h3>ob.age : <jsp:getProperty property="age" name="ob"/></h3>
<%@ page import="day05.Member" %>
<%
out.print("<h4>" + ob.getName()+ " </h4>");
out.print("<h4>" + ob.getAge()+ " </h4>");
Member test = (Member)pageContext.getAttribute("ob");
out.print("<h5>" + ob.getName()+ " </h5>");
out.print("<h5>" + ob.getAge()+ " </h5>");
%>
<hr>
<h3>ob.name : ${ob.name}</h3>
<h3>ob.age : %{ob.age}</h3>
getProperty를 안쓰는 이유
<EL태그>
attribute의 데이터 저장 허용범위가 작은 순 -> 큰 순으로 데이터가 있는지 검사한다.
먼저 pageContext의 데이터를 검사(없으면) -> request -> session -> application을 검사
만약 위 4개중 어느것도 데이터가 없으면 출력시키지않는다.
(EL태그는 null값이면 출력하지 않는다.)
게다가 연산, 조건문도 EL 태그 안에서 전부 수행이 가능하다.
이것만으로도 JSP을 다룰때 EL태그를 꼭 써야할 이유가 충분하다.
만약 el태그를 쓰지않고 표현식(expression) 으로 처리를 해야한다면?
<h3>ob.name : <%= ((Member)pageContext.getAttribute("ob")).getName() %></h3>
<h3>ob.age : <%= ((Member)pageContext.getAttribute("ob")).getAge() %></h3>
pageContext 의 getAttribute에 담긴 객체 "ob" 는 Object(자바의 최상위) 클래스라서 Member클래스로 다운캐스팅을
해주고 그 이후 getName으로 선언해줘야 아래 출력결과가 나타난다.
(표현식이 Member 클래스의 .name을 찾지못하기 때문)
즉, EL태그와 getProperty, 표현식의 출력결과가 똑같다.
위 <h3>표현식은 attribute와 자바의 구조를 명확하게 이해하기에는 좋을 것 같으나. EL태그가 있다면
굳이 쓸 필요는 없어보인다
톰캣의 8.5버전에서는 별도의 라이브러리를 추가로 설치하지 않더라도 EL태그를 기본적으로 쓸 수 있다. .
정보 입력
ex02.jsp
<body>
<h1>ex02.jsp</h1>
<hr>
<form method="POST" action="ex02-action.jsp">
<p><input type="text" name="name" placeholder="name"></p>
<p><input type="text" name="age" placeholder="Age"></p>
<p><input type="submit"></p>
</form>
</body>
form - action 정보 처리
ex02-action.jsp
<body>
<h1>ex02-action.jsp</h1>
<hr>
<% request.setCharacterEncoding("UTF-8"); %>
<%--비어있는 Member 객체를 생성하고, ob에 저장한다. --%>
<jsp:useBean id="ob" class="day05.Member" scope="request" />
<%--넘어오는 모든 파라미터를 필드이름에 맞게 ob에 저장한다. --%>
<jsp:setProperty property="*" name="ob" />
<%-- ex02-result.jsp로 포워드한다. --%>
<jsp:forward page="ex02-result.jsp" />
</body>
파라미터가 아무리많아도
useBean (객체 빈통 한줄)
jsp:setProperty의 property를 *로 지정해주기만 하면 (넘어오는파라미터의 ob를 필드이름에맞게 지정)
jsp:forward를 표현식으로 바꾸면
request.getRequestDispatcher("ex02-result.jsp").forward(request,response);
<출력>
ex02-result.jsp
<body>
<h1>ex02-result.jsp</h1>
<hr>
<%--getProperty가 ob를 못찾음 --%>
<!-- <fieldset> -->
<!-- <legend><strong>getProperty</strong></legend> -->
<%-- <h3>name: <jsp:getProperty property="name" name="ob"/></h3> --%>
<%-- <h3>age: <jsp:getProperty property="age" name="ob"/></h3> --%>
<!-- </fieldset> -->
<br>
<fieldset>
<legend><strong>EL태그</strong></legend>
<h3>name: ${ob.name }</h3>
<h3>age: ${ob.age }</h3>
</fieldset>
</body>
객체를 만들어준게 없는데
EL태그로 출력이 가능하다.( attribute에 있는 값을 출력하기 굉장히 편해졌다. )
왜? request로 포워딩시켰기때문에.
include:
임의로 만든 jsp페이지를 현재위치에 포함시키고 싶을때 쓴다.
<body>
<h1>ex03-jsp</h1>
<hr>
<details>
<summary>ex01.jsp</summary>
<fieldset>
<legend>ex01.jsp</legend>
<jsp:include page="ex01.jsp" />
</fieldset>
</details>
<details>
<summary>ex02.jsp</summary>
<fieldset>
<legend>ex02.jsp</legend>
<jsp:include page="ex02.jsp" />
</fieldset>
</details>
</body>
summary는 접었을때도 보이는 이름 (숨겼을때도 표시되는요약). (details안에 자손태그로 만든다. )
ex03.jsp를 실행했지만 ex01.jsp와 ex02.jsp를 include해주었기때문에
선언된 위치에 각각의 영역이 출력된다.