2012年12月3日月曜日

ものぐさな院生のDWR入門

どうも,立ち位置が微妙な院生の矢ヶ崎です.
3日目は僕が使ってるDWRというものについて書いていきます.

DWRって?

JavaScriptから直接Javaのメソッドを叩けるよく分からない技術です.
研究室で数年前流行ってました(今は多分僕しか使ってません).


なんで使ってるの?

クライアント,サーバ間で通信する為の記述が面倒臭いから使ってます.
AjaxだのPostだのでリクエスト飛ばしてgetAttributeでどーのこーのして
処理が終わったらレスポンスにイソイソ値を入れてとか・・・

いちいち書くのが面倒くせぇ!!ので使ってます.
因みにセキュリティ的にどうのとか書き方的にどうのとか全く気にしてないので知りません.
あとこれしか書けることがn・・・ゲフン
そこ,「またDWRかよ…」とか言わない;


何が良いの?

  • サーブレットを書く必要が無い.
  • だからrequestやらresponseやらに値を出し入れする事もない.
  • Javaさえ書ければ簡単にサーバー側を書ける!(多分).

DWRについてもちっと詳く

JavaScriptからJavaのメソッドが直接叩けます.
例えばControllerクラスのloginCheck(String,String)みたいなメソッドがあれば
JavaScript側でController.loginCheck("admin","areare");で呼び出せます.

準備

