Skip to content

Instantly share code, notes, and snippets.

@hw0k
Created March 18, 2019 03:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hw0k/85e5bb7f95eb8d9d6eddae7e83fe9993 to your computer and use it in GitHub Desktop.
Save hw0k/85e5bb7f95eb8d9d6eddae7e83fe9993 to your computer and use it in GitHub Desktop.
190318 Spring Study
package kr.hs.dgsw.calculator;
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;
@RestController
public class CalculatorController {
@Autowired
private CalculatorService calculatorService;
@RequestMapping("/add")
public String add(@RequestParam String par1, @RequestParam String par2) {
return calculatorService.add(par1, par2);
}
@RequestMapping("/subtract")
public String subtract(@RequestParam String par1, @RequestParam String par2) {
return calculatorService.subtract(par1, par2);
}
@RequestMapping("/multiply")
public String multiply(@RequestParam String par1, @RequestParam String par2) {
return calculatorService.multiply(par1, par2);
}
@RequestMapping("/divide")
public String divide(@RequestParam String par1, @RequestParam String par2) {
return calculatorService.divide(par1, par2);
}
}
package kr.hs.dgsw.calculator;
public interface CalculatorService {
String add(String par1, String par2);
String subtract(String par1, String par2);
String multiply(String par1, String par2);
String divide(String par1, String par2);
}
package kr.hs.dgsw.calculator;
import org.springframework.stereotype.Service;
@Service
public class CalculatorServiceImpl implements CalculatorService {
@Override
public String add(String par1, String par2) {
return "계산 결과: " + (Integer.parseInt(par1) + Integer.parseInt(par2));
}
@Override
public String subtract(String par1, String par2) {
return "계산 결과: " + (Integer.parseInt(par1) - Integer.parseInt(par2));
}
@Override
public String multiply(String par1, String par2) {
return "계산 결과: " + (Integer.parseInt(par1) * Integer.parseInt(par2));
}
@Override
public String divide(String par1, String par2) {
return "계산 결과: " + (Integer.parseInt(par1) / Integer.parseInt(par2));
}
}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<input type="text" id="par1">
<input type="text" id="par2">
<br>
<label for="op">연산자</label>
<select name="op" id="op">
<option value="add">+</option>
<option value="subtract">-</option>
<option value="multiply">*</option>
<option value="divide">/</option>
</select>
<br>
<button onclick="send()">전송</button>
<br>
<p id="result"></p>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.min.js"></script>
<script>
const send = async () => {
let resultElem = document.getElementById("result");
const requestData = {
op: document.getElementById("op").value,
par1: document.getElementById("par1").value,
par2: document.getElementById("par2").value
};
try {
const res = await axios.get(`/${requestData.op}?par1=${requestData.par1}&par2=${requestData.par2}`);
resultElem.innerText = res.data;
}
catch (err) {
resultElem.innerText = "에러 발생";
}
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment