티스토리 뷰

 

 

Controller

 

MatchController.java

package com.springmvc.controller;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

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.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.springmvc.domain.Game;
import com.springmvc.domain.Match;
import com.springmvc.service.GameService;
import com.springmvc.service.MatchService;

@Controller
@RequestMapping("/match")
public class MatchController {

	@Autowired
	private GameService gameService;
	
	@Autowired
	private MatchService matchService;
	
	@GetMapping
	public String requestGameById(@RequestParam("id") String gameId, Model model) {
		Game gameById = gameService.readGameById(gameId);
		model.addAttribute("game",gameById);
		
		return "/Game/game";
	}
	
	
	@GetMapping("/add")
	public String requestAddMatchForm(@ModelAttribute("addMatch") Match match, @RequestParam("id") String gameId, Model model) {
		
//		Game game = new Game();
//		String gameId = game.getGameId();
		//model.addAttribute("match",gameById);
		model.addAttribute("gameId", gameId);
		
		return "/Game/addMatch";
	}
	
	@PostMapping("/add")
	public String submitAddMatch(@ModelAttribute("addMatch") Match match, @RequestParam("id") String gameId, Model model, HttpServletRequest request) {
		System.out.println("POSTMAPPING ADD 도착");
		System.out.println("Game ID from URL: " + gameId);
		
		//Game game = new Game();
		//String gameId = game.getGameId();
		match.setGameId(gameId);
		
		
		MultipartFile img = match.getGameImage();
		
		String saveName = img.getOriginalFilename();
		String save = request.getSession().getServletContext().getRealPath("/resources/images");
		File saveFile = new File(save,saveName);
		
		if(img!=null&&!img.isEmpty()) {
			try {
				img.transferTo(saveFile);
				match.setFileName(saveName);
			}
			catch(Exception e) {
				throw new RuntimeException("사진 등록 실패");
			}
		}
		System.out.println("filname="+match.getFileName());
		matchService.setNewMatch(match);
		return "redirect:/games";
	}
	
	@GetMapping("/update")
	public String getUpdateMatchForm(@ModelAttribute("updateMatch") Match match, @RequestParam("id")String gameId, Model model) {
		System.out.println("gameId = "+gameId);
		Match gameById = matchService.readMatchById(gameId);
		model.addAttribute("game",gameById);
		
		return "/Game/updateMatch";
	}
	
	@PostMapping("/update")
	public String submitUpdateMatchForm(@ModelAttribute("updateMatch") Match match, HttpServletRequest request, Model model) {
		
		MultipartFile img = match.getGameImage();
		String saveName = img.getOriginalFilename();
		String save = request.getSession().getServletContext().getRealPath("/resources/images");
		File saveFile = new File(save,saveName);
		
		if(img!=null&&img.isEmpty()) {
			try {
				img.transferTo(saveFile);
				match.setFileName(saveName);
			}
			catch(Exception e) {
				throw new RuntimeException("사진 등록 실패");
			}
		}
		
		matchService.setUpdateMatch(match);
		return "redirect:/games";
	}
	
	@RequestMapping("/delete")
	public String getDeleteForm(Model model, @RequestParam("id")String gameId) {
		System.out.println("delete시작");
		System.out.println(gameId+"삭제됨!!!");
		matchService.setDeletMatch(gameId);
		return "redirect:/games";
	}
}

 

 

 


Domain

Match.java

package com.springmvc.domain;

import java.io.Serializable;

import org.springframework.web.multipart.MultipartFile;

public class Match implements Serializable {


	private static final long serialVersionUID = 1226981758454687975L;
	
	private Game game;
	private String gameId;  //게임아이디
	private String teamId;  //팀아이디 not null

	private String teamName;  //팀명 not null

	//private String date;  //경기날짜
	private String userId;   //사용자 아이디
	private String userName;  //사용자 이름
	private String userNumber;   //사용자전화번호
	//private String stadium;   //경기장
	private String fileName;
	private MultipartFile gameImage;
	public Match() {
		super();
		// TODO Auto-generated constructor stub
	}
	public String getGameId() {
		return gameId;
	}
	public void setGameId(String gameId) {
		this.gameId = gameId;
	}
	public String getTeamId() {
		return teamId;
	}
	public void setTeamId(String teamId) {
		this.teamId = teamId;
	}

	public String getTeamName() {
		return teamName;
	}
	public void setTeamName(String teamName) {
		this.teamName = teamName;
	}
	
//	public String getDate() {
//		return date;
//	}
//	public void setDate(String date) {
//		this.date = date;
//	}
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserNumber() {
		return userNumber;
	}
	public void setUserNumber(String userNumber) {
		this.userNumber = userNumber;
	}
//	public String getStadium() {
//		return stadium;
//	}
//	public void setStadium(String stadium) {
//		this.stadium = stadium;
//	}
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public MultipartFile getGameImage() {
		return gameImage;
	}
	public void setGameImage(MultipartFile gameImage) {
		this.gameImage = gameImage;
	}
	public Game getGame() {
		return game;
	}
	public void setGame(Game game) {
		this.game = game;
	}
	

}

 

 

 


Repository

 

MatchRepository.java

package com.springmvc.repository;

import com.springmvc.domain.Match;

public interface MatchRepository {
	
	Match readMatchById(String gameId);
	
	void setNewMatch(Match match);
	void setUpdateMatch(Match match);
	void setDeletMatch(String gameId);
}

 

 

MatchRepositoryImpl.java

package com.springmvc.repository;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.springmvc.domain.Match;


@Repository
public class MatchRepositoryImpl implements MatchRepository{

	private JdbcTemplate template;
	
	@Autowired
	public void setJdbcTemplate(DataSource dataSource) {
		this.template=new JdbcTemplate(dataSource);
	}
	
	@Override
	public Match readMatchById(String gameId) {

	    Match matchInfo = null;
	    
	    String SQL = "select count(*) from matching where g_id=?";
	    
	    int rowCount = template.queryForObject(SQL, Integer.class, gameId);
	    
	    if(rowCount != 0) {
	        SQL = "select * from matching where g_id=?";
	        matchInfo = template.queryForObject(SQL, new Object[] {gameId}, new MatchRowMapper());
	    }
	    if(rowCount == 0) {
	        throw new IllegalArgumentException("일치하는 항목이 없습니다.");
	    }
	    return matchInfo;
	}

	@Override
	public void setNewMatch(Match match) {
		String SQL = "insert into matching(g_id,t_id,t_name,userid,username,userphone,g_filename)"+"values(?,?,?,?,?,?,?)";
		
		template.update(SQL,match.getGameId(), match.getTeamId(), match.getTeamName(), match.getUserId(),match.getUserName(),match.getUserNumber(),match.getFileName());
		System.out.println("setNew도착");
		System.out.println(match.getFileName());
	}

	@Override
	public void setUpdateMatch(Match match) {
		
		if(match.getFileName()!=null) {
			String SQL = "UPDATE matching SET t_id=?,t_name=?,userid=?,username=?,userphone=?,g_stadium=?,g_filename=? WHERE g_id=?";
			template.update(SQL, match.getTeamId(), match.getTeamName(), match.getUserId(),match.getUserName(),match.getUserNumber(),match.getFileName(),match.getGameId());
		}
		else if(match.getFileName()==null) {
			String SQL = "UPDATE matching SET t_id=?,t_name=?,userid=?,username=?,userphone=? WHERE g_id=?";
			template.update(SQL, match.getTeamId(), match.getTeamName(), match.getUserId(),match.getUserName(),match.getUserNumber(),match.getGameId());
		}
		
	}

	@Override
	public void setDeletMatch(String gameId) {
		String SQL = "DELETE FROM matching WHERE g_id=?";
		this.template.update(SQL, gameId);
		
	}

}

 

 


Service

 

MatchService.java

package com.springmvc.service;

import com.springmvc.domain.Match;

public interface MatchService {

	Match readMatchById(String gameId);
	
	void setNewMatch(Match match);
	void setUpdateMatch(Match match);
	void setDeletMatch(String gameId);
}

 

MatchServiceImpl.java

package com.springmvc.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.springmvc.domain.Match;
import com.springmvc.repository.MatchRepository;

@Service
public class MatchServiceImpl implements MatchService{

	@Autowired
	MatchRepository matchRepository;
	
	@Override
	public Match readMatchById(String gameId) {
		Match gameById = matchRepository.readMatchById(gameId);
		return gameById;
	}

	@Override
	public void setNewMatch(Match match) {
		matchRepository.setNewMatch(match);
		
	}

	@Override
	public void setUpdateMatch(Match match) {
		matchRepository.setUpdateMatch(match);
		
	}

	@Override
	public void setDeletMatch(String gameId) {
		matchRepository.setDeletMatch(gameId);
		
	}

}

 

 

 


View

 

 

 

 

 

 

 

 

addMatch.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>   
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h3>매칭 페이지</h3>

	<form:form modelAttribute="addMatch" action="./add?id=${gameId}" class="form-horizontal" enctype="multipart/form-data" method="post">
		
		<p> 팀 아이디 : <form:input path="teamId" value="${match.teamId}"/>
		<p> 팀 이름 : <form:input path="teamName" value="${match.teamName}"/>
		<p> 예약자 아이디 : <form:input path="userId" value="${match.userId}"/>
		<p> 예약자명 : <form:input path="userName" value="${match.userName}"/>
		<p> 예약자 번호 : <form:input path="userNumber" value="${match.userNumber}"/>
		<div class="col-sm-10">
			이미지<form:input path="gameImage" type="file" class="form-control"/>
		</div>
		<div class="form-group row">
			<input type="submit" class="btn btn-primary" value="등록"/>
			<a href="<c:url value="/games"/>" class="btn btn-primary">취소</a>
		</div>
		${gameId }
	</form:form>

</body>
</html>

 

 

 

 

 

 

 

 

 

 

 

updateMatch.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>   
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h3>수정 페이지</h3>
	<div class="col-md-4">
		<img src="<c:url value='/resources/images/${match.fileName}'/>" alt="image" style="width: 20%"/>
	</div>
	<form:form modelAttribute="updateMatch" action="./update?id=${match.gameId}" class="form-horizontal" enctype="multipart/form-data" method="post">
		<p> 게임 아이디 : <form:input path="gameId" value="${match.gameId}" readonly="readonly"/>
		<p> 팀 아이디 : <form:input path="teamId" value="${match.teamId1}"/> <!-- path 는 DTO 객체의 변수 -->		
		<p> 팀 이름 : <form:input path="teamName" value="${match.teamName}"/>	
		<p> 예약자 아이디 : <form:input path="userId" value="${match.userId}"/>
		<p> 예약자명 : <form:input path="userName" value="${match.userName}"/>
		<p> 예약자 번호 : <form:input path="userNumber" value="${match.userNumber}"/>
		<div class="col-sm-10">
			이미지<form:input path="gameImage" type="file" class="form-control"/>
		</div>
		<div class="form-group row">
			<input type="submit" class="btn btn-primary" value="수정"/>
			<a href="<c:url value="/games"/>" class="btn btn-primary">취소</a>
		</div>
		${game }
	</form:form>

</body>
</html>

 

 

 

 

 

매칭 취소 클릭 (delete 실행)

 

 

 

 

 

 

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday