(1)์์๋ DB๋ฑ์ ์ฐ๊ฒฐํ๊ณ seleteํ์ฌ jspํ์ด์ง์ ๊ฐ๋จํ๊ฒ ๋ฟ๋ ค๋ณด์๋ค.
์๋๋ ์ด๊ธฐํ๋ฉด์ด๋ค.
์ด๊ธฐํ๋ฉด์ ์ ์ฒด๋ณด๊ธฐํ๋ฉด์ด๋ค.
๋ฒ ์ด์ปค๋ฆฌ, ์ปคํผ๋ฅผ ํด๋ฆญํ๋ฉด ํ์ด์ง๋๊น ์์ด ajax๋ก type์ด ๋ฒ ์ด์ปค๋ฆฌ, ์ปคํผ์ธ ๊ฒ์ ์ถ๋ ฅ ํ ๊ฒ์ด๊ณ
๊ฐ๊ฒฉ์, ์ฃผ๋ฌธ์ ๋ฒํธ๋ฅ ํด๋ฆญํ๋ฉด ์ง๊ธ ํ์ฌ ํ์ด์ง์ ์๋ ๋ฐ์ดํฐ์์ ๊ฐ๊ฒฉ์, ์ฃผ๋ฌธ์์ผ๋ก ์ ๋ ฌ ํ ๊ฒ์ด๋ค.
-product.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>seoha_procudt</title>
</head>
<body>
<button id="product">์ ์ฒด๋ณด๊ธฐ</button>
<button id="bakery">๋ฒ ์ด์ปค๋ฆฌ</button>
<button id="coffee">์ปคํผ</button>
<button id="price_desc">๊ฐ๊ฒฉ์</button>
<button id="order_desc">์ฃผ๋ฌธ์</button>
<table border="1">
<tr id="title">
<th>id</th>
<th>name</th>
<th>price</th>
<th>orderCount</th>
<th>type</th>
</tr>
<c:forEach var="product" items="${products}">
<tr class="item">
<td>${product.id}</td>
<td>${product.name}</td>
<td>${product.price}</td>
<td>${product.orderCount}</td>
<td>${product.type}</td>
</tr>
</c:forEach>
</table>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
let type="";
$('#product').on('click', function() {
$.ajax({
type : 'GET',
url : '/product',
data : 'json'
}).done(function(result){
console.log(result);
$('.item').remove();
for(var r of result){
var item_el= "<tr class='item'><td>"+r.id+"</td><td>"+r.name+"</td><td>"+r.price+"</td><td>"+r.orderCount+"</td><td>"+r.type+"</td></tr>";
$('#title').after(item_el);
}
}).fail(function(result){
console.log('์๋ฌ');
console.log(result);
});;
});
$('#bakery').on('click', function(){
$.ajax({
type: 'GET',
url: '/product/๋ฒ ์ด์ปค๋ฆฌ',
dataType: 'json'
}).done(function(result){
type="๋ฒ ์ด์ปค๋ฆฌ";
console.log(result);
$('.item').remove();
for(var r of result){
var item_el= "<tr class='item'><td>"+r.id+"</td><td>"+r.name+"</td><td>"+r.price+"</td><td>"+r.orderCount+"</td><td>"+r.type+"</td></tr>";
$('#title').after(item_el);
}
}).fail(function(result){
console.log('์๋ฌ');
console.log(result);
});;
});
$('#coffee').on('click', function(){
$.ajax({
type: 'GET',
url: '/product/์ปคํผ',
dataType: 'json'
}).done(function(result){
type="์ปคํผ";
console.log(result);
$('.item').remove();
for(var r of result){
var item_el= "<tr class='item'><td>"+r.id+"</td><td>"+r.name+"</td><td>"+r.price+"</td><td>"+r.orderCount+"</td><td>"+r.type+"</td></tr>";
$('#title').after(item_el);
}
}).fail(function(result){
console.log('์๋ฌ');
console.log(result);
});;
});
$('#price_desc').on('click', function(){
$.ajax({
type: 'GET',
url: '/product/price/'+type,
dataType: 'json'
}).done(function(result){
console.log(result);
$('.item').remove();
for(var r of result){
var item_el= "<tr class='item'><td>"+r.id+"</td><td>"+r.name+"</td><td>"+r.price+"</td><td>"+r.orderCount+"</td><td>"+r.type+"</td></tr>";
$('#title').after(item_el);
}
}).fail(function(result){
console.log('์๋ฌ');
console.log(result);
});;
});
$('#order_desc').on('click', function(){
$.ajax({
type: 'GET',
url: '/product/orderByCount/'+type,
dataType: 'json'
}).done(function(result){
console.log(result);
$('.item').remove();
for(var r of result){
var item_el= "<tr class='item'><td>"+r.id+"</td><td>"+r.name+"</td><td>"+r.price+"</td><td>"+r.orderCount+"</td><td>"+r.type+"</td></tr>";
$('#title').after(item_el);
}
}).fail(function(result){
console.log('์๋ฌ');
console.log(result);
});;
});
</script>
</body>
</html>
ajax url์ ๋ฒ ์ด์ปค๋ฆฌ, ์ปคํผ๋ฅผ ํ๊ธ๋ก ๋ด๊ฐ ํ์ดํํ๋๋ฐ ์ง๋๋ฅผ ๋๊ฐ๋ค๋ณด๋ ๊ทธ ์์์ value๊ฐ์ ๋ค๊ณ ์์ ํ๋์ jax๋ฌธ์ผ๋ก ํฉ์น ์๋ ์์ ๊ฒ ๊ฐ๋น.
๊ทธ๋ฆฌ๊ณ ํ์ฌ ์ด๋ค ํ์ ์ผ๋ก ๋ฆฌ์คํธ๊ฐ ๋ฟ๋ ค์ ธ์๋์ง๋ฅผ ํ์ธํ ์ ์๋ let type๋ ์๋ฐ์ static์ ๊ธฐ๋ฅ๊ณผ๋ ๋น์ทํ let์ ์ฌ์ฉํ์ฌ ์ ์ฅํ์ฌ์ ์ฝ๊ฒ ๋ฟ๋ฆด์ ์์๋ค. ์ด๋ฐ ๊ธฐ๋ฅ์ด ์์๋๋ผ๋ฉด ๋ฒํผ์ ํด๋ฆญํ ๋๋ง๋ค ์ด๋ ํ ๋ณ์์ ์ด๊ฒ์ ๋ด์์ด์ผ๊ฒ ์ง..
- ProductController
package com.seoha.product.controller;
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.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import com.seoha.product.model.Product;
import com.seoha.product.repository.ProductRepository;
@Controller
public class ProductController {
@Autowired
private ProductRepository productRepository;
@GetMapping({ "", "/" })
public String index(Model model) {
List<Product> products = productRepository.findAll();
model.addAttribute("products", products);
return "product";
}
@GetMapping("/product")
public @ResponseBody List<Product> product() {
List<Product> products = productRepository.findAll();
return products;
}
@GetMapping("/product/{type}")
public @ResponseBody List<Product> productType(@PathVariable String type){
List<Product> products = productRepository.findType(type);
return products;
}
@GetMapping("/product/orderByCount/{type}")
public @ResponseBody List<Product> productOrderbyCount(@PathVariable String type){
List<Product> products = productRepository.orderByCount(type);
return products;
}
@GetMapping("/product/price/{type}")
public @ResponseBody List<Product> productOrderbyprice(@PathVariable String type){
List<Product> products = productRepository.orderByPrice(type);
return products;
}
}
- product.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper
namespace="com.seoha.product.repository.ProductRepository">
<select id="findAll" resultType="com.seoha.product.model.Product">
select * from product
</select>
<select id="findType" resultType="com.seoha.product.model.Product">
select * from product where type= #{type}
</select>
<select id="orderByPrice" resultType="com.seoha.product.model.Product">
select * from product where type= #{type} order by price desc;
</select>
<select id="orderByCount" resultType="com.seoha.product.model.Product">
select * from product where type= #{type} order by orderCount desc;
</select>
</mapper>
- ProductRepository
package com.seoha.product.repository;
import java.util.List;
import com.seoha.product.model.Product;
public interface ProductRepository {
List<Product> findAll();
List<Product> findType(String type);
List<Product> orderByPrice(String type);
List<Product> orderByCount(String type);
}
์ง์ง 1๋ ๋ชจ๋ฅด๊ฒ ๋๋ฐ ํ๋ค๋ณด๋๊น ์๊ฒ ๋ค..
๋ค๋ฅธ๊ฒ ๋ง๋ฒ์ด ์๋๊ณ ์ด๊ฒ ๋ง๋ฒ์ด๋ค..
'java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์คํ๋ง Session (0) | 2021.02.27 |
---|---|
AJAX ์ฌ์ฉ๋ฒ (0) | 2021.02.27 |
์คํ๋ง - ์ํ ์กฐํ ๊ฒ์ํ ๋ง๋ค๊ธฐ(1) (0) | 2021.02.27 |
์คํ๋ง - ์ ํจ์ฑ๊ฒ์ฌ, ํ์ผ์ ๋ก๋ (0) | 2021.02.27 |
์คํ๋ง mysql ์ฐ๊ฒฐํ๊ธฐ (0) | 2021.02.27 |