2014年11月7日金曜日

jQueryでブラウザで表示中のビューポートの情報を得るには?

getViewport = function() {
    var $w = $(window);
    return {
        l: $w.scrollLeft(),
        t: $w.scrollTop(),
        w: $w.width(),
        h: $w.height()
    }
}

参考リンク
http://stackoverflow.com/questions/10324753/jquery-function-to-get-the-curren-viewport

d3でcssのクラスを複数追加するには

d3.selectAll(".someDiv").attr("test1 test2");

参考リンク
http://stackoverflow.com/questions/13188125/d3-add-multiple-classes-with-function

MavenでWeb Applicationプロジェクトの作成方法

1.mvnコマンドで以下をEclipseのworkspaceで実行

$ mvn archetype:generate -DgroupId=com.test
-DartifactId=TestApp
-DarchetypeArtifactId=maven-archetype-webapp
-DinteractiveMode=false

2.WTPプロジェクトに変換

$ mvn eclipse:eclipse -Dwtpversion=2.0

3.pom.xml、web.xmlを自分用に編集していく

参考リンク
http://www.mkyong.com/maven/how-to-create-a-web-application-project-with-maven/


Spring MVCでreCaptchaを使うには?

1.GoogleでreCaptchを使用するのに必要なプライベートキー、パブリックキーを取得。

http://www.google.com/recaptcha


2.pom.xmlに追加。

<dependency>
 <groupId>net.tanesha.recaptcha4j<groupId>
     <artifactId>recaptcha4j<artifactId>
   <version>0.0.7<version>
<dependency>

3.JSPでタグを追加。

<tags:captcha privateKey='XXXX' publicKey='YYYY'><tags:captcha>

4.コントローラー(サーブレット)でのパラメータと認証。

@RequestMapping(value='', method=RequestMethod.POST)
 public String submitForm(@ModelAttribute('userInfo') UserInfo userInfo, @RequestParam('recaptcha_challenge_field') String challangeField, @RequestParam('recaptcha_response_field') String responseField, ServletRequest servletRequest) {
 String remoteAddress = servletRequest.getRemoteAddr();

 ReCaptchaResponse reCaptchaResponse = this.reCaptcha.checkAnswer(remoteAddress, challangeField, responseField);

 if(reCaptchaResponse.isValid()) {
  return 'success';
 } else {
  return 'fail'; 
 }
}

参考リンク
http://www.javacodegeeks.com/2012/11/use-recaptcha-in-a-spring-mvc-web-application.html
https://code.google.com/p/recaptcha/wiki/HowToSetUpRecaptcha#Java/JSP