내 웹 사이트에 Rest WS를 구현하는 방법을 배우기 시작했지만 조금 어려웠습니다. 제 생각은 아주 간단한 예제로 시작하는 것입니다. 기본을 이해하면 더 복잡한 가이드를 이해할 수 있습니다. 2 개의 텍스트 필드가있는 양식이 있다고 가정하면 2 개의 숫자를 소개 한 다음이 두 숫자의 곱셈이 표시됩니다.Grails Rest 간단한 계산기 예
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sample title</title>
</head>
<body>
<h1>${sms}</h1>
</body>
</html>
나에 대한 도움이 필요합니다 :
class CalculatorController {
def index = { }
def calc = {
def nr_1 = params.first_nr
def nr_2 = params.second_nr
def result
def erro = 'no'
if(nr_1.isInteger() && nr_2.isInteger())
result = nr_1.toInteger() * nr_2.toInteger()
else
erro = 'yes'
chain(action:"print_result", model:[erro: erro, result: result, nr1: nr_1, nr2: nr_2])
}
def print_result = {
if(chainModel.erro.equals('yes'))
[sms : 'Please introduce only 2 numbers!']
else
[sms: 'The result of the multiplication of ' + chainModel.nr1 + ' with ' + chainModel.nr2 + ' is ' + chainModel.result]
}
}
홈페이지보기 :
<html>
<head>
<title></title>
<meta name="layout" content="main" />
<style type="text/css" media="screen">
</style>
</head>
<body>
This program is a calculator:<br><br>
<g:form name="myForm" action="calc">
<h1>Introduce first number: </h1><g:textField name="first_nr" value="${myValue}" /> <br>
<h1>Introduce second number: </h1><g:textField name="second_nr" value="${myValue}" /> <br>
<g:submitButton name="update" value="Update" />
</g:form>
</body>
</html>
결과보기 이것은 그 수행하는 코드입니다 을 - 처리 HTTP와 나머지 요청 (Grails는 어느 것이 요청되는지를 알 것이다) - 요청을 보내기위한 새로운 클래스를 생성한다 (두 개의 숫자를 입력으로, 출력으로 t).
추신. 미안하지만 이것은 너무 기본적인 것이지만 웹상에서 그러한 기본 정보를 찾을 수 있다면 정말 그런 것을 묻지 않을 것입니다. 미리 감사드립니다. PP
답변 해 주셔서 감사합니다. 내가 수업에서 말하는 것은 (클라이언트로서) 내가 그 웹 서비스에 접근 할 수있는 방법이다. – recoInrelax
어떻게 접근 할 수 있는지 예제 코드를 제공해 주시겠습니까? 나는 그것을 작동시키지 않을거야. – recoInrelax
당신이 일을 못하고있는 이유를 설명 할 수 있니? REST 서비스에 액세스 할 때 javascript 또는 groovy를 원하십니까? – Todd