//Google AdSense

DI : 의존성 주입

자바기반 설정방식이나 XML기반 설정방식에서 명시적으로 Bean을 선언하는 것과 달리
DI 컨테이너에 빈을 자동으로 주입하는 방식
MainService.java
package ksmart35.springboot.service;

import java.util.List;

import org.springframework.stereotype.Service;

import ksmart35.springboot.dao.Member;

@Service
public class MainService {
	
	public List<Member> repeat (List<Member> list) {
		for(int i = 1; i <=10;i++) {
			 list.add(new Member("id00"+i,"pw00"+i,"홍0"+i,"홍0"+i+"@ksmart.or.kr"));
		}
		return list;
	}
}
MainController.java 
>Autowired
package ksmart35.springboot.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import ksmart35.springboot.dao.Member;
import ksmart35.springboot.service.MainService;

@Controller
public class MainController {
	//DI 의존성 주입
	 @Autowired
	 private MainService mainService;
	
	 @GetMapping("/exam5")
	 public String exam5 (Model model) {
		 String title = "ksmart35";
		 List<Member> list = new ArrayList<Member>();
		 //객체생성
		    model.addAttribute("memberList", mainService.repeat(list));
		 return "exam/exam5";
	 }
}	

 

 


지역변수

MainController.java
package ksmart35.springboot.controller;

import java.util.ArrayList;
import java.util.List;

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

import ksmart.springboot.dao.Member;

@Controller
public class MainController {

	 @GetMapping("/exam5")
	 public String exam5 (Model model) {
		 String title = "ksmart35";
		 List<Member> list = new ArrayList<Member>();
			 for(int i = 1; i <=10;i++) {
				 list.add(new Member("id00"+i,"pw00"+i,"홍0"+i,"홍0"+i+"@ksmart.or.kr"));
			}
		    System.out.println(list.toString() + "<--list");
		    model.addAttribute("memberList", list);
		 return "exam/exam5";
	 }
}	
exam5.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymleaf.org">
<head>
<meta charset="UTF-8">
<title th:text="${title}" ></title>
</head>
<body>
	<!-- 지역변수 -->
	<!-- 조건문 -->
	<table border="1" th:with="target='지역변수'">
		<thead>
			<tr>
				<th>구분</th>
				<th>아이디</th>
				<th>비밀번호</th>
				<th>이름</th>
				<th>이메일</th>
			</tr>
		</thead>
		<tbody>
			<tr th:each="m, i : ${memberList}">
				<td th:text="${i.index}"></td>
				<td th:text="${m.mId}"></td>
				<td th:text="${m.mPw}"></td>
				<td th:text="${m.mName}"></td>
				<td th:text="${m.mEmail}"></td>
			</tr>
		</tbody>
		<tfoot>
			<tr>
				<td colspan="5" th:text="${target}"></td>
			</tr>
		</tfoot>
	</table>
</body>
</html>


조건문

<!DOCTYPE html>
<html xmlns:th="http://www.thymleaf.org">
<head>
<meta charset="UTF-8">
<title th:text="${title}" ></title>
</head>
<body>
	<!-- 지역변수 -->
	<!-- 조건문 -->
	<table border="1" th:with="target='지역변수', id='id001'">
		<thead>
			<tr>
				<th>구분</th>
				<th>아이디</th>
				<th>비밀번호</th>
				<th>이름</th>
				<th>이메일</th>
			</tr>
		</thead>
		<tbody>
			<tr th:each="m, i : ${memberList}">
				<td th:text="${i.index}"></td>
				<td th:if="${m.mId == id}" style="color: red;" th:text="${m.mId}"></td>
				<td th:unless="${m.mId == id}" th:text="${m.mId}"></td>
				<td th:text="${m.mPw}"></td>
				<td th:text="${m.mName}"></td>
				<td th:text="${m.mEmail}"></td>
			</tr>
		</tbody>
		<tfoot>
			<tr>
				<td colspan="5" th:text="${target}"></td>				
			</tr>
		</tfoot>
	</table>
</body>
</html>

 


<!DOCTYPE html>
<html xmlns:th="http://www.thymleaf.org">
<head>
<meta charset="UTF-8">
<title th:text="${title}" ></title>
</head>
<body>
	<!-- 지역변수 -->
	<!-- 조건문 -->
	<table border="1" th:with="target='지역변수', id='id001'">
		<thead>
			<tr>
				<th>구분</th>
				<th>아이디</th>
				<th>비밀번호</th>
				<th>이름</th>
				<th>이메일</th>
			</tr>
		</thead>
		<tbody>
			<tr th:each="m, i : ${memberList}">
				<td th:text="${i.index}"></td>
				<td th:if="${m.mId == id}" style="color: red;" th:text="${m.mId}"></td>
				<td th:unless="${m.mId == id}" th:text="${m.mId}"></td>
				<td th:text="${m.mPw}"></td>
				<th:block th:if="${m.mId eq id}">
					<td th:text="${m.mName}" style="color: red;"></td>
					<td th:text="${m.mEmail}" style="color: red;"></td>
				</th:block>
				<th:block th:if="${m.mId ne id}">
					<td th:text="${m.mName}"></td>
					<td th:text="${m.mEmail}"></td>
				</th:block>
			</tr>
		</tbody>
		<tfoot>
			<tr>
				<td colspan="5" th:text="${target}"></td>				
			</tr>
		</tfoot>
	</table>
</body>
</html>


Basic Objects

MainController.java
package ksmart35.springboot.controller;

import java.util.ArrayList;
import java.util.List;

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

