天天看点

二、Struts 2的类型转换---1.Struts 2中实现自定义的类型转换

Struts 2 鐨勭被鍨嬭浆鎹?

1锛幝?浼犵粺鐨勭被鍨嬭浆鎹㈣繃绋嬨€?

浼犵粺鐨勭被鍨嬭浆鎹㈣繃绋嬬洿鎺ラ€氳繃servlet鍙栧緱鐢ㄦ埛鐨勮緭鍏ュ弬鏁帮紝鐒跺悗缈诲埌Bean涓紝鐒跺悗鍙互鐩存帴閫氳繃servlet鐢熸垚杈撳嚭銆傚ぇ鑷村疄鐜拌繃绋嬪涓嬨€?

public class Regist extends HttpServlet

{

聽聽聽 public void service(HttpServletRequest request,HttpServletResponse response)throws IOException

{

聽聽聽聽聽聽聽 request.setCharacterEncoding("GBK");

聽聽聽聽聽聽聽 String name = request.getParameter("username");

聽聽聽聽聽聽聽 String pass = request.getParameter("pass");

聽聽聽聽聽聽聽 String strAge = request.getParameter("age");

聽聽聽聽聽聽聽 String strBirth = request.getParameter("birth");

聽聽聽聽聽聽聽 int age = Integer.parseInt(strAge);

聽聽聽聽聽聽聽 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD");

聽聽聽聽聽聽聽 Date birth = null;

聽聽聽聽聽聽聽 try

聽聽聽聽聽聽聽 {

聽聽聽聽聽聽聽聽聽聽聽 birth = sdf.parse(strBirth);

聽聽聽聽聽聽聽 }

聽聽聽聽聽聽聽 catch (Exception e)

聽聽聽聽聽聽聽 {

聽聽聽聽聽聽聽聽聽聽聽聽聽聽 e.printStackTrace();

聽聽聽聽聽聽聽 }

聽聽聽聽聽聽聽 UserBean user = new UserBean(name , pass , age , birth);

聽聽聽聽聽聽聽 response.setContentType("text/html;charset=GBK");

聽聽聽聽聽聽聽 PrintWriter out = response.getWriter();

聽聽聽聽聽聽聽 out.println("<html><head><title>");

聽聽聽聽聽聽聽 out.println("绫诲瀷杞崲椤甸潰");

聽聽聽聽聽聽聽 out.println("</title></head><body>");

聽聽聽聽聽聽聽 out.println("<h1>绫诲瀷杞崲椤甸潰</h1>");

聽聽聽聽聽聽聽 out.println(user.getName() + "<br>");

聽聽聽聽聽聽聽 out.println(user.getPass() + "<br>");

聽聽聽聽聽聽聽 out.println(user.getAge() + "<br>");

聽聽聽聽聽聽聽 out.println(user.getBirth() + "<br>");

聽聽聽聽聽聽聽 out.println("</body></html>");

}

聽聽聽 public void destroy()

聽聽聽 {

聽聽聽聽聽聽聽 super.destroy();

聽聽聽 }

}

聽聽聽聽聽聽 姝e涓婇潰浠g爜锛屾垜浠厛浠嶩ttpServletRequest涓彇鍑虹敤鎴疯緭鍏ョ殑鍙傛暟锛岀劧鍚庡仛涓€浜涚畝鍗曠殑绫诲瀷杞崲鍚庣敤杩欎簺鍙傛暟鍘诲垵濮嬪寲UserBean锛孶serBean鏄竴涓甫榛樿鏋勯€犲嚱鏁扮殑POJO銆傜劧鍚庢槸鐢熸垚杈撳嚭銆傛樉鐒惰繖鏍风殑杩囩▼姣旇緝绻佺悙銆傝€屼笖杩欎簺姝ラ閮芥槸寰堢▼寮忓寲鐨勩€傛帴涓嬫潵锛屾垜浠湅浠ヤ笅Struts 2鐨勭被鍨嬭浆鎹㈠櫒銆?

2锛幝?Struts 2涓熀浜嶰GNL鐨勭被鍨嬭浆鎹㈠櫒

Struts 2鐨勭被鍨嬭浆鎹㈠櫒瀹為檯涓婃槸鍩轰簬OGNL瀹炵幇鐨勩€傛垜浠厛鐪嬩竴涓婳GNL涓彁渚涚殑TypeConverter鎺ュ彛銆?

璇ユ帴鍙e畾涔夊涓嬶細

public interface TypeConverter {

聽聽聽聽聽聽 public Object convertValue(Map context, Object target, Member member, String propertyName, Object toType);

}

璇ユ帴鍙h繕鏈変竴涓疄鐜扮被锛欴efaultTypeConverter,鎴戜滑鍙互閫氳繃缁ф壙璇ョ被鏉ュ疄鐜拌嚜宸辩殑绫诲瀷杞崲鍣ㄣ€?

