ColdFusion コンポーネント使い方
6359 ワード
ColdFusion コンポーネント使い方
cfcにDBの作業や、if等の分岐作業をまとめておく。
その際のcfcomponentの使い方。
cfmで使う側
- cfinvoke cfcのどのfunctionを使うか指示する
- method cfcのfunction要素
- component cfcのファイル名
- returnVariable 戻り値を受け取る名前
- cfinvokeargumentで渡したい引数を準備する(ないならなし)
- form送信した内容をusernameに入れて引数で渡している。
<!---ユーザ追加のデータ渡す--->
<cfif isDefined("form.action") and form.action is "add">
<cfinvoke method="createuser" component="login_function" returnvariable="message">
<cfinvokeargument name="username" value="#form.username#">
<cfinvokeargument name="password" value="#form.password#">
</cfinvoke>
<cfoutput>#message#</cfoutput>
</cfif>
<h2>ユーザの追加</h2>
<form action="<cfoutput>#cgi.script_name#</cfoutput>" method="post">
<input type="hidden" name="action" value="add">
ユーザー名:<input type="text" name="username" size="20"><br>
パスワード:<input type="password" name="password" size="20"><br>
<br>
<input type="submit" value="追加">
</form>
cfcで作業する側
- cfcomponent コンポーネントはまとめて1つでOK
- 名前を付けても多分使わない。cfmで使う名前はファイル名 login_function.cfcを使うので。
- cffunction
- name cfmで付けた名前で
- access remote/public/private…
- returntype 戻り値の型。 複数⇒struct、DB参照の結果⇒query
- cfargument 引数の受け取り(ないならなし)
- name 名前を付ける。cfmと一緒の方が分かりやすい
- type 型
- required 引数の受け取りが必須かどうか
- cfreturn 戻り値(ないならなし)
- 返したいものを記入する。DBの結果ならcfqueryにつけた名前。
<!---ユーザの追加--->
<cfcomponent>
<cffunction name="createuser" access="remote" returntype="string">
<cfargument name="username" type="string" required="true">
<cfargument name="password" type="string" required="true">
<cfquery datasource="sample" name="adduserck">
select *
from accounttable
where username = '#arguments.username#'
</cfquery>
<cfif adduserck.recordcount gt 0>
<cfset message = "#arguments.username#様は既に存在しているため登録できません">
<cfelse>
<cfquery datasource="sample" name="useradd">
insert into accounttable values (
'#arguments.username#', '#encrypt(arguments.password, "aaa")#',null,null,null,0,0,null,0,null
)
</cfquery>
<cfset message = "#arguments.username#様を追加しました">
</cfif>
<cfreturn message>
</cffunction>
</cfcomponent>
Author And Source
この問題について(ColdFusion コンポーネント使い方), 我々は、より多くの情報をここで見つけました https://qiita.com/sumichan/items/f6a426564025830bdeea著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .