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