2013년 10월 30일 수요일

테이블 열 추가할때

Table add row


$('#myTable > tbody:first').append('<tr>...</tr><tr>...</tr>');

$('#myTable > tbody:last').append('<tr>...</tr><tr>...</tr>');

<table id=myTable>
  <tbody id=first>
    <tr><td>
      <table id=myNestedTable>
        <tbody id=last>
        </tbody>
      </table>
    </td></tr>
  </tbody>
</table>

2013년 10월 29일 화요일

프로퍼티 파일 로드방법



JAVA에서 Properties 파일 읽을 경우
@Service("applicationConfig")
public class ApplicationConfig
{
//util:properties 변수 읽을때
@Value("#{commonapp['app.CLASS_DEBUG']}")
String classDeBug;

//properties-placeholder 변수 읽을때
@Value("${config.application.title}")
String configApplicationTitle;

//환경변수 읽을때
@Value("#{systemProperties['os.name']}")
String osName;
}



Properties 파일 UTF-8 형식으로 읽기(한글 사용시)
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations" value="classpath:config.properties"/>
    <property name="fileEncoding" value="UTF-8"/>
</bean>

2013년 10월 3일 목요일

[Spring Framework] Interceptor를 이용하여 세션 체크하기

[spring-config.xml]
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<mvc:interceptors> <!-- 인터셉터로 사용할 빈을 정의한다. -->
<bean class="com.prompt.cam.management.service.CheckLocaleInterceptor"/>
<mvc:interceptor>
<mvc:mapping path="/*"/> <!-- 인터셉터가 반응할 매핑 경로를 정의한다. -->
<bean class="com.prompt.cam.management.service.CheckLocaleInterceptor"/> <!-- 해당 클래스를 통해 인터셉터가 동작한다. -->
</mvc:interceptor>
</mvc:interceptors>


[CheckLocaleInterceptor.java]
package com.test.service;

import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

@Service
public class CheckLocaleInterceptor extends HandlerInterceptorAdapter {  // HandlerInterceptorAdapter를 상속받은 클래스를 생성

 @Override
 public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler){ // 컨트롤러 접근전에 가로채기 위해 preHandle 사용
        HttpSession session = request.getSession();
        Locale locale = (Locale) session.getAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME);
     
        if (locale == null) {
        // set default language 'ko'
        session.setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, Locale.KOREAN);
            return true;
        } else {
        // Not null skip
            return true;
        }
    }
}

2013년 10월 1일 화요일

[Spring Framework] spring messagesource locale(다국어 지원)

Spring Message 처리 방법(다국어)

불러올 언어별 프로퍼티파일 생성
message.properties(아래 두 파일이 없을 경우 불러온다)
message_ko.properties
message_en.properties

파일안에 형식은 아래와 같다
불러올때사용할ID=텍스트
ex)
  test.input.name=이름입력

------------------------------------------------------------------------------
아래처럼 리스트에 ko파일과 en파일을 넣으면 우선적으로
접속한 사용자의 로컬언어를 먼저 찾고 그다음 없을 경우
기본 파일의 내용을 가져오게 된다.

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="defaultEncoding" value="UTF-8"/> //<-- 이렇게 해놓고 properties파일을 UTF-8형식으로 사용하면 한글이 유니코드로 변환되지 않고 정상적으로 로드됨
   <property name="basenames">
       <list>
        <value>classpath:message</value> // <-- message.properties 파일이 있는 경로를 넣어준다.(locale에 따라 자동으로 _ko, _en 파일을 로드한
       </list>
   </property>
   <property name="fallbackToSystemLocale" value="false"/>   //<-- 'fallbackToSystemLocale' property가 true인 경우, locale에 해당하는 file이 없을 경우 system locale을 사용
   <property name="cacheSeconds" value="5"/> //<-- 5초마다 업데이트 된 properties 파일을 새로 로드함
</bean>

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
   <property name="defaultLocale" value="ko" />  //<-- 세션에 locale 정보가 없을 경우 기본 언어
</bean>

<bean id="/setChangeLocale.do" class="com.prompt.cam.management.controller.SessionLocaleController" />  // <-- 세션에 locale 정보를 업데이트 하는 세션 컨트롤러
------------------------------------------------------------------------------
package com.test.controller;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

@Controller
public class SessionLocaleController {
@RequestMapping(value = "/setChangeLocale.do")
public String changeLocale(@RequestParam(required = false) String locale,
  ModelMap map, HttpServletRequest request, HttpServletResponse response) {
HttpSession session = request.getSession();
  Locale locales = null;
// 넘어온 파라미터에 ko가 있으면 Locale의 언어를 한국어로 바꿔준다.
// 그렇지 않을 경우 영어로 설정한다.
if (locale.matches("ko")) {
locales = Locale.KOREAN;
} else {
locales = Locale.ENGLISH;
}

// 세션에 존재하는 Locale을 새로운 언어로 변경해준다.
session.setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, locales);
// 해당 컨트롤러에게 요청을 보낸 주소로 돌아간다.
String redirectURL = "redirect:" + request.getHeader("referer");
return redirectURL;
}
}
------------------------------------------------------------------------------
실제 소스에 적용시 아래와 같이 넣어준다.
태그 라이브러리 호출
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>

자바스크립트에 넣을 경우
<spring:message code="test.input.name" javaScriptEscape="true"/>

HTML TAG안에 TEXT로 넣을 경우
<spring:message code="test.input.name"/>
------------------------------------------------------------------------------
적용 후 jsp에서 간단하게 아래와 같이 테스트 해볼 수 있다.
<a href="/setChangeLocale.do?locale=ko">한국어</a>
<a href="/setChangeLocale.do?locale=en">ENGLISH</a>