JavaScript2017. 12. 20. 13:54

모바일에서는 ajax나 getJSON으로 웹 페이지를 불러올 때 이미지가 있는 경우 뚝뚝 끊길 때가 많다.

네트워크 상태가 안 좋거나 이미지 크기가 크거나 그럴 때.


그래서 이미지가 완전히 로드된 후에 나머지 페이지 처리를 하고 싶었다.

구글링을 하다가 좋은 예제가 있어 담아왔다. 출처는 아래에 ..


먼저 이미지로드를 처리할 확장형 객체를 구현한다.

$.fn.imagesLoaded = function() {
  var $imgs = this.find('img[src!=""]');
  if(!$imgs.length) 
    return $.Deferred().resolve().promise();

  var dfds = [];
  $imgs.each(function() {
    var dfd = $.Deferred();
    dfds.push(dfd);
    var img = new Image();
    img.onload = function() {
      dfd.resolve();
    }
    img.onerror = function() {
      drd.resolve();
    }
    img.src = this.src;
  });
  
  return $.whdn.apply($, dfds);
}

이제 이미지를 로드하는 곳에 붙여넣기만 하면 끝.

$(selector).css('display', 'none');
$(selector).empty();
$.getJSON('/url.do', function(result) {
  ...
  $(selector).append(result.date.imageSrc).imagesLoaded().then(function() {
    $(selector).css('display', 'block');
    // window.android.hideProgressBar();
    // 그 외 나머지 처리할 작업
  });
}

출처 : https://stackoverflow.com/questions/4774746/jquery-ajax-wait-until-all-images-are-loaded


참고로 같은 이름의 플러그인도 있으니 다운받아서 사용하면 더 간단하지 싶다. 

https://github.com/desandro/imagesloaded

Posted by 홍규홍규
Java & Spring2017. 12. 19. 00:06

application-context.xml 에 다음을 밑줄 친 부분을 추가한다.

<beans xmlns="http://www.springframework.org/schema/beans"
    ...
    xmlns:task="http://www.springframework.org/schema/task"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context.xsd
                         ...
                        http://www.springframework.org/schema/task 
                        http://www.springframework.org/schema/task/spring-task-3.0.xsd"> 

그리고 아랫 부분도 추가해준다.

<context:component-scan base-package="패키지명" />
<bean id="scheduleJob" class="패키지명.클래스이름" />
<task:scheduler id="scheduler" pool-size="10" />
<task:annotation-driven scheduler="scheduler" /> 

xml 설정은 이걸로 끝난다. 매우 간단.

다음은 구현부인데 구현부도 간단하다.

@Component
public class Shceduler {
  
  @Scheduled(cron="0 0 1 * * *")
  public void sheduler() {
    Calendar calendar = Calendar.getInstance();
    Date date = calendar.getTime();
    String today = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    System.out.println("스케쥴러 작동 시간 : " + today);
  }

}

클래스 위에 @Component 어노테이션을 붙여주고, 구현 메서드에도 @Scheduled 어노테이션을 붙여주면 된다.

cron 표현식은 스케쥴러가 작동하는 주기.

아래 블로그에 cron 표현식에 대해 자세하게 정리되어 있다. 


http://blog.naver.com/PostView.nhn?blogId=lovemema&logNo=140200056062


내 경우 하루에 한 번씩 주기적으로 쿼리문을 돌려야 해서 위 스케쥴러를 구현하게 되었다.


- 2019-02-19 추가

위 처럼 하면 스케쥴러가 중복실행되는 이슈가 발생한다. 

이것은 바보처럼 Scheduler 클래스에 @Component 어노테이션을 붙였기 때문인데 

xml에서 스케쥴러 세팅을 했는데도 클래스를 빈으로 등록하는 어노테이션을 선언했기 때문이다.


따라서 @Component 어노테이션은 삭제해주자...^^;;

Posted by 홍규홍규
MySQL2017. 12. 13. 17:50

테이블에 특정 값들을 Insert하고 성공 시 Insert된 데이터의 Primary Key가 필요한 경우가 있다.

이때는 <selectKey>를 사용하면 된다.

<insert id="insert" parameterType="Member">
    INSERT INTO MEMBER(name, age) VALUES(#{name}, #{age})
    <selectKey resultType="int" keyProperty="no" order="AFTER">
        SELECT LAST_INSERT_ID()
    </selectKey>
</insert>

keyProperty는 VO에 정의한 테이블에 들어가는 컬럼 ID를 넣으면 된다.

따라서 Primary Key 뿐만 아니라 다른 데이터도 가져올 수 있다는 뜻.


order는 순서다. 

특정 값을 이용해 INSERT하려면 당연하게도 INSERT 앞에 위치시키고 BEFORE 명령어를 쓰면 된다.


그러면 insert가 성공하면 MEMBER 객체의 no 변수에 Auto Increment된 값이 저장된다.


member.getNo()로 가져오면 끝.



'MySQL' 카테고리의 다른 글

동적 쿼리문 작성해보기  (0) 2018.04.24
Posted by 홍규홍규