본문 바로가기
SPRING

230222_기본

by 경 훈 2023. 2. 22.

 

package com.ezen.kim3_002;

public class Address {
	String name,phone,address;
	Score score;
	public Address() {
	}
	public Address(String name, String phone, String address) {
		super();
		this.name = name;
		this.phone = phone;
		this.address = address;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public Score getScore() {
		return score;
	}
	public void setScore(Score score) {
		this.score = score;
	}
	public void out() {
		System.out.println("이름:"+name);
		System.out.println("전화번호:"+phone);
		System.out.println("주소:"+address);
		System.out.println("국어:"+score.kor);
		System.out.println("영어:"+score.eng);
		System.out.println("수학:"+score.mat);
	}
}

 

package com.ezen.kim3_002;

public class Score {
	int kor,eng,mat;
	public Score() {
	}
	public Score(int kor, int eng, int mat) {
		super();
		this.kor = kor;
		this.eng = eng;
		this.mat = mat;
	}
	public int getKor() {
		return kor;
	}
	public void setKor(int kor) {
		this.kor = kor;
	}
	public int getEng() {
		return eng;
	}
	public void setEng(int eng) {
		this.eng = eng;
	}
	public int getMat() {
		return mat;
	}
	public void setMat(int mat) {
		this.mat = mat;
	}
}

 

package com.ezen.kim3_002;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AddConfig {
	@Bean
	public Address korea() {
		Score sc = new Score();
		sc.setKor(82);
		sc.setEng(80);
		sc.setMat(99);
		Address add = new Address();
		add.setName("홍길동");
		add.setPhone("010-4567-7894");
		add.setAddress("수원시 팔달구");
		add.setScore(sc);
		return add;
	}
}

 

package com.ezen.kim3_002;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

public class AddMain {

