반응형
*JSONArray
JSONArray=>[{"name":"노","age":30},{"name":"김","age":20},{"name":"공","age":28}];
$.ajax({
url:"url",
data:QueryString,
type:"get|post",
dataType:"xml|json|text|html",
error:function(xhr){
},
success:function(json_arr_data){ //파싱된데이터 //jquery에서 반복시키는 함수.each를 사용해
$.each(json_arr_data, (사용할 함수)function(인덱스, json_obj){
json_obj.이름; //해당하는 값이 잘 나오게 된다.=>파싱=>어떻게 뷰를 구성할것인지 넣어주면 되겠다.
});
}
});
//json simple liberay=>로 jsonArray만든다..
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="http://localhost:8080/jsp_prj/common/main_v190130.css"/>
<style type="text/css">
#wrap{margin:0px auto;width:800px; height:860px;}
#header{width:800px; height:140px; background:#FFFFFF url(http://localhost:8080/jsp_prj/common/images/header_bg.png) repeat-x;
position: relative; }
#headerTitle{font-family: HY견고딕,고딕; font-size:35px; font-weight:bold; text-align:center;
/* padding-top: 35px */ position:absolute; top:40px; left:290px; }
#container{width:800px; min-height: 600px; }
#footer{width:800px; height:120px; }
#footerTitle{float: right; font-size:15px; padding-top:20px; padding-right:20px; }
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$("#setValue").click(function(){
$.ajax({
url:"json_result.jsp",
type:"post",
dataType:"json",
error:function(xhr){
alert(xhr.status+" "+xhr.statusText);
},
success:function(jsonObj){
//alert(jsonObj.age);
$("#name").val(jsonObj.name);
$("#age").val(jsonObj.age);
}//success
});//ajax
});//click
});//ready
</script>
</head>
<body>
<div id="wrap">
<div id="header">
<div id="headerTitle">SIST Class4</div>
<div style="padding-top:100px; ">
<c:import url="../common/jsp/main_menu.jsp"></c:import>
</div>
</div>
<div id="container">
<div>
<a href="#void" id="setValue">값 설정</a>
</div>
<div>
이름 : <input type="text" name="name" id="name" class="inputBox"/><br/>
나이 : <input type="text" name="age" id="age" class="inputBox"/><br/>
</div>
</div>
<div id="footer">
<div id="footerTitle">copyright© all right reserved. class 4.</div>
</div>
</div>
</body>
</html>
<%@page import="org.json.simple.JSONArray"%>
<%@page import="org.json.simple.JSONObject"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String[] nameArr={"김","김","노","공"};
String[] addrArr={"서울시","서울시","서울시","서울시"};
int[] ageArr={30,28,31,29};
JSONArray json_arr=new JSONArray();
JSONObject json_obj=null;
for(int i=0; i<nameArr.length; i++){
//배열이 존재한다면 JSONObject을 생성
json_obj=new JSONObject();
json_obj.put("name", nameArr[i]);
json_obj.put("age", ageArr[i]);
json_obj.put("addr",addrArr[i]);
//생성된 JSONObject를 JSONArray에 추가
json_arr.add(json_obj);
}//end for
out.print(json_arr.toJSONString());
%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="http://localhost:8080/jsp_prj/common/main_v190130.css"/>
<style type="text/css">
#wrap{margin:0px auto;width:800px; height:860px;}
#header{width:800px; height:140px; background:#FFFFFF url(http://localhost:8080/jsp_prj/common/images/header_bg.png) repeat-x;
position: relative; }
#headerTitle{font-family: HY견고딕,고딕; font-size:35px; font-weight:bold; text-align:center;
/* padding-top: 35px */ position:absolute; top:40px; left:290px; }
#container{width:800px; min-height: 600px; }
#footer{width:800px; height:120px; }
#footerTitle{float: right; font-size:15px; padding-top:20px; padding-right:20px; }
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$("#getJsonArr").click(function(){
$.ajax({
url:"json_array.jsp",
type:"get",
dataType:"json",
error:function(xhr){
alert(xhr.status+"/"+xhr.statusText);
},
success:function(json_arr){
var output="<ul>";
$.each(json_arr,function(idx,json_obj){
//alert(json_obj);
output+="<li>"+json_obj.name+"/"+json_obj.age+"/"+json_obj.addr+"</li>"
});//each
$("#jsonArrDiv").html(output);
}//success
});//ajax
});//click
});//ready
</script>
</head>
<body>
<div id="wrap">
<div id="header">
<div id="headerTitle">SIST Class4</div>
<div style="padding-top:100px; ">
<c:import url="../common/jsp/main_menu.jsp"></c:import>
</div>
</div>
<div id="container">
<div>
<a href="#void" id="getJsonArr">값얻기</a>
</div>
<div id="jsonArrDiv">
</div>
</div>
<div id="footer">
<div id="footerTitle">copyright© all right reserved. class 4.</div>
</div>
</div>
</body>
</html>
<<db조회해 불러다 놓기>>
<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="org.json.simple.JSONObject"%>
<%@page import="json0328.JsonService"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String deptno=request.getParameter("deptno");
JSONObject empData=null;
try{
int intDeptno=Integer.parseInt(deptno);
JsonService js=new JsonService();
empData=js.searchEmpData(intDeptno);
}catch(NumberFormatException nfe){
//nfe.printStackTrace();
empData=new JSONObject();
empData.put("result", false);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
empData.put("pubData",sdf.format(new Date()));
empData.put("resultData",null);
}//end catch
out.println(empData.toJSONString());
%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="http://localhost:8080/jsp_prj/common/main_v190130.css"/>
<style type="text/css">
#wrap{margin:0px auto;width:800px; height:860px;}
#header{width:800px; height:140px; background:#FFFFFF url(http://localhost:8080/jsp_prj/common/images/header_bg.png) repeat-x;
position: relative; }
#headerTitle{font-family: HY견고딕,고딕; font-size:35px; font-weight:bold; text-align:center;
/* padding-top: 35px */ position:absolute; top:40px; left:290px; }
#container{width:800px; min-height: 600px; }
#footer{width:800px; height:120px; }
#footerTitle{float: right; font-size:15px; padding-top:20px; padding-right:20px; }
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$("#btn").click(function(){
var deptno=$("#deptno").val();
if(deptno==""){
alert("부서번호는 필수 입력!!");
$("#deptno").focus();
return;
}//end if
$.ajax({
url:"json_db.jsp",
type:"post",
data:"deptno="+deptno,
dataType:"json",
error:function(xhr){
alert(xhr.status+"/"+xhr.statusText);
},
success:function(json_obj){
var result=json_obj.result;
if(result){
//alert("검색 데이터 존재");
var output="<strong>"+deptno+"</strong>번 부서사원 조회 결과<br/>";
var json_arr=json_obj.resultData;
$.each(json_arr,function(idx,jsonEmpData){
output+="<div style='margin-bottom:10px'>"
+"<table border='1'><tr><td width='100'>사원번호</td><td width='100'>"+jsonEmpData.empno
+"</td><td width='80'>사원명</td><td width='120'>"+jsonEmpData.ename
+"</td><td width='80'>입사일</td><td width='150'>"+jsonEmpData.hiredate+"</td></tr>"
+"<tr><td width='80'>연봉</td><td width='150'>"
+"<input type='text' name='sal' value='"+jsonEmpData.sal
+"'/></td><td width='80'>직무</td><td colspan='3'>"
+"<input type='text' name='job' value='"+jsonEmpData.job
+"'/></td></tr></table></div>";
});//each
output+="데이터 생성일 :"+json_obj.pubData;
$("#empView").html(output);
}else{
//alert("검색 데이터 존재X");
var img="<img src='../common/images/sist_logo.jpg'><br/>부서에 사원정보가 존재하지 않습니다.";
$("#empView").html(img);
}//end else
}//success
});//ajax
});//click
});//ready
</script>
</head>
<body>
<div id="wrap">
<div id="header">
<div id="headerTitle">SIST Class4</div>
<div style="padding-top:100px; ">
<c:import url="../common/jsp/main_menu.jsp"></c:import>
</div>
</div>
<div id="container">
<div>
<label>부서번호:</label>
<input type="text" name="deptno" id="deptno" class="inputBox"/>
<input type="button" value="사원 조회" id="btn" class="btn"/>
</div>
<div id="empView">
</div>
</div>
<div id="footer">
<div id="footerTitle">copyright© all right reserved. class 4.</div>
</div>
</div>
</body>
</html>
package ajax0401;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class WebMemberDAO {
private static WebMemberDAO wm_dao;
public WebMemberDAO() {
}//WebMemberDAO
public static WebMemberDAO getInstance() {
if(wm_dao==null) {
wm_dao=new WebMemberDAO();
}//end if
return wm_dao;
}//getInstance
public Connection getConn() throws SQLException{
Connection con=null;
try {
//1.JNDI 사용객체 생성
Context ctx=new InitialContext();
//2.DBCP에서 DataSource 얻기
DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/jsp_dbcp");
//3.DataSource에서 Connection 얻기
con=ds.getConnection();
} catch (NamingException e) {
e.printStackTrace();
}//end catch
return con;
}//getConn
/**
* ID가 존재 하는지 중복 검사
* @param id
* @return
* @throws SQLException
*/
public boolean selectId(String id) throws SQLException{
boolean flag=false;
Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
try {
//1.
//2.
//3.
con=getConn();
//4.
String selectId="select id from web_member where id=?";
pstmt=con.prepareStatement(selectId);
pstmt.setString(1, id);
//5.
rs=pstmt.executeQuery();
flag=!rs.next();//있으면 true->반대로 false
}finally {
//6.
if(rs!=null) {rs.close();}//end if
if(pstmt!=null) {pstmt.close();}//end if
if(con!=null) {con.close();}//end if
}//end finally
return flag;
}//selectId
}//class
<%@page import="org.json.simple.JSONObject"%>
<%@page import="ajax0401.WebMemberService"%>
<%@page import="json0328.JsonService"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String id=request.getParameter("id");
WebMemberService wm_service=new WebMemberService();
JSONObject json=wm_service.searchId(id);
out.println(json.toJSONString());
%>
package ajax0401;
import java.sql.SQLException;
import org.json.simple.JSONObject;
public class WebMemberService {
//업무로직 처리
public JSONObject searchId(String id) {
JSONObject json=new JSONObject();
WebMemberDAO wm_dao=WebMemberDAO.getInstance();
boolean flag=false;
try {
flag=wm_dao.selectId(id);
} catch (SQLException e) {
e.printStackTrace();
}//end catch
//DB조회 결과를 JSONObject에 추가
json.put("idResult", flag);
return json;
}//searchId
}//class
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="http://localhost:8080/jsp_prj/common/main_v190130.css"/>
<style type="text/css">
#wrap{margin:0px auto;width:800px; height:860px;}
#header{width:800px; height:140px; background:#FFFFFF url(http://localhost:8080/jsp_prj/common/images/header_bg.png) repeat-x;
position: relative; }
#headerTitle{font-family: HY견고딕,고딕; font-size:35px; font-weight:bold; text-align:center;
/* padding-top: 35px */ position:absolute; top:40px; left:290px; }
#container{width:800px; min-height: 600px; }
#footer{width:800px; height:120px; }
#footerTitle{float: right; font-size:15px; padding-top:20px; padding-right:20px; }
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$("#id").keyup(function(){
var id=$("#id").val();
if(id.length>2){
if(id.trim()==""){
$("#idResult").text("아이디를 입력해 주세요!").css("color","#FF0000");
$("#id").val("");
return;
}//end if
//ID중복확인을 비동기로 처리
$.ajax({
url:"json_dup_id.jsp",
type:"get",
aync:false,
data:"id="+id,
dataType:"json",
error:function(xhr){
alert("서비스가 원활하지 못합니다. 잠시후 다시 시도해주세요");
console.log(xhr.status+"/"+xhr.statusText);
},
success:function(json_obj){
var output="[<strong>"+id+"</strong>]는 사용 불가";
var color="#FF0000";
if(json_obj.idResult){
output="[<strong>"+id+"</strong>]는 사용가능 합니다.";
color="#0000FF";
}//end if
$("#idResult").html(output).css("color",color);
for(var i=0; i<4; i++){
$("#idResult").fadeIn(1000).fadeOut(1000).fadeIn(1000);
}//end for
}//success
});//ajax
}else{
$("#idResult").html("");
}//end else
});//keyup
});//ready
</script>
</head>
<body>
<div id="wrap">
<div id="header">
<div id="headerTitle">SIST Class4</div>
<div style="padding-top:100px; ">
<c:import url="../common/jsp/main_menu.jsp"></c:import>
</div>
</div>
<div id="container">
<strong>회원가입</strong>
<table>
<tr>
<td>아이디</td>
<td width="500">
<input type="text" name="id" class="inputBox" id="id"/>
<span id="idResult"></span><br/>
<span style="color:#333333">아이디는 3글자 이상 입력하셔야 합니다.</span>
</td>
</tr>
</table>
</div>
<div id="footer">
<div id="footerTitle">copyright© all right reserved. class 4.</div>
</div>
</div>
</body>
</html>
*Model2(MVC2)
-
//어렵다-대규모 시스템인 경우 개발속도도 중요하지만 유지보수도 중요해 도입해 유지보수성을 높이려는것
-
//모델1은 유지보수성이 떨어짐
-
유지보수의 성능 향상(편의성 증대)
-
업무구분이 쉽다(코드를 분리하여 제작)
-
개발속도가 느리다.
-
파일관리가 어렵다.
-
에러처리가 어렵다.
-
Servlet+DAO+jsp로 구분되어 작성
-
(이미지)
-
각 역할)
-
Main Controller : 진입점(무조건 하나)
-
=>Servlet : 모든 요청을 받아 해당 요청을 처리할 수 있는 Controller를 실행 (Controller실행 결과를 보여줄 View로 이동)
-
Controller : class //옛날 이름이 Action 스프링 와서 이름이 바뀜
-
=>Web Parameter처리, 관계유지, View페이지의 명을 반환, 이동방식의 결정(설정), Service클래스 사용(Service가 처리할 결과를 받는다)
-
Service : class
-
=>업무로직에 대한 처리, DAO단의 사용(DB처리 결과 사용)
-
DAO : class
-
=>DB업무 처리
-
JSP : 업무처리한 결과를 받아서 접속자가 사용할 화면을 만드는 일
-
흐름)
-
webContainer에 Controller에서 MAP을 이용해 값을 찾고 .class로 만들어 DBCP dataSource를 찾는다. >View가 응답
-
DB사용
-
Web Client>요청>Servlet>Controller>vo형태>Service>vo형태>DAO>vo형태>Service>java의데이터형|VO|JSON|XML>Controller>java의데이터형|VO|JSON|XML>JSP>응답>Web Client
-
DB사용X (단순 계산)
-
Web Client>업무처리요청>Servle>Controller>Service>Controller>Servlet>JSP>응답>Web Client
-
파라메터만 처리 (관계유지->필수사항)
-
Web Client>Servlet>Controller>Servlet>JSP>Web Client
-
//에노테이션??
-
//응답하는 View가 jsp라서 응답할때 Controller와 View가 혼동된다면 집입점이 2개가 되는거
-
작성 방법)
-
진입점을 하나로 설정-통제의 편의성/JSP의 역할때문에
-
//응답하는 View가 jsp라서 응답할때 Controller와 View가 혼동된다면 집입점이 2개가 되는거
-
(Servlet 하나 생성)
-
DD : Security Constraint 설정=>JSP를 직접요청하지 못하도록 설정
-
객체다형성 (inheritance-상속)
-
//객체 다형성 :하나의 이름으로 객체를 다양하게 쓸수 있는것 (장: 중복코드가 줄어든다.
-
//Map 에 해당되는 이야기...
-
//servlet에서는 각각 들어가야 함..
-
페이지 이동
-
//forward-화면처리와 업무로직을 구분할 때 사용(브라우저는이동하는페이지를 모르고 객체와 파라메터로 넘길수 있다. vs redircet-비정상적인 요청
new>Dynamic web~>2.5 모델2를 할거라서!!
hello.jsp를 만들고 model2_prj로 바꾸어준다.
lib에 jstl.jar&standard.jar 넣어주고 jsp_prj common 복사해옴.
hello.jsp를 실행 시켰을때 model2는 직접 실행되게 하면 안된다.
보통의 홈페이지에서 만약 실행되면 DB연동이 안되는 껍데기부분만 나오게 됨
Apache Tomcat이 없으면 Error로 잡아져 있어야 한다! 없으면추가
<security-constraint>이부분을 추가해 줌으로 직접요청을 막을 수 있다. (403뜸!!)
엔카 같은경우에는 jsp요청시에 에러페이지가 뜨는데...그건 위와같은 방법으로 설정이 가능하다.
에러페이지 설정!
문제는 에러가 안나서 확인 불가ㅋㅋㅋㅋㅋ서비스 할때에만 설정해 둘것!!
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="http://localhost:8080/model2_prj/common/main_v190130.css"/>
<style type="text/css">
#wrap{margin:0px auto;width:800px; height:860px;}
#header{width:800px; height:140px; background:#FFFFFF url(http://localhost:8080/model2_prj/common/images/header_bg.png) repeat-x;
position: relative; }
#headerTitle{font-family: HY견고딕,고딕; font-size:35px; font-weight:bold; text-align:center;
/* padding-top: 35px */ position:absolute; top:40px; left:290px; }
#container{width:800px; min-height: 600px; }
#footer{width:800px; height:120px; }
#footerTitle{float: right; font-size:15px; padding-top:20px; padding-right:20px; }
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
});//ready
</script>
</head>
<body>
<div id="wrap">
<div id="header">
<div id="headerTitle">SIST Class4</div>
<div style="padding-top:100px; ">
<c:import url="../common/jsp/main_menu.jsp"></c:import>
</div>
</div>
<div id="container">
안녕하세요?<br/>
<!-- 경로설정이 잘못됨-이미지가 보여질때 Servlet->실행시 브라우저의 url에 위치에 따라 상대경로의 위치로 생각해 경로설정을 해주어야 한다. -->
<!-- <img src="../common/imagex/img.png"/> -->
<!--
http://localhost:8080/model2_prj/common/images/img.png
http://localhost:8080/model2_prj/MainController
라서 같은 위치에 있다고 생각되므로 common/images/img.png 만 적으면 열리는것
-->
<img src="common/images/img.png"/>
</div>
<div id="footer">
<div id="footerTitle">copyright© all right reserved. class 4.</div>
</div>
</div>
</body>
</html>
<security-constraint>
<web-resource-collection>
<web-resource-name></web-resource-name>
<url-pattern>*.jsp</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name></role-name>
</auth-constraint>
</security-constraint>
<error-page>
<error-code>403</error-code>
<location>/common/error/err_403.html</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/common/error/err_403.html</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/common/error/err_403.html</location>
</error-page>
<servlet>
<description></description>
<display-name>MainController</display-name>
<servlet-name>MainController</servlet-name>
<servlet-class>day0401.MainController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MainController</servlet-name>
<url-pattern>/MainController</url-pattern>
</servlet-mapping>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="http://localhost:8080/model2_prj/common/main_v190130.css"/>
<style type="text/css">
#wrap{margin:0px auto;width:800px; height:860px;}
#header{width:800px; height:140px; background:#FFFFFF url(http://localhost:8080/model2_prj/common/images/header_bg.png) repeat-x;
position: relative; }
#headerTitle{font-family: HY견고딕,고딕; font-size:35px; font-weight:bold; text-align:center;
/* padding-top: 35px */ position:absolute; top:40px; left:290px; }
#container{width:800px; height:600px; }
#footer{width:800px; height:120px; }
#footerTitle{float: right; font-size:15px; padding-top:20px; padding-right:20px; }
</style>
</head>
<body>
<div id="wrap">
<div id="header">
<div id="headerTitle">SIST Class4</div>
</div>
<div id="container">
<img src="http://localhost:8080/model2_prj/common/images/err.png"/>
</div>
<div id="footer">
<div id="footerTitle">copyright© all right reserved. class 4.</div>
</div>
</div>
</body>
</html>
package day0401;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class MainController extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
//으로 post로 넘겨 진입점을 하나로 만듦
}//doGet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//response.sendRedirect("day0401/hello.jsp");//페이지의 변경을 알아 보안에 걸려서 쓰지 않는다.
RequestDispatcher rd=request.getRequestDispatcher("day0401/hello.jsp");
rd.forward(request, response);
}//doPost
}//class
-
객체다형성의 장점-map에 대한 부분)
-
+SeonUi +JaeHyun +TeackSung
-
-
+execute() String 어~~ 저요 배고프다
-
//일때
-
Map m=new Map();
-
m.put("gong", new SeonUi());
-
m.put("lee", new JaeHyun());
-
m.put("Jung", new TeackSung());
-
String key="?"; <="gong"|"lee"|"jung" 3가지 밖에 되지 않아도 if 로 물어봐줄 수 밖에 없다.
-
if("gong".equals(key)){
-
SeonUi su=(SeonUi)map.get(key); //재현은 선의로 캐스팅할 수 없으므로 Error
-
su.execute(); =>어~~ //(부르는 메소드는 같다!)
-
}
-
if("lee".equals(key)){ //키에따른 객체를 찾아 캐스팅하고 담아 실행
-
}
-
-
//웹에서는 이 key를 커멘드! 라고 한다.
반응형
'국비지원학원 > XML,JSON,AJAX' 카테고리의 다른 글
109일차-객체다형성+Model2예시 (0) | 2019.04.15 |
---|---|
107일차-AJAX (0) | 2019.04.11 |
106일차-JSON/AJAX (0) | 2019.04.10 |
105일차-XML+Parsing (0) | 2019.04.08 |
104일차-XML+Parser (0) | 2019.04.08 |