자료 전송방식 3방식
1. Model
2. Model & View
3. Command
<?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 ="s1" class="com.ezen.kim2_001.DTO">
<constructor-arg value="korea"></constructor-arg>
<constructor-arg value="홍길동"></constructor-arg>
<constructor-arg value="24"></constructor-arg>
<property name="kor" value="56"></property>
<property name="eng" value="56"></property>
<property name="mat" value="56"></property>
</bean>
</beans>
<%@ 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">
<table border="1" align="center">
<tr>
<th>아이디</th>
<td><input type="text" name="id"></td>
</tr>
<tr>
<th>이름</th>
<td><input type="text" name="name"></td>
</tr>
<tr>
<th>나이</th>
<td><input type="text" name="age"></td>
</tr>
<tr>
<th>국어</th>
<td><input type="text" name="kor"></td>
</tr>
<tr>
<th>영어</th>
<td><input type="text" name="eng"></td>
</tr>
<tr>
<th>수학</th>
<td><input type="text" name="mat"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="전송">
<input type="reset" value="초기화">
</td>
</tr>
</table>
</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>
<h2 align="center">${message}</h2>
<table border="1" align="center">
<tr>
<th>아이디</th><th>이름</th><th>나이</th>
<th>국어</th><th>영어</th><th>수학</th>
</tr>
<tr>
<td>${dto.id}</td><td>${dto.name}</td><td>${dto.age}</td>
<td>${dto.kor}</td><td>${dto.eng}</td><td>${dto.mat}</td>
</tr>
</table>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://Java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 한글깨짐 방지 -->
<!-- filter와 filter-mapping을 만들어 준다. -->
<filter>
<!-- filter안에는 filter-name, filter-class, init-param을 추가해 준다.
filter-name은 원하는대로 지정해도됨 -->
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<!-- encoidng값을 UTF-8로 만들어 준다. -->
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
package com.ezen.kim2_001;
public class DTO {
String id,name;
int age;
int kor,eng,mat;
public DTO() {
}
public DTO(String id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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 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("아이디:"+id);
System.out.println("이름:"+name);
System.out.println("나이:"+age);
System.out.println("국어:"+kor);
System.out.println("영어:"+eng);
System.out.println("수학:"+mat);
}
}
package com.ezen.kim2_001;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class Main {
public static void main(String[] args) {
String path = "classpath:sample1.xml";
AbstractApplicationContext aac = new GenericXmlApplicationContext(path);
DTO dto = aac.getBean("s1",DTO.class);
dto.out();
///java파일 이용
AnnotationConfigApplicationContext acac =
new AnnotationConfigApplicationContext(Config.class);
DTO dto1 = acac.getBean("koko",DTO.class);
dto1.out();
}
}
package com.ezen.kim2_001;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Config {
@Bean
public DTO koko() {
DTO dto = new DTO();
dto.setId("korea");
dto.setName("홍길동");
dto.setAge(25);
dto.setKor(60);
dto.setEng(70);
dto.setMat(90);
return dto;
}
}
package com.ezen.kim2_001;
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 id = request.getParameter("id");
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
int kor = Integer.parseInt(request.getParameter("kor"));
int eng = Integer.parseInt(request.getParameter("eng"));
int mat = Integer.parseInt(request.getParameter("mat"));
DTO dto = new DTO();
dto.setId(id);
dto.setName(name);
dto.setAge(age);
dto.setKor(kor);
dto.setEng(eng);
dto.setMat(mat);
mo.addAttribute("message","회원성적표");
mo.addAttribute("dto", dto);
return "out";
}
}
폴더 인식
spring -> appServlet -> servlet-context.xml 수정
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<resources mapping="/image/**" location="/image/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.ezen.kim2_001" />
</beans:beans>
package com.ezen.kim2_004;
public class CCC {
int first,second;
DDD ddd;
public CCC() {
}
public CCC(int first, int second) {
super();
this.first = first;
this.second = second;
}
public int getFirst() {
return first;
}
public void setFirst(int first) {
this.first = first;
}
public int getSecond() {
return second;
}
public void setSecond(int second) {
this.second = second;
}
public DDD getDdd() {
return ddd;
}
public void setDdd(DDD ddd) {
this.ddd = ddd;
}
public void out( ) {
ddd.out(first, second);
}
}
package com.ezen.kim2_004;
public class DDD {
public void out(int a, int b) {
System.out.println(a+"*"+b+"="+(a*b));
}
}
<?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="d1" class="com.ezen.kim2_004.DDD">
</bean>
<bean id="c1" class="com.ezen.kim2_004.CCC">
<property name="first" value="45"></property>
<property name="second" value="88"></property>
<property name="ddd"><ref bean="d1"></ref></property>
</bean>
</beans>
package com.ezen.kim2_004;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class CCCMain {
public static void main(String[] args) {
String path = "classpath:ccdd.xml";
AbstractApplicationContext aac = new GenericXmlApplicationContext(path);
CCC ccc = aac.getBean("c1",CCC.class);
ccc.out();
}
}
package com.ezen.kim2_005;
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() {
int kor = score.getKor();
int eng = score.getEng();
int mat = score.getMat();
System.out.println("이름: "+name);
System.out.println("전번: "+phone);
System.out.println("주소: "+address);
System.out.println("국어: "+kor);
System.out.println("영어: "+eng);
System.out.println("수학: "+mat);
}
}
package com.ezen.kim2_005;
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;
}
}
<?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="sc" class="com.ezen.kim2_005.Score">
<property name="kor" value="65"></property>
<property name="eng" value="75"></property>
<property name="mat" value="80"></property>
</bean>
<bean id="add" class="com.ezen.kim2_005.Address">
<constructor-arg value="홍길동"></constructor-arg>
<constructor-arg value="010-1234-1234"></constructor-arg>
<constructor-arg value="경기도 수원시"></constructor-arg>
<property name="score"><ref bean="sc"></ref></property>
</bean>
</beans>
package com.ezen.kim2_005;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class Main {
public static void main(String[] args) {
String path = "classpath:sample.xml";
AbstractApplicationContext aac = new GenericXmlApplicationContext(path);
Address address = aac.getBean("add",Address.class);
address.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">
<table border="1" align="center">
<tr>
<th>이름</th>
<td><input type="text" name="name"></td>
</tr>
<tr>
<th>국어</th>
<td><input type="text" name="kor"></td>
</tr>
<tr>
<th>영어</th>
<td><input type="text" name="eng"></td>
</tr>
<tr>
<th>수학</th>
<td><input type="text" name="mat"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="전송">
<input type="reset" value="초기화">
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<!-- Model & ModelAndView 방식 -->
<%-- <body>
<h2 align="center">
${message}
</h2>
<table border="1" align="center">
<tr>
<th>이름</th><th>국어</th><th>영어</th>
<th>수학</th> <th>총점</th> <th>평균</th>
</tr>
<tr>
<td>${list.name}</td><td>${list.kor}</td><td>${list.eng}</td>
<td>${list.mat}</td><td>${tot}</td><td>${avg}</td>
</tr>
</table>
</body> --%>
<!-- Command 방식의 출력 -->
<body>
<h2 align="center">
Command 방식
</h2>
<table border="1" align="center">
<tr>
<th>이름</th><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.kor+dto.eng+dto.mat}</td>
<td><fmt:formatNumber value="${(dto.kor+dto.eng+dto.mat)/3}" pattern="#,##0.0">
</fmt:formatNumber>
</td>
</tr>
</table>
<hr>
<c:set var="tot" value="${dto.kor+dto.eng+dto.mat}"></c:set>
총점:<c:out value="${tot}"></c:out>
<br>
<c:set var="avg" value="${tot/3}"></c:set>
평균:<c:out value="${avg}"></c:out>
</body>
</html>
package com.ezen.kim2_006;
public class DTO {
String name;
int kor,eng,mat;
public DTO() {
}
public DTO(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;
}
}
package com.ezen.kim2_006;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
/**
* Handles requests for the application home page.
*/
@Controller
public class HomeController {
@RequestMapping(value = "/in")
public String ko1() {
return "input";
}
// Model 전송방식
// @RequestMapping(value = "/save",method = RequestMethod.POST)
// public String ko2(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"));
// int tot = kor+eng+mat;
// double avg = (double)tot/3;
// DTO dto = new DTO();
// dto.setName(name);
// dto.setKor(kor);
// dto.setEng(eng);
// dto.setMat(mat);
// mo.addAttribute("message","Model 전송방식");
// mo.addAttribute("list",dto);
// mo.addAttribute("tot",tot);
// mo.addAttribute("avg",avg);
// return "out";
// }
// Model & View 방식
// @RequestMapping(value = "/save",method = RequestMethod.POST)
// public ModelAndView ko3(HttpServletRequest request) {
// 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"));
// int tot = kor+eng+mat;
// double avg = (double)tot/3;
// DTO dto = new DTO();
// dto.setName(name);
// dto.setKor(kor);
// dto.setEng(eng);
// dto.setMat(mat);
// ModelAndView mav = new ModelAndView();
// mav.addObject("message", "ModelAndView 전송방식");
// mav.addObject("list",dto);
// mav.addObject("tot",tot);
// mav.addObject("avg",avg);
// mav.setViewName("out");
// return mav;
// }
// Command 방식
@RequestMapping(value = "/save",method = RequestMethod.POST)
public String ko4(DTO dto) {
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="spname"><br>
수량<input type="text" name="su"><br>
금액<input type="text" name="price"><br>
<input type="submit" value="전송"><br>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ 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>
상품명 : ${sp.spname} <br>
수량 : ${sp.su} <br>
가격 : <fmt:formatNumber value="${sp.price}" pattern="#,##0"></fmt:formatNumber><br>
비고 : ${sp.bigo} <br>
</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="save1" method="post">
상품명<input type="text" name="spname"><br>
수량<input type="text" name="su"><br>
금액<input type="text" name="price"><br>
<input type="submit" value="전송"><br>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
상품명 : ${spDTO.spname} <br>
수량 : ${spDTO.su} <br>
가격 : <fmt:formatNumber value="${spDTO.price}" pattern="#,##0"></fmt:formatNumber><br>
비고 :
<c:choose>
<c:when test="${spDTO.price >=100000 }">
고가품
</c:when>
<c:when test="${spDTO.price <=99999 && spDTO.price >=30000}">
중저가
</c:when>
<c:otherwise>
저렴한 상품
</c:otherwise>
</c:choose>
</body>
</html>
package com.ezen.kim2_007;
public class SpDTO {
String spname;
int su,price;
String bigo;
public SpDTO() {
}
public SpDTO(String spname, int su, int price, String bigo) {
super();
this.spname = spname;
this.su = su;
this.price = price;
this.bigo = bigo;
}
public String getSpname() {
return spname;
}
public void setSpname(String spname) {
this.spname = spname;
}
public int getSu() {
return su;
}
public void setSu(int su) {
this.su = su;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getBigo() {
return bigo;
}
public void setBigo(String bigo) {
this.bigo = bigo;
}
}
package com.ezen.kim2_007;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 price = Integer.parseInt(request.getParameter("price"));
String bigo = null;
if(price>=100000) {
bigo = "고가품";
}
else if(price<=99999 & price>=30000) {
bigo= "중저가";
}
else {
bigo= "저렴한 상품";
}
SpDTO sp = new SpDTO();
sp.setSpname(spname);
sp.setSu(su);
sp.setPrice(price);
sp.setBigo(bigo);
mo.addAttribute("sp", sp);
return "out";
}
@RequestMapping(value= "/in1")
public String ko3() {
return "input1";
}
@RequestMapping(value= "/save1", method = RequestMethod.POST)
public String ko4(SpDTO spDTO) {
return "out1";
}
}
'SPRING' 카테고리의 다른 글
230227_MyBatis (0) | 2023.02.27 |
---|---|
230224_MyBatis (0) | 2023.02.24 |
230223_MyBatis (0) | 2023.02.23 |
230222_기본 (0) | 2023.02.22 |
230220_기본 (0) | 2023.02.20 |
댓글