	public static void main(String[] args) {
//		String path = "classpath:sample.xml";
//		AbstractApplicationContext aac = new GenericXmlApplicationContext(path);
//		Address add = aac.getBean("add",Address.class);
//		add.out();
		AnnotationConfigApplicationContext acac =
				new AnnotationConfigApplicationContext(AddConfig.class);
		Address add = acac.getBean("korea",Address.class);
		add.out();
	}
}

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="save" method="post">
	상품명 : <input type="text" name="spname"><br>
	수량 : <input type="text" name="su"><br>
	판매금액 : <input type="text" name="kum"><br>
	<input type="submit" value="전송">
	</form>
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h3 align="center">${massage}</h3>
	<table border="1" align="center">
		<tr>
			<th>상품명</th><th>수량</th><th>금액</th><th>비고</th>	
		</tr>
		<tr>
			<td>${spname}</td><td>${su}</td>
			<td><fmt:formatNumber value="${kum}" pattern="#,##0"></fmt:formatNumber></td>
			<c:set var="bigo" value="aa"/>
			<c:choose>
				<c:when test="${kum>=100000}">
					<c:set var="bigo" value="고가품"/>
				</c:when>
				<c:when test="${kum>=30000}">
					<c:set var="bigo" value="중저가"/>
				</c:when>
				<c:otherwise>
					<c:set var="bigo" value="저렴한 상품"/>
				</c:otherwise>
			</c:choose>
			<td>${bigo}</td>
		</tr>
	</table>
	
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="save2" method="post">
	상품명 : <input type="text" name="spname"><br>
	수량 : <input type="text" name="su"><br>
	판매금액 : <input type="text" name="kum"><br>
	<input type="submit" value="전송">
	</form>
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h3 align="center">${massage}</h3>
	<table border="1" align="center">
		<tr>
			<th>상품명</th><th>수량</th><th>금액</th><th>비고</th>	
		</tr>
		<tr>
			<td>${spname}</td><td>${su}</td>
			<td><fmt:formatNumber value="${kum}" pattern="#,##0"></fmt:formatNumber></td>
			<td>${bigo}</td>
		</tr>
	</table>
	
</body>
</html>

 

package com.ezen.kim3_004;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {
	
	@RequestMapping(value = "/in") //외부에서 실행 시키는 이름
	public String ko1() {
		return "input";
	}
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String ko2(HttpServletRequest request, Model mo) {
		String spname = request.getParameter("spname");
		int su = Integer.parseInt(request.getParameter("su"));
		int kum = Integer.parseInt(request.getParameter("kum"));
		mo.addAttribute("massage","스프링방식에서 Model 방식");
		mo.addAttribute("spname",spname);
		mo.addAttribute("su",su);
		mo.addAttribute("kum",kum);
		return "out";
	}
	@RequestMapping(value = "/in2") //외부에서 실행 시키는 이름
	public String ko3() {
		return "input2";
	}
	@RequestMapping(value = "/save2", method = RequestMethod.POST)
	public String ko4(HttpServletRequest request, Model mo) {
		String spname = request.getParameter("spname");
		int su = Integer.parseInt(request.getParameter("su"));
		int kum = Integer.parseInt(request.getParameter("kum"));
		String bigo = null;
		if(kum>=100000) bigo="고가품";
		else if(kum>=30000) bigo="중저가";
		else bigo="저렴한 상품";
		mo.addAttribute("massage","스프링방식에서 Model 방식");
		mo.addAttribute("spname",spname);
		mo.addAttribute("su",su);
		mo.addAttribute("kum",kum);
		mo.addAttribute("bigo",bigo);
		return "out2";
	}
}

package com.ezen.kim3_005;

import java.util.ArrayList;

public class Myinfo {
	String name;
	int age;
	ArrayList<String> hobby; //취미.. 여러가지 선택 배열
	int kor,eng,mat;
	public Myinfo() {
	}
	public Myinfo(String name, int age, ArrayList<String> hobby) {
		super();
		this.name = name;
		this.age = age;
		this.hobby = hobby;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public ArrayList<String> getHobby() {
		return hobby;
	}
	public void setHobby(ArrayList<String> hobby) {
		this.hobby = hobby;
	}
	public int getKor() {
		return kor;
	}
	public void setKor(int kor) {
		this.kor = kor;
	}
	public int getEng() {
		return eng;
	}
	public void setEng(int eng) {
		this.eng = eng;
	}
	public int getMat() {
		return mat;
	}
	public void setMat(int mat) {
		this.mat = mat;
	}
	public void out() {
		System.out.println("이름:"+name);
		System.out.println("나이:"+age);
		System.out.println("취미:"+hobby);
		System.out.println("국어:"+kor);
		System.out.println("영어:"+eng);
		System.out.println("수학:"+mat);
	}
}

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="my" class="com.ezen.kim3_005.Myinfo">
	<constructor-arg value="홍길동"></constructor-arg>
	<constructor-arg value="32"></constructor-arg>
	<constructor-arg>
		<list>
			<value>여행</value>
			<value>드라이브</value>
			<value>맛집기행</value>
			<value>영화</value>
			<value>운동</value>
		</list>
	</constructor-arg>
	<property name="kor" value="80"></property>
	<property name="eng" value="95"></property>
	<property name="mat" value="84"></property>
</bean>

</beans>

 

package com.ezen.kim3_005;

import java.util.ArrayList;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyConfig {
	@Bean
	public Myinfo korea() {
		ArrayList<String> hobby = new ArrayList<String>();
		hobby.add("여행");
		hobby.add("맛집기행");
		hobby.add("영화");
		hobby.add("등산");
		hobby.add("낚시");
		Myinfo mydto = new Myinfo("한라산",22,hobby);
		mydto.setKor(80);
		mydto.setEng(85);
		mydto.setMat(70);
		return mydto;
	}
}

 

package com.ezen.kim3_005;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

public class Mymain {

	public static void main(String[] args) {
		String path = "classpath:myinfo.xml";
		AbstractApplicationContext aac = new GenericXmlApplicationContext(path);
		Myinfo mydto = aac.getBean("my",Myinfo.class);
		mydto.out();
		AnnotationConfigApplicationContext acac = 
				new AnnotationConfigApplicationContext(MyConfig.class);
		Myinfo mydto1 = acac.getBean("korea",Myinfo.class);
		mydto1.out();
	}
}

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="save" method="post">
		이름 : <input type="text" name="name"><br>
		나이 : <input type="text" name="age"><br>
		취미 : 
		<input type="checkbox" name="hobby" value="여행">여행
		<input type="checkbox" name="hobby" value="운동">운동
		<input type="checkbox" name="hobby" value="드라이브">드라이브
		<input type="checkbox" name="hobby" value="영화">영화<br>
		국어 : <input type="text" name="kor"><br>
		영어 : <input type="text" name="eng"><br>
		수학 : <input type="text" name="mat"><br>
		<input type="submit" value="전송">
	</form>
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<table border="1" align="center">
		<tr>
			<th>이름</th><th>나이</th><th>취미</th>
			<th>국어</th><th>영어</th><th>수학</th>
		</tr>
		<td>${name}</td><td>${age}</td>
		<td>
			<c:forEach var="i" begin="0" end="${fn:length(hobby)}" step="1">
			${hobby[i]}
			</c:forEach>
		</td>
		<td>${kor}</td><td>${eng}</td><td>${mat}</td>
	</table>
</body>
</html>

 

package com.ezen.kim3_006;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {
	
	@RequestMapping(value="/index")
	public String ko1() {
		return "input";
	}
	@RequestMapping(value="/save",method = RequestMethod.POST)
	public String ko2(HttpServletRequest request,Model mo) {
		String name = request.getParameter("name");
		int age = Integer.parseInt(request.getParameter("age"));
		String [] hobby = request.getParameterValues("hobby");
		int kor = Integer.parseInt(request.getParameter("kor"));
		int eng = Integer.parseInt(request.getParameter("eng"));
		int mat = Integer.parseInt(request.getParameter("mat"));
		mo.addAttribute("name", name);
		mo.addAttribute("age", age);
		mo.addAttribute("hobby", hobby);
		mo.addAttribute("kor", kor);
		mo.addAttribute("eng", eng);
		mo.addAttribute("mat", mat);
		return "out";
	}
}

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="save" method="post">
		이름:<input type="text" name="name"><br>
		국어:<input type="text" name="kor"><br>
		영어:<input type="text" name="eng"><br>
		수학:<input type="text" name="mat"><br>
		<input type="submit" value="전송">
	</form>
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<table border="1" align="center">
		<tr>
			<th>이름</th><th>국어</th><th>영어</th><th>수학</th>
			<th>비고</th>
		</tr>
		<tr>
			<td>${dto.name}</td>
			<td>${dto.kor}</td><td>${dto.eng}</td><td>${dto.mat}</td>
			<td>${dto.bigo}</td>
		</tr>
	</table>
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="savea" method="post">
		이름:<input type="text" name="name"><br>
		국어:<input type="text" name="kor"><br>
		영어:<input type="text" name="eng"><br>
		수학:<input type="text" name="mat"><br>
		<input type="submit" value="전송">
	</form>
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<table border="1" align="center">
		<tr>
			<th>이름</th><th>국어</th><th>영어</th><th>수학</th>
			<th>비고</th>
		</tr>
		<tr>
			<td>${sc.name}</td>
			<td>${sc.kor}</td><td>${sc.eng}</td><td>${sc.mat}</td>
			<td>${bigo}</td>
		</tr>
	</table>
</body>
</html>

 

package com.ezen.kim3_007;

public class Score {
	String name;
	int kor,eng,mat;
	String bigo;
	Scorebigo scorebigo = new Scorebigo();
	public Score() {
	}
	public Score(String name, int kor, int eng, int mat) {
		super();
		this.name = name;
		this.kor = kor;
		this.eng = eng;
		this.mat = mat;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getKor() {
		return kor;
	}
	public void setKor(int kor) {
		this.kor = kor;
	}
	public int getEng() {
		return eng;
	}
	public void setEng(int eng) {
		this.eng = eng;
	}
	public int getMat() {
		return mat;
	}
	public void setMat(int mat) {
		this.mat = mat;
	}
	public String getBigo() {
		return bigo;
	}
	public void setBigo(String bigo) {
		this.bigo = bigo;
	}
	public Scorebigo getScorebigo() {
		return scorebigo;
	}
	public void setScorebigo(Scorebigo scorebigo) {
		this.scorebigo = scorebigo;
	}
	public String bigo(double avg) {
		if(avg>=scorebigo.agrade) bigo="최우수";
		else if(avg>=scorebigo.bgrade) bigo="우수";
		else if(avg>=scorebigo.cgrade) bigo="보통";
		else bigo="이하저조함";
		return bigo;
	}
	public String ppp(int kor,int eng,int mat) {
		return scorebigo.bigo(kor,eng,mat);
	}
}

 

package com.ezen.kim3_007;

public class Scorebigo {
	int agrade,bgrade,cgrade,dgrade;
	public Scorebigo() {
	}
	public Scorebigo(int agrade, int bgrade, int cgrade, int dgrade) {
		super();
		this.agrade = agrade;
		this.bgrade = bgrade;
		this.cgrade = cgrade;
		this.dgrade = dgrade;
	}
	public int getAgrade() {
		return agrade;
	}
	public void setAgrade(int agrade) {
		this.agrade = agrade;
	}
	public int getBgrade() {
		return bgrade;
	}
	public void setBgrade(int bgrade) {
		this.bgrade = bgrade;
	}
	public int getCgrade() {
		return cgrade;
	}
	public void setCgrade(int cgrade) {
		this.cgrade = cgrade;
	}
	public int getDgrade() {
		return dgrade;
	}
	public void setDgrade(int dgrade) {
		this.dgrade = dgrade;
	}
	public String bigo(int kor,int eng,int mat) {
		int tot = kor+eng+mat;
		double avg = (double) tot/3;
		String bigo = null;
		if(avg>=agrade) bigo="최우수";
		else if(avg>=bgrade) bigo="우수";
		else if(avg>=cgrade) bigo="보통";
		else bigo="이하저조함";
		return bigo;
	}
}

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id ="bigo" class="com.ezen.kim3_007.Scorebigo">
	<property name="agrade" value="90"></property>
	<property name="bgrade" value="80"></property>
	<property name="cgrade" value="60"></property>
	<property name="dgrade" value="0"></property>
</bean>
<bean id="sc1" class="com.ezen.kim3_007.Score">
	<property name="name" value="홍길동"></property>
	<property name="kor" value="50"></property>
	<property name="eng" value="60"></property>
	<property name="mat" value="70"></property>
	<property name="scorebigo"><ref bean="bigo"></ref></property>
</bean>
</beans>

 

package com.ezen.kim3_007;


import javax.servlet.http.HttpServletRequest;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {
	
	@RequestMapping(value = "/in")
	public String ko1() {
		return "input";
	}
	@RequestMapping(value = "/save",method = RequestMethod.POST)
	public String ko2(HttpServletRequest request,Model mo) {
		String path = "classpath:score.xml";
		AbstractApplicationContext aac = 
				new GenericXmlApplicationContext(path);
		Score scdto = aac.getBean("sc1",Score.class);
		String name = request.getParameter("name");
		int kor = Integer.parseInt(request.getParameter("kor"));
		int eng = Integer.parseInt(request.getParameter("eng"));
		int mat = Integer.parseInt(request.getParameter("mat"));
		double avg = (kor+eng+mat)/3;
		String bigo = scdto.bigo(avg);
		scdto.setName(name);
		scdto.setKor(kor);
		scdto.setEng(eng);
		scdto.setMat(mat);
		scdto.setBigo(bigo);
		mo.addAttribute("dto", scdto);
		return "out";
	}
	@RequestMapping(value = "/in2")
	public String ko3() {
		return "input2";
	}
	@RequestMapping(value = "/savea",method = RequestMethod.POST)
	public String ko4(HttpServletRequest request,Model mo) {
		String name = request.getParameter("name");
		int kor = Integer.parseInt(request.getParameter("kor"));
		int eng = Integer.parseInt(request.getParameter("eng"));
		int mat = Integer.parseInt(request.getParameter("mat"));
		///XML 파일을 불러옴
		String path = "classpath:score.xml";
		AbstractApplicationContext aac = 
				new GenericXmlApplicationContext(path);
		Score sc = aac.getBean("sc1",Score.class);
		sc.setName(name);
		sc.setKor(kor);
		sc.setEng(eng);
		sc.setMat(mat);
		String bi = sc.ppp(kor,eng,mat);
		mo.addAttribute("sc", sc);
		mo.addAttribute("bigo", bi);
		return "out2";
	}
}

 

'SPRING' 카테고리의 다른 글

230227_MyBatis  (0) 2023.02.27
230224_MyBatis  (0) 2023.02.24
230223_MyBatis  (0) 2023.02.23
230221_기본  (0) 2023.02.21
230220_기본  (0) 2023.02.20

댓글