私は自分のウェブサイトでRest WSを実装する方法を学び始めましたが、少し難解でした。私の考えは、非常に簡単な例から始めることです。基本を理解すると、より複雑なガイドを理解することができます。 2つのテキストフィールドを持つフォームがあるとすると、2つの数字を導入し、それらの2つの数字の乗算を示します。これは、それを達成するためのコードです:Grails Rest簡単な電卓の例
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>
結果ビュー:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sample title</title>
</head>
<body>
<h1>${sms}</h1>
</body>
</html>
私はのための助けが必要です を - 両方のhttpを処理し、要求を休みます(Grailsはどちらが要求されているか知っているでしょう) - リクエストを送信するための新しいクラスを作成します(2つの数字を入力として、 tを出力とする)。
PS。申し訳ありませんが、これはあまりにも基本的ですが、ウェブ上でこのような基本情報を見つけることができれば、本当にそのようなことは尋ねません。 ありがとうございます。 PP
あなたの答えに感謝します。私がクラスで意味することは、(クライアントとして)私がそのウェブサービスにアクセスする方法です。 – recoInrelax
あなたはそれにアクセスする方法のサンプルコードを提供してくれますか?私はそれが働くようになっていないよ – recoInrelax
あなたはあなたが働くことを得ていないことを説明できますか?あなたはRESTサービスにアクセスする際にjavascriptやgroovyが必要なのでしょうか? – Todd