瑕佺户鎵緿efaultTypeConverter绫婚渶瑕侀噸鍐檆onvertValue鏂规硶銆?

浠ヤ笅鏄竴涓ず渚嬶細

public class UserConverter extends DefaultTypeConverter

{

聽聽聽 public Object convertValue(Map context, Object value, Class toType)

聽聽聽聽聽聽 {

聽聽聽聽聽聽聽 if (toType == User.class )

聽聽聽聽聽聽聽聽聽聽聽聽聽 {

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 String[] params = (String[])value;

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 User user = new User();

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 String[] userValues = params[0].split(",");

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 user.setName(userValues[0]);

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 user.setPass(userValues[1]);

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 return user;

聽聽聽聽聽聽聽 }

聽聽聽聽聽聽聽聽聽聽聽聽聽 else if (toType == String.class )

聽聽聽聽聽聽聽聽聽聽聽聽聽 {

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 User user = (User) value;

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 return "<" + user.getName() + "," + user.getPass() + ">";

聽聽聽聽聽聽聽 }

聽聽聽聽聽聽聽 return null ;

聽聽聽 }

}

鎴戜滑鐪嬪埌涓婇潰鐨刄serConverter缁ф壙鑷?DefaultTypeConverter骞堕噸鍐欎簡 convertValue鏂规硶銆傚湪convertValue鏂规硶涓厛鍒ゆ柇toType鏄竴涓猆ser绫昏繕鏄竴涓猄tring銆傚鏋滄槸User锛屾剰鎬濆氨鏄瑕佽浆鎹㈠緱鐩爣绫诲瀷鏄疷ser锛屽弽涔嬪垯杞崲鎴怱tring銆傚姝わ紝瀹炵幇User鍜孲tring涔嬮棿鐨勭浉浜掕浆鎹€?

涓婇潰鐨勫疄鐜伴€昏緫骞朵笉澶嶆潅銆傞偅涔堝浣曡绯荤粺鑷姩鍘诲畬鎴愯繖涓浆鎹㈤€昏緫鍛€傜瓟妗堟椂閫氳繃绾﹀畾鐨勯厤缃枃浠讹紝鎶婅繖涓浆鎹㈢被鍜孶ser绫昏仈绯昏捣鏉ャ€?

鏍规嵁浣滅敤鑼冨洿鐨勪笉鍚屽垎涓哄眬閮ㄧ被鍨嬭浆鎹㈠櫒鍜屽叏灞€绫诲瀷杞崲鍣ㄤ袱绉嶃€?

鈶村眬閮ㄧ被鍨嬭浆鎹㈠櫒

灞€閮ㄧ被鍨嬭浆鎹㈠櫒鐨勮浆鎹㈤€昏緫鍙綔鐢ㄤ簬鍗曚釜Action鐨勬煇浜涘睘鎬х被銆?

杩欎釜鏂囦欢鐨勫懡鍚嶈鍒欏涓嬶細

ActionName-conversion.properties锛欰ctionName鏄渶瑕佽浆鎹㈠櫒鐢熸垚鐨凙ction鐨勭被鍚嶏紝鍚庨潰鐨?conversion.properties涓哄浐瀹氶儴鍒嗐€?

杩欎釜鏂囦欢鐨勯厤缃柟寮忎篃鏄痥ey-value瀵广€傚舰寮忓涓嬶細

propertyName=绫诲瀷杞崲鍣ㄧ被

姣斿璇存垜浠湁涓狶oginAction鐨凙ction绫伙紝姝ょ被涓湁涓€涓猆ser绫荤殑瀵硅薄user銆傛垜浠殑杈撳叆鍙傛暟鏄竴涓互鈥滐紝鈥濆垎鍓茬殑鍖呭惈User绫诲悇灞炴€х殑String锛岄渶瑕侀€氳繃涓婇潰鐨刄serConverter鎶婅緭鍏ュ弬鏁拌浆鎹㈡垚鎴戜滑闇€瑕佺殑User绫诲璞°€?

閭d箞鎴戜滑寤虹珛涓€涓悕涓篖oginAction-conversion.properties鐨勬枃浠讹紝姝ゆ枃浠跺唴瀹瑰涓嬨€?

user=abc.UserConverter

姝ゆ枃浠惰鍜孡oginAction.class鏀惧湪鍚屼竴鐩綍涓嬶紝鏃EB-INF/classes/abc璺緞涓嬨€?

濡傛灉LoginAction閲岃繕鏈変竴涓猆ser绫荤殑瀵硅薄customer锛岄偅涔堟垜浠啀鍔犱竴琛岄厤缃鍙ワ細

customer=abc.UserConverter