import ksmart.springboot.dao.Member;

@Controller
public class MainController {
	 @GetMapping("/exam6")
	 public String exam6 (Model model) {
		 String title = "ksmart35";
		 model.addAttribute("title", title);
		 return "exam/exam6";
	 }
}	
exam6.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymleaf.org">
<head>
<meta charset="UTF-8">
<title th:text="${title}" ></title>
</head>
<body>
	<div th:with="nowValue=${#dates.createNow()}, price=12345">
		<h1>[[${nowValue}]]</h1><br>
		<h1 th:text="${#dates.format(nowValue, 'yyyy-MM-dd HH:mm:ss')}"></h1><br>
		<h1 th:text="${#numbers.formatInteger(price,3,'COMMA')}"></h1><br>
	</div>
</body>
</html>

createToday로 적용하면 날짜만 제대로 출력되고 시간은 00:00:00으로 나온다.


Link

MainController.java

package ksmart35.springboot.controller;

import java.util.ArrayList;
import java.util.List;

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

import ksmart.springboot.dao.Member;

@Controller
public class MainController {
	 @GetMapping("/exam7")
	 public String exam7 (Model model) {
		List<String> list = new ArrayList<String>();
		String title = "ksmart35 hungry";
		model.addAttribute("title",title);
		 return "exam/exam7";
	 }
}	

exam7.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymleaf.org">
<head>
<meta charset="UTF-8">
<title th:text="${title}" ></title>
</head>
<body>
	<!-- 링크 연결 @{/exam1}  -->
	<th:block th:each="num : ${#numbers.sequence(1,6)}">
	<a th:href="@{${'/exam' + num + '?' +}}">[[${'exam' + num}]]</a>
	</th:block>
</body>
</html>


get방식으로 값을 전달

> @{${'/exam' + num}(id='id001',pw='pw001')}

<!DOCTYPE html>
<html xmlns:th="http://www.thymleaf.org">
<head>
<meta charset="UTF-8">
<title th:text="${title}" ></title>
</head>
<body>
	<!-- 링크 연결 @{/exam1}  -->
	<th:block th:each="num : ${#numbers.sequence(1,6)}">
	<a th:href="@{${'/exam' + num}(id='id001',pw='pw001')}">[[${'exam' + num}]]</a>
	</th:block>
</body>
</html>

get방식으로 전달한 값을 받는 처리

MainController.java
>null 처리에 유의하자.
package ksmart35.springboot.controller;

import java.util.ArrayList;
import java.util.List;

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

import ksmart.springboot.dao.Member;

@Controller
public class MainController {
 
	 @RequestMapping(value = "/exam1", method = RequestMethod.GET)
	 public String exam1(Model model
			 			, @RequestParam(name = "id",required = false) String id
			 			, @RequestParam(name = "pw",required = false) String pw) {
		 String title = "ksmart35";
		 Member member1 = new Member();
		 member1.setmId("id001");
		 Member member2 = new Member();
		 member2.setmId("id002");
		 model.addAttribute("title", title);
		 model.addAttribute("member1", member1);
		 model.addAttribute("member2", member2);
         
		 if(id!=null && !"".equals(id)) {
			 System.out.println( id + " < id");
			 model.addAttribute("id", id);
		 }
		 if(pw!=null && !"".equals(pw)) {
			 System.out.println( pw + " < pw");
			 model.addAttribute("pw", pw);
		 }
		 		 
		 return "exam/exam1";
	 }
}	
exam1.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymleaf.org">
<head>
<meta charset="UTF-8">
<title th:text="${title}" ></title>
</head>
<body>
	EXAMPLE1
	<label th:text="${member1.mId}"></label>
	<label th:text="${member2.mId}"></label><br>
	<label th:text="${'id : '+ id}"></label><br>
	<label th:text="${'pw : ' + pw}"></label>
	<label></label>
</body>
</html>


Command 객체 사용

>DTO,DAO 사용해야하는 작업이 매우 간략해짐

MainController.java
package ksmart35.springboot.controller;

import java.util.ArrayList;
import java.util.List;

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

import ksmart.springboot.dao.Member;

@Controller
public class MainController {

	 @RequestMapping(value = "/exam1", method = RequestMethod.GET)
	 public String exam1(Model model
			 			, Member member) {
		 String title = "ksmart35";
		 Member member1 = new Member();
		 member1.setmId("id001");
		 Member member2 = new Member();
		 member2.setmId("id002");
		 model.addAttribute("title", title);
		 model.addAttribute("member1", member1);
		 model.addAttribute("member2", member2);
		 		 
		 if(member !=null && !"".equals(member)) {
			 System.out.println( member + " < member");
			 model.addAttribute("member", member);
		 }
		 
		 return "exam/exam1";
	 }
}

 

exam1.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymleaf.org">
<head>
<meta charset="UTF-8">
<title th:text="${title}" ></title>
</head>
<body>
	EXAMPLE1
	<label th:text="${member1.mId}"></label>
	<label th:text="${member2.mId}"></label><br>
	
	<label th:text="${'id : ' + member.mId}"></label><br>
	<label th:text="${'pw : ' + member.mPw}"></label>
	<label></label>
</body>
</html>


 

'SpringBoot' 카테고리의 다른 글

[MyBatis] mybatis03  (0) 2020.05.27
[MyBatis] mybatis02 Dynamic SQL  (0) 2020.05.26
[MyBatis] mybatis01  (0) 2020.05.25
[SpringBoot] Thymeleaf layout  (0) 2020.05.19

+ Recent posts