Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Tuesday, January 21, 2014

[JAVA] Automatic Mapping ValueObject(VO) Example

It was very useful.
On spring framework.



source code:
package com.kaudo.common.util;

import org.apache.commons.collections.map.ListOrderedMap;

public class DataMap extends ListOrderedMap {

    private static final long serialVersionUID=6723434363565852261L;

    @Override
    public Object put(Object key, Object value){
        return super.put(convert2CamelCase((String)key),value);
    }

    public static String convert2CamelCase(String underScore){
        if (underScore.indexOf('_') < 0
            && Character.isLowerCase(underScore.charAt(0))){
            return underScore;
        }
        StringBuilder result = new StringBuilder();
        boolean nextUpper = false;
        int len = underScore.length();
        for (int i = 0; i < len; i++){
            char currentChar = underScore.charAt(i);
            if (currentChar == '_'){
                nextUpper = true;
            } else {
                if (nextUpper){
                    result.append(Character.toUpperCase(currentChar));
                    nextUpper = false;
                } else {
                    result.append(Character.toLowerCase(currentChar));
                }
            }
        }
        return result.toString();
    }
}
when used in ibatis xml
<sqlMap namespace="LolDao">

 <typeAlias  alias="lolVo" type="com.kaudo.www.lol.vo.LolVo"/>
 <typeAlias  alias="dataMap" type="com.kaudo.common.util.DataMap"/>
...
...
...
 <select id="selChampionList" resultClass="dataMap"  parameterClass="lolVo">
  <![CDATA[
  SELECT /*+ LolDao.selChampionList 목록 조회 */
   id,
   name,
   displayName,
   title,
...
...
when used in dao
...
...
 @SuppressWarnings("unchecked")
 public List selChampionList(LolVo lolVo) throws SQLException {
  return (List)this.getSqlMapClient().queryForList("LolDao.selChampionList",lolVo);
 }
...
...
...

[JAVA] Login Interceptor Example

Login Interceptor Example
On Spring Framework

package com.kaudo.common.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

@Service
public class LoginCheckInterceptor extends HandlerInterceptorAdapter {

 private static final Logger logger = Logger.getLogger(SecurityInterceptor.class);

  @Override
 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  HttpSession session=request.getSession(false);
  @SuppressWarnings("unused")
  String referer=request.getHeader("referer");
  //logger.info(referer);
  if(session==null){
   logger.info("Intercepted by session: "+session);
   response.sendRedirect("/interceptor?url=/");
   return false;
  }
  String userid=(String)session.getAttribute("userid");
  String usertype=(String)session.getAttribute("usertype");
  if(userid==null || usertype==null || ("").equals(userid) || ("").equals(usertype)){
   logger.info("Intercepted by userid: "+userid+", usertype: "+usertype);
   response.sendRedirect("/interceptor?url=/");
   return false;
  }
  return true;
 }
}

Friday, October 5, 2012

[JAVA] Java Split Function, Examples









JAVA FUNCTION - SPLIT


Is a function that returns an array of strings by regular expressions.
Second parameter limit the value to the number of options you can control.

public String [] split(String  regex, int limit)


Special character
String str="1+2+10+15";
String splitted[]=str.split("[+]");
for (String value : splitted)
 System.out.println(value);

// RESULT
// 1
// 2
// 10
// 15


Blank value
String str="1,,3,";

String splitted[]=str.split(",");
for (String value : splitted)
 System.out.println(value);
// RESULT
// 1
// 3

String splitted[]=str.split(",",-1);
for (String value : splitted)
 System.out.println(value);

// RESULT
// 1
// 
// 3
//

Monday, September 17, 2012

[JAVA/JSP] How to get the variables as an Array, request.getParameter, request.getParameterValues












1. For array - request.getParameterValues([VALUENAMEASARRAY]);
You can get variables as an Array by follow codes
String[] arrayParam1=request.getParameterValues("param1");
for(int i=0;i<arrayParam1.length;i++){
 System.out.println("arrayParam1["+i+"] : "+arrayParam1[i]);
}


2. For single - request.getParameter ([VALUENAME]);
Normally of how to get a variables
<%
String yourparams=request.getParameter("yourparams");
System.out.println("value : "+yourparams);
%>

Wednesday, September 12, 2012

[FRAMEWORK] Devon LMail Class Java source examples

Devon framework lmail class usage
Easy better than JAVA Mail API






Implements
String MAIL_HOST="localhost";
String FROM_ADDRESS="<a href="mailto:sender@lgcns.com">sender@lgcns.com</a>";
String FROM_NAME="sender";
String TO_ADDRESS="<a href="mailto:receiver@lgcns.com">receiver@lgcns.com</a>";
String TO_NAME="receiver";

LMail mail = new LMail();  
mail.setFromMailAddress(FROM_ADDRESS, FROM_NAME);  
mail.setToMailAddress(TO_ADDRESS, TO_NAME);  
mail.setSubject("Mail Test");  
String htmlMessage = "<html><span style="color: red;">Html message Test</span></html>";  
mail.setHtmlAndFile(htmlMessage, new String[]{"C:/test.txt", "C:/attach.txt"});  
mail.send();

Controller
// default spec에 example body-template을 사용 할 경우.  
LMail mail = new LMail("default", "example");  

// 현재 설정을 보여준다.  
mail.printCurrentConfigInfo();  

// mail.loadHtml 을 htmlExample로 정의된 템플릿에 ld 에 저장된 data를 삽입하는것이고.  
// 이 때 String이 리턴되므로 mail.setHtml 에 바로 넘긴다.  
mail.setHtml(mail.loadHtml("htmlExample", ld));  

// 메일을 발송한다.  
mail.send();  

Java 코드를 입력하십시요.


Setting
  
  
true  
tester@lgcns.com  
  
  
test  
test  
  
localhost  
25  
true  
admin@aaa.co.kr  
default sender  
7bit  
text/plain  
text/html  
euc-kr  
Q  
  
  
example@aaa.co.kr  
example sender  
  

  
  
#home/mail  
default.html  
127.0.0.1  
9568  
true  
#home/mail/save  
  

  
  
#home/mail/example  
example.html