forward/useBean으로 구구단 출력 - (c태그 / el태그 이용)

조수경·2022년 1월 7일
0

JSP

목록 보기
15/45

c태그

1. <c:forEach> : 배열, 컬렉션, 맵에 저장되어 있는 값들을 순차적으로 처리할 때 사용
2. <c:set> : EL에서 사용되어질 수 있는 Bean, Map등에 값을 설정을 하거나, 일반 변수를 생성해서 값을 할당 할 수 있습니다.

forward02

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>

<title>액션 태그</title>

</head>
<body>
  <!-- 같은 폴더 안이라 /이딴거 안써두댐 -->
  <!-- forward02_data.jsp?dan=5 -->
  <jsp:forward page="forward02_data.jsp">
  	<jsp:param name="dan" value="5" />
  </jsp:forward>
  <!-- 파람을 보내야되서 단일 테그로 끝나면 안됨 -->
</body>
</html>

forward02_data.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>

<title>Insert title here</title>
</head>
<body>
  <!-- forward02_data.jsp?dan=5 -->
  <!-- 파라미터로 넘어오는 순간 문자가 됨 그래서 형변환 해주기! -->    
<%
    String danStr = request.getParameter("dan");
	//i: 단(5단)
	int i = Integer.parseInt(danStr);
%>
<c:set var="i" value="<%=i%>" />
<!-- i를 쓰고싶은데 둘은 영역이 달라서 사용불가능 ㅠㅠ -->
<c:forEach var="j" begin="1" end="9" step="1">
   ${i} * ${j} = ${i*j}<br />
</c:forEach>

<%--
 
//방법2
<% 
  int dan = Integer.parseInt(request.getParameter("dan"));
  for(int i=1; i<10; i++){
%>
  <p><%=dan%> * <%=i%> = <%=dan*i%></p>
<%  
}
%> 

--%>


</body>
</html>

GuGuDan.java

package ch04.com.dao;
//자바빈 클래스가 아니라 시리얼 안함
public class GuGuDan {
	//곱셈을 계산하는 역할
	public int process(int i, int j) {
		return i * j;
	}

}

useBean.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>

<title>구구단</title>
</head>
<body>
  <jsp:useBean id="guGuDan" class="ch04.com.dao.GuGuDan" scope="page"/>
  <h4>구구단 출력하기</h4>
  <c:forEach var="j" begin="1" end="9" step="1">
  5 * ${j} = ${guGuDan.process(5,j)}<br />
  </c:forEach>
  
<%--   
${ }

- JSP가 실행될 때 즉시 반영된다. (Immediate evaluation)
- 객체 프로퍼티 값을 꺼낼때 주로 사용 
--%>
  
</body>
</html>
profile
신입 개발자 입니다!!!

0개의 댓글