본문 바로가기

Java, Spring/08일

8일차 1 - Java Beans

java beans

 

jsp 파일 : /webdev/WebContent/beans/beanTest.jsp

class : /webdev/src/test/bean/BeanTest.java 만들어

package test.bean;

public class BeanTest {
	
	private String name = "SEOTAIJI";

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<jsp:useBean id="beantest" class = "test.bean.BeanTest" />
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%=beantest.getName() %><br/>
<jsp:getProperty property="name" name="beantest"/> <!-- name에 id값 주고,BeanTest의 getName메서드가 호출되..  -->
</body>
</html>

<jsp:getProperty property="name" …  의 name은 변수명이 아니라 method이름이야.

get이나 set을 뺀 값으로 접근하면 돼.

package test.bean;

public class BeanTest {
	
	private String name = "SEOTAIJI";

	public String getNameXXXXX() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<jsp:useBean id="beantest" class = "test.bean.BeanTest" />
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:getProperty property="nameXXXXX" name="beantest"/>
</body>
</html>

잘 실행되지..

 

이번엔 값을 바꿔볼까..

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<jsp:useBean id="beantest" class = "test.bean.BeanTest" />
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:getProperty property="name" name="beantest"/><br/>
<jsp:setProperty property="name" name="beantest" value="정현철"/>
<jsp:getProperty property="name" name="beantest"/>
</body>
</html>

 

최근 java beans는 별로 사용되지 않는데 아직도 몇군데에서 사용돼..

일단 이전 페이지에서 넘어온 값을 한꺼번에 집어넣을 때 사용..

 

값을 받아와 dto에 넣는 간단한 예를 볼까

<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form name="myform" action="namecardpro.jsp" method="post">
이름 : <input type="text" name="name"/><br/>
주소 : <input type="text" name="address"/><br/>
아이디 : <input type="text" name="id"/><br/>
비밀번호 : <input type="text" name="password"/><br/>
<input type="submit" value="등록"/>
</form>
</body>
</html>

예) 전에 이런 식으로 일일이 받아 일일이 집어 넣어야했지.

<%@page import="board.model.BoardDto"%>
<%@page import="board.model.BoardDao"%>
<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	String name = request.getParameter("name");
	String title = request.getParameter("title");
	String password = request.getParameter("password");
	String content = request.getParameter("content");
%>    
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
	BoardDto boardDto = new BoardDto();
	boardDto.setName(name);
	boardDto.setTitle(title);
	boardDto.setPassword(password);
	boardDto.setContent(content);

 	BoardDao boardDao = new BoardDao();
	boolean result = boardDao.insertBoard(boardDto);
%>
<script>
alert('<%= result == true ? "글이 입력되었습니다." : "글 입력 실패" %>');
location.href='<%= result == true ? "list.jsp" : "javascript:history.back();" %>';
</script>
</body>
</html>

이것을 java beans을 사용하면 한번에 넣을 수 있어

 
package test.bean;

public class Namecard {
	private String name;
	private String address;
	private String id;
	private String password;
	
	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;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<jsp:useBean id="namecard" class="test.bean.Namecard" />
<jsp:setProperty name="namecard" property="*" /> <!-- 모든 property가 다 한번에 들어가 -->
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 잘 넘어갔는지 출력해보지 -->
이름 : <jsp:getProperty property="name" name="namecard"/><br/>
주소 : <jsp:getProperty property="address" name="namecard"/><br/>
아이디 : <jsp:getProperty property="id" name="namecard"/><br/>
비밀번호 : <jsp:getProperty property="password" name="namecard"/><br/>
</body>
</html>

 

그럼 이전에 만든 위의 insert_action을 bean으로 바꿔보자..

<%@page import="board.model.BoardDto"%>
<%@page import="board.model.BoardDao"%>
<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%> 
<jsp:useBean id="boardDto" class="board.model.BoardDto"/>
<jsp:setProperty property="*" name="boardDto"/>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
 	BoardDao boardDao = new BoardDao();
	boolean result = boardDao.insertBoard(boardDto);
%>
<script>
alert('<%= result == true ? "글이 입력되었습니다." : "글 입력 실패" %>');
location.href='<%= result == true ? "list.jsp" : "javascript:history.back();" %>';
</script>
</body>
</html>

 

 

문제

java beans를 이용하여 값을 생성한 후 뷰페이지에서 출력
600, 700,800,900,1000, 1100, 1200, 1300 하나만 나오게. 출력.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<jsp:useBean id = "numGen" class = "test.bean.NumberGen"/>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:getProperty property = "number" name="numGen"/> 
</body>
</html>

package test.bean;

public class NumberGen {
	private int number;
	
	public NumberGen() {
		number = ((int)(Math.random() * 8) + 6 )*100;
	}
	
	public int getNumber() {
		return number;
	}
}

'Java, Spring > 08일' 카테고리의 다른 글

8일차 2 - 화면분할(파일분할)  (0) 2012.09.20
8일차 3 - session  (0) 2012.09.20
8일차 4 - Cookie (1)  (0) 2012.09.20
8일차 5 - Cookie (2)  (0) 2012.09.20
8일차 6-2 - login Model2로 만들기 (1)  (0) 2012.09.20