1:DWRからdwr.jarを落とす.
2:web.xmlのweb-app以下に下記を追加する.

 Direct Web Remoter Servlet
 DWR Servlet
 dwr-invoker
 uk.ltd.getahead.dwr.DWRServlet
 
  debug
  true
 


 dwr-invoker
 /dwr/*

3:WEB-INF以下にdwr.xmlを作成する.


 
  
 


例:testパッケージ内のCalculatorクラスをcalcという名前で
アプリケーションが終了するまでインスタンスを生存させる場合.



これでおしまい.

scopeについて

DWRで生成されるJavaインスタンスの生存期間が指定できます.
何も書かないとメソッドを叩く度に生成されます.

  • application アプリケーション全体で一つのインスタンスを生成. 
  • session セッション単位. 
  • request リクエスト毎.
  • page ページ毎.
  • script ページを表示している間.

確認の仕方

アプリケーションを実行しアドレスの末尾に「/dwr」とつけてアクセスするとデバッグページが表示されます.(http://localhost:8080/application/dwrみたいな感じ)


使える引数

JavaとJS間では以下のように値を受け渡しできます.
Java ⇔ JavaScript
boolean  論理型
Byte,Short,Integer,Long,Float,Double  数値型
Character,String  文字列型
JavaBeansとJavaオブジェクトも受け渡し可能です.

使い方(JS側)

HTMLに以下を記述する.(Helloクラスの場合)




これでHelloクラスのメソッドを叩くことができます.
例えば下記のようなメソッドがある場合
public String helloName(String name){
 return "Hello "+name;
}
js側でこんな風に呼び出します.(コンソールに「Hello Yaga」って出ます)
Hello.helloName("Yaga",callbackFunc);
function callbackFunc(returnData){
 console.log(returnData);
}
引数が1つ多いと思いますが,DWRでJavaメソッドを呼び出すと
最後の引数+1にメソッドの戻り値が返ってきます.
上記の場合helloNameメソッドの結果をcallbackFunk関数で受け取ってます.
(引数のreturnDataに戻り値が入ってます)

引数にJavaBeansやJavaオブジェクトを使う方法

dwr.xmlのallow以下に以下のように書きます.
JavaBeans以外のJavaオブジェクトを利用する場合は
converterをobjectにします.

後はreturnData.propertyのようにアクセスできます.
var name;
var id;
var age;
School.getStudentData("studentName",function(returnData){
 name = returnData.name;
 id = returnData.id;
 age = returnData.age;
 console.log("学籍番号:"+id+" 氏名:"+name+" 年齢:"+age);
});
var addStudentData = {name:"Meso",id:"C123456",age:10};
School.setStudentData(addStudentData);
10~11行目はBeanDataをサーバー側に送る記述を書いてます.

ちょっと便利なutil関数

JS側に書いてインポートしてるutil.jsを使って色々できます.
ここにはその一部を紹介します.

dwr.util.getValue(id)

引数で指定したid属性を取ってきます,まとめて取ってくるgetValuesってのもあります.

dwr.util.setValue(id,value)

引数で指定したid属性の値を変更します.

dwr.util.addOptions(id,OptionData)

idで指定したセレクトボックスのOptionを追加します.
サーバーからオプションの一覧をStringの配列で返して第2引数に突っ込むだけでOKです.

他にも色々あるので興味のある人は見てみて下さいutil.js


以上になります
特別,必要な技術って訳じゃないですが
やってみると意外と便利だったりするので興味のある人は是非試してみてください.

そんなわけで4日目は前回node.jsについて書いてくれた高木君にバトンタッチします.

それではまた ノシ

41 件のコメント:

  1. Thanks for sharing, nice post! Post really provice useful information!

    Giaonhan247 chuyên dịch vụ mua hộ hàng mỹ với các mặt hàng từ pandora úc hay từ web ebay việt nam cùng với bảng giá gửi hàng đi mỹ uy tín, giá rẻ.

    返信削除
  2. คาสิโนออนไลน์ที่น่าเชื่อถือและมีความเป็นมืออาชีพที่สุดในตอนนี้
    โปรโมชั่นGclub ของทางทีมงานตอนนี้แจกฟรีโบนัส 50%
    เพียงแค่คุณสมัคร สล็อตออนไลน์ กับทางทีมงานของเราเพียงเท่านั้น
    ร่วมมาเป็นส่วนหนึ่งกับเว็บไซต์คาสิโนออนไลน์ของเราได้เลยค่ะ
    สมัครสล็อตออนไลน์ >>> Goldenslot
    สนใจร่วมสนุกกับ คาสิโนออนไลน์ คลิ๊กได้เลย
    มีทั้งคาสิโนออนไลน์ หวยออนไลน์ ฟุตบอลออนไลน์ สล็อตออนไลน์ และอื่นๆอีกมากมาย

    返信削除
  3. If you are looking for the online assignment help service provider then you should definitely go for AllAssignmentHelp.com. We offer all types of subjects assignment helps at the best price.

    返信削除
  4. . Our specialists are mainstays of our administration. These journalists are completely fruitful in their training that they endeavor hard to give splendid work on task composing.
    science homework help
    PR homework help
    math homework help

    返信削除
  5. A discussion board post is an academic task that is often assigned in high schools, colleges, and universities. The essential purpose of such an assignment is to enable the students to demonstrate their writing, research, and analytical skills. do my coursework for me

    返信削除
  6. Great, I must say and thanks for sharing this informative post. I am really impressed that there is so much information about this subject that have been uncovered and you’ve done your best,MyAssignmenthelp.com is giving help to students who are stressed with their homework help and submit their assignment on time. we are already trusted by thousands of students who struggle to write their academic papers and also by those students who simply want online homework writing service to save their time and make life easy.

    返信削除
  7. An introduction to DWR for graduate students thanks sharing great article.It is one of the best information which you are providing. I love your blog.Get best service Web Design Riyadh you visit here site.

    返信削除
  8. Learn more about DWR this example and intro such amazing.This is a great inspiring article. I am pretty much pleased with your good work.The top company Cleaning Company Dubai provide good service of cleaning.

    返信削除
  9. Thanks for coming up with these kinds of whiter than white write ups. If you guys unable to deal with your assignment and seeking assignment help Sydney service then hire assignment writer.

    返信削除
  10. edes Jahr buchen tausende Reisende unsere bewährten sowie umfangreichen Dienstleistungen und das aus gutem Grund. Mit 30 Jahren Erfahrung in den Bereichen Vollcharterflüge, Privatflugzeug Mieten und Serviceleistungen bewegen wir unsere Kunden zuverlässig von einem Reiseziel zum anderen. Besonders freuen dürfen wir uns von FlightTime über die große Anzahl zufriedener Reisegäste, die sich immer wieder für unser Team bestehend aus professionellen Charterexperten entscheiden. Unsere Kunden wählen für sich ganz bewusst die routinierte und exklusive Abwicklung sämtlicher Transferdetails aus. Wir stehen Ihnen überall in der Welt rund um die Uhr – 24 Stunden am Tag und 7 Tage die Woche – mit unserem ganzheitlichen Charterflug-Service zur Verfügung. Für weitere Informationen rufen Sie uns an unter +49-(0)4292-81119-0.

    返信削除
  11. As this blog is in relation to the graduates. I have something for those who are still in the process of graduating. If you are having trouble with your
    Business Assignment do visit us.



    返信削除
  12. Great Article. Thanks for sharing valuable information. Keep Posting. NLP Tools

    返信削除
  13. Are you feeling stressed while composing your accounting homework? Take Accounting Assignment Help to finish your assigned task before the due dates and without having any stress. Time to raise your numbers via experts’ assistance.
    Accounting Homework Help
    Accounting Assignments
    Accounting Assignment
    Do my Accounting Assignment Help
    Do my Accounting Homework Help
    PEEL Paragraph

    返信削除
  14. Great post! The information you shared is great and very helpful for all. I am Tom Walker professional blogger, academic consultants at assignmenthelp provider website ABCassignmenthelp.com

    返信削除
  15. myassignmenthelp strive to meet the need of customized assignment help of students from Universities across Australia. With our team of enthusiastic and unwavering writing experts, our aim is to help every student improve the academic grades and submit optimum quality assignments that are 100% unique meeting the University guidelines. Connect with us now to understand how it works and start the unique experience of customised writing help.

    返信削除
  16. I am trying to open my software, it is showing me QuickBooks won't open message. That’s why, I am worrying more about it.

    返信削除
  17. Your article is so convincing that I never stop myself from appreciating this. Thank you for posting this. If you want to get experts’ advice for your homework writing, hire an academic writer via Online Assignment Help and share your concerns properly. You will get a wonderful chance to save your time and present effective papers for your project submission.
    Online Assignment Help
    Assignment Help Online
    Assignment Helper

    返信削除
  18. Nepal Tour Packages I have a long desire to travel Nepal once in the whole life. Now, I am planning a short Nepal Tour with my family.
    Nepal Tour Package

    返信削除
  19. Such a valuable post. I like it very much and I like your choice words also. Please post some more topics related to online help because I also work in the online professional support industry which provides support of Outlook. I am waiting for your next valuable post.
    How do I contact outlook by phone?
    How do I contact Outlook support?
    Is there a phone number for Outlook support?
    Outlook Support Phone number
    Microsoft Outlook Support
    Outlook Support
    Outlook Support Number

    返信削除
  20. This write up is really good which gives the clear understanding how to scale up your knowledge stream without any difficulty. If you want to add some valuable impact in your data, you can use the feature of Microsoft office 365 download for getting the best result.Visit here:- https://www.office365download.co/
    office 365 download
    Download Office 365
    Microsoft office 365 download
    download Microsoft office 365

    返信削除
  21. The mentioned points in this guide are very informative and helped me to resolve my issue in a very proficient manner. At the time of solving my query I got stuck at one point, so I called at outlook Support Phone number and the technician who answered my call guided me thoroughly to get rid of my issue. I really appreciate the hardworking of that guy, who helped me during my crisis of time and never leave my side until problem didn’t get resolved. Therefore, I personally suggest everyone to get hold of Microsoft outlook Support if you really want to resolve your query related to your Outlook.
    How do I call Outlook support?
    Is there a phone number for Outlook support?
    Windows support
    Microsoft windows support
    Office 365 Support
    Microsoft Office 365 Support
    Microsoft 365 Support
    Office 365
    Microsoft Office 365
    Microsoft Office Support
    Microsoft Office Support number
    Microsoft Office Support Phone number

    返信削除
  22. When I try to open company files, I am experiencing QuickBooks Error 80029c4a. This is the most common error that is making me more irritating. This error code mainly takes place, when I face difficulties to open the company files.

    返信削除
  23. Take Assignment help in Saudi Arabia if you don’t want to craft your papers. If you want to finish your papers without any stress, take experts’ assistance from professional academic writers and accomplish your work without any stress.
    Assignment help Saudi Arabia
    Assignment help

    返信削除
  24. I was going through your website, and got really great matter here. I must say that your content is authentic and knowledgeable. I’m going to be your regular reader now. By the way, I am a booking associate at Hawaiian airlines booking. If any time, you want to book your hawaiian airlines Reservations, contact us.

    返信削除
  25. Avail of Australian Assignment Help if you want to take the help of Australian writers. It may difficult to cope with the tough study structure of Australian university but you get effective writing skills using online assignment writing services at the best price.
    Assignment Help Online
    Online Assignment Help

    返信削除
  26. Browsed several sites for deals and offers on flight tickets but got none. But here I got what I wanted. I’m fully impressed and satisfied with its services. Well, hello there! I’m working at American Airlines Reservations. We offer affordable deals for flight tickets booking. Contact us now to save money on tickets.

    返信削除
  27. Borrow experts' assistance via online assignment help for completing your homework on time. Students can enhance their marks and understand how to explain given questions in the correct format.
    Online assignment help
    assignment help online
    assignment helper
    online assignment helper

    返信削除
  28. Thanks for sharing this post. Accounting help is here. accounting classes online

    返信削除
  29. Simply drop your files in the dropzone. Make sure to drop all VCF file that you want to combine at once. The online service will automatically merge your VFC files and start a download of the single vcf-merge file that contains all uploaded contacts.

    返信削除
  30. Hello, I would like to thank you for sharing this interesting information. What is more, the material provided here will be useful for a lot of people. I was actually looking for someone who will rewrite my essay. Nevertheless, thanks a lot,

    返信削除

  31. 'I'm highly impressed by the piece of thoughts you have shared on this portal. all the best
    connect us on Assignment Help can shed your burden of assignments with a return of qualitative assignments.
    Law Assignment Help
    Programming Assignment Help
    Management Assignment Help
    My Assignment Help

    返信削除
  32. Quicken is the financial assistance that manages all the accounts and financial related chores. Therefore, stopping such software may result in a big problem, such as you won’t be able to see your quicken files or access them. Sometimes low network, server issue or slow internet connection cause this error. Hence, to solve such a situation you should consult a trained technician, who has skills and years of experience. Quicken techniques are very friendly with the software and also with all the errors, and can immediately solve quicken download error 15500.
    Quicken Online Backup
    Quicken Error CC-587
    quicken download error 324
    quicken download error 106
    Quicken Error Code 105
    generate access code capital one 360

    返信削除
  33. Hi, thank you for writing this excellent piece of content. I am really flabbergasted! Your article helped me clear my head around this topic. Also, anyone who is looking for some additional assignment help online can visit my company’s site. They are providing low cost high quality Assignment Help to students. I know writing assignments can be a real pain. So it is better that you take someone’s help. Why should you not contact them instead of wasting or dreading for failing the grade? Don’t do that. You can get better grades with some academic help.
    Assignment Help Online
    Best Assignment Help
    Assignment Helper
    Assignment Help In USA
    Online Assignment Help
    Assignment Help Company
    Assignment Help Experts
    Assignment Help Service
    Online Assignment Help Services

    返信削除
  34. NOBLEQQ MERUPAKAN Situs Poker Online TERBAIK DENGAN WINRATE TERTINGGI SE ASIA WA :+8551090383

    返信削除