1. -스프링으로 가위바위보를 구현하시오.
<%@ 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>RPS MAIN</title>
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
crossorigin="anonymous"
/>
<script
src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
crossorigin="anonymous"
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
</head>
<body>
<header>
<c:forEach var="rps" items="${rpsList}">
<span class="rps">${rps}</span><br>
</c:forEach>
<br>
</header>
<main></main>
</body>
<script type="text/javascript">
let d = $(document);
d.ready(function() {
let header = $("header");
header.append($('<div id="rps1">'));
header.append($('<div id="rps2">'));
header.append($('<div id="result">'));
$(".rps").on("click", function() {
$.post("/rps/ajax", {
'rps': $(this).text()
},
function(response, status) {
$("#rps1").text("YOU: " + response.rps1);
$("#rps2").text("COMPUTER: " + response.rps2);
$("#result").text("RESULT: " + response.result);
});
});
});
</script>
</html>
package edu.sejong.ex.controller;
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 edu.sejong.ex.service.RPSService;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Controller
@RequestMapping("/rps")
public class RPSController {
@Autowired
private RPSService rpsService;
@GetMapping("/main")
public String list(Model model) {
log.info("list()..");
model.addAttribute("rpsList", rpsService.getRPSList());
return "/rps/main";
}
}
package edu.sejong.ex.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import edu.sejong.ex.service.RPSService;
import edu.sejong.ex.vo.RPSVO;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@RestController
@RequestMapping("/rps")
public class RPSRestController {
@Autowired
private RPSService rpsService;
@RequestMapping("/ajax")
public RPSVO ajax(@RequestParam("rps") String rps) {
log.info("ajax()..");
return rpsService.getJson(rps);
}
}
package edu.sejong.ex.service;
import edu.sejong.ex.vo.RPSVO;
public interface RPSService {
String[] getRPSList();
RPSVO getJson(String rps1);
}
package edu.sejong.ex.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import edu.sejong.ex.repository.RPSRepository;
import edu.sejong.ex.vo.RPSVO;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Service
public class RPSServiceImpl implements RPSService {
@Autowired
private RPSRepository rpsRepository;
@Override
public String[] getRPSList() {
log.info("getRPSList()..");
return rpsRepository.getRPSList();
}
@Override
public RPSVO getJson(String rps1) {
log.info("getJson()..");
return rpsRepository.getRPSVO(rps1);
}
}
package edu.sejong.ex.repository;
import edu.sejong.ex.vo.RPSVO;
public interface RPSRepository {
String[] getRPSList();
RPSVO getRPSVO(String rps1);
}
package edu.sejong.ex.repository;
import org.springframework.stereotype.Repository;
import consts.Const;
import edu.sejong.ex.vo.RPSVO;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Repository
public class RPSRepositoryImpl implements RPSRepository {
@Override
public String[] getRPSList() {
log.info("getRPSList()..");
return Const.rpsList;
}
@Override
public RPSVO getRPSVO(String rps1) {
log.info("getRPSVO()..");
return new RPSVO(rps1);
}
}
package edu.sejong.ex.vo;
import consts.Const;
import lombok.Getter;
@Getter
public class RPSVO {
private String rps1;
private String rps2;
private String result;
public RPSVO(String rps1) {
this.rps1 = rps1;
this.rps2 = Const.rpsList[(int) (Math.random() * 3.)];
if (this.rps1.equals(this.rps2)) {
this.result = "TIE";
} else if ((this.rps1.equals(Const.rpsList[0]) && this.rps2.equals(Const.rpsList[2]))
|| (this.rps1.equals(Const.rpsList[1]) && this.rps2.equals(Const.rpsList[0]))
|| (this.rps1.equals(Const.rpsList[2]) && this.rps2.equals(Const.rpsList[1]))) {
this.result = "WIN";
} else {
this.result = "LOSE";
}
}
}