2012년 8월 11일 토요일

SpringMVC Tip

서블릿 매칭을 제외한 스프링프레임웍으로부터 현재 매핑된 주소 동적으로 가져오기


Case1. 단순매칭시

@RequestMapping(value=[이부분], method={RequestMethod.GET,RequestMethod.POST})
public ModelAndView index(HttpServletRequest request, HttpServletResponse reponse){
    String mappingValue = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    System.out.println("Mapping Value = "  + mappingValue);
    return new ModelAndView("index");
}


Case2. url 변수 사용시

@RequestMapping(value=index. + "{val}", method={RequestMethod.GET,RequestMethod.POST})
public ModelAndView index(HttpServletRequest request, HttpServletResponse reponse,
    @PathVariable("val") final String val){
    String mappingValue = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
    Map<?, ?> mapping = (Map<?, ?>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
    Iterator<?> iter = mapping.keySet().iterator();
    while(iter.hasNext()){
        String key = (String) iter.next();
        Object value = mapping.get(key);
        if(mappingValue.contains("{"+key+"}")){
            mappingValue = mappingValue.replaceAll("\\{"+key+"\\}", value == null ? "" : value.toString());
        }
    }
    System.out.println("Mapping Value = "  + mappingValue);
    return new ModelAndView("index");
}


참고

2012년 7월 9일 월요일

[JAVA] My .properties file loader




import java.io.IOException;
import java.net.URL;
import java.util.Calendar;
import java.util.Properties;
import java.util.TimeZone;
/**
 *
 * @author pig
 *
 */
public class Loader {

public static String getPropertieValue(String key) {
String value=Loader.getInstance().getPropertie(key);
if(value == null){
System.out.println("not find ["+key+"] from init.properties file");
System.out.println("set "+key+" : null");
}else{
System.out.println("set "+key+" : " + value);
}
return value;
}
public static TimeZone getDefaultTimeZone() {
String timeZoneStr = Loader.getInstance().getPropertie("default.time.zone");
TimeZone timezone;
if(timeZoneStr == null){
timezone = Calendar.getInstance().getTimeZone();
System.out.println("not find [default.time.zone] from init.properties file");
System.out.println("set default timezone(take value from system) : " + timezone.getDisplayName());
}else{
timezone = TimeZone.getTimeZone(timeZoneStr);
System.out.println("set default timezone : " + timezone.getDisplayName());
}
return timezone;
}
private static final Loader loader = new Loader();

public static Loader getInstance() {
return loader;
}

private Properties properties = new Properties();
private Loader() {

try {
//System.out.println(this.getClass().getResource("/"));
URL url = new URL("file:" + this.getClass().getResource("").getFile().split("classes")[0] + "classes/init.properties");
System.out.println("--Load : init.properties-------------------------------------------------------------------");
System.out.println("--necessery properties file : init.properties");
System.out.println("--properties file location : " + url.getPath());
System.out.println("--develop by oneofworld.com");
//System.out.println("--v0.1");
properties.load(url.openStream());
System.out.println("--Load properties SUCCESS");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("--Load properties FAIL : " + e.getMessage());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("--Load properties FAIL : " + e.getMessage());
}
System.out.println("-------------------------------------------------------------------------------------------");
}
public Properties getProperties() {
return properties;
}
public String getPropertie(String key) {
return properties.getProperty(key);
}
}


2012년 6월 6일 수요일

[JSP] 브라우저 파일 다운로드창에서 한글깨짐

파일명을 UTF-8로 인코딩 해준다.

response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "utf-8") + ";");