鍙灞€閮ㄧ被鍨嬭浆鎹㈠櫒璁剧疆鐨勪竴涓狝ction涓瘡涓€涓璞″搴旂殑杞崲鍣紝濡傛灉涓€涓被鏈夊瀵硅薄鍒欒娌′釜瀵硅薄閮借缃竴娆°€傛樉鐒跺緢楹荤儲銆傚鏋滄垜浠兂璁剧疆User绫诲拰UserConverter鐩存帴瀵瑰簲璧锋潵閭d箞灏卞彲浠ヤ娇鐢ㄥ叏灞€绫诲瀷杞崲鍣?

鈶靛叏灞€绫诲瀷杞崲鍣?

娉ㄥ唽涓€涓叏灞€绫诲瀷杞崲鍣ㄨ鎻愪緵涓€涓悕涓簒work-conversion.properties鏂囦欢锛岃鏂囦欢鐨勫唴瀹逛负鈥濆鍚堢被鍨?瀵瑰簲绫诲瀷杞崲鍣ㄢ€濄€備粛鐒舵槸涓婇潰鐨勪緥瀛愩€?

寤虹珛涓€涓獂work-conversion.properties鏂囦欢锛屾斁鍦╓EB-INF/classes鐩綍涓嬨€傞厤缃唴瀹瑰涓嬨€?

lee.User=lee.UserConverter

璇ュ叏灞€绫诲瀷杞崲鍣ㄥ皢浼氬鎵€鏈塙ser绫诲瀷灞炴€ц捣浣滅敤銆?

3锛幝?閫氳繃StrutsTypeConverter鎶借薄绫诲疄鐜扮被鍨嬭浆鎹㈠櫒

StrutsTypeConverter鏄疍efaultTypeConverter鐨勫瓙绫汇€傝绫讳腑宸茬粡閲嶅啓浜哻onvertValue()鏂规硶銆?

public abstract class StrutsTypeConverter extends DefaultTypeConverter {聽聽聽 public Object convertValue(Map context, Object o, Class toClass) {聽聽聽聽聽聽聽 if (toClass.equals(String.class)) {聽聽聽聽聽聽聽聽聽聽聽 return convertToString(context, o);聽聽聽聽聽聽聽 } else if (o instanceof String[]) {聽聽聽聽聽聽聽聽聽聽聽 return convertFromString(context, (String[]) o, toClass);聽聽聽聽聽聽聽 } else if (o instanceof String) {聽聽聽聽聽聽聽聽聽聽聽 return convertFromString(context, new String[]{(String) o}, toClass);聽聽聽聽聽聽聽 } else {聽聽聽聽聽聽聽聽聽聽聽 return performFallbackConversion(context, o, toClass);聽聽聽聽聽聽聽 }聽聽聽 }聽聽聽聽 protected Object performFallbackConversion(Map context, Object o, Class toClass) {聽聽聽聽聽聽聽 return super.convertValue(context, o, toClass);聽聽聽 }聽聽聽聽 public abstract Object convertFromString(Map context, String[] values, Class toClass);聽聽聽聽 public abstract String convertToString(Map context, Object o);}

杩樻槸閭e墠闈㈢殑渚嬪瓙鏉ヨ锛屾垜浠瀹炵幇UserConverter鍙渶瑕侀噸鍐檆onvertFromString()鍜宑onvertToString()涓や釜鏂规硶灏卞彲浠ヤ簡銆傚涓嬶細

public class UserConverter extends StrutsTypeConverter {

聽聽聽 public Object convertFromString(Map context, String[] values, Class toClass){

聽聽聽聽聽聽聽聽聽聽聽聽聽 User user = new User();

聽聽聽聽聽聽聽聽聽聽聽聽聽 String[] userValues = values[0].split(",");

聽聽聽聽聽聽聽聽聽聽聽聽聽 user.setName(userValues[0]);

聽聽聽聽聽聽聽聽聽聽聽聽聽 user.setPass(userValues[1]);

聽聽聽聽聽聽聽聽聽聽聽聽聽 return user;

聽聽聽 }

聽聽聽 @Override

聽聽聽 public String convertToString(Map context, Object o){

聽聽聽聽聽聽聽聽聽聽聽聽聽 User user = (User)o;

聽聽聽聽聽聽聽聽聽聽聽聽聽 return "<" + user.getName() + "," + user.getPass() + ">";

聽聽聽 }

}

涓婇潰鐨勪唬鐮佸簲璇ュ緢瀹规槗鐞嗚В锛屾墍浠ヤ笉澶氳В閲婁簡銆?

鏈枃鏉ヨ嚜CSDN鍗氬锛岃浆杞借鏍囨槑鍑哄锛歨ttp://blog.csdn.net/jackycc/archive/2009/01/11/3753928.aspx