隨筆 - 23  文章 - 11  trackbacks - 0
          <2025年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          http://www.jscud.com/srun/news/viewhtml/4_2005_1/26.htm

          posted @ 2007-03-28 15:36 小小~咖啡豆 閱讀(1465) | 評論 (0)編輯 收藏

          用StrutsTestCase能很好的對Struts來進行測試,可是如果時用Spring來管理Struts的action的時候,用StrutsTestCase的常規方式是不能進行測試的,以下的一個文章對此有很好的說明:

          http://www.jetmaven.net/contents/documents/p_spring_junit_combination.php

          posted @ 2007-03-25 23:07 小小~咖啡豆 閱讀(444) | 評論 (0)編輯 收藏

          webwork的IOC(基于2.1.X版本)
          webwork的ioc其實在webwork中使用起來挺方便的,雖然其功能不算強大,但是已經能很好的滿足我們一般的需要了,就算我們使用spring 的ioc,如果不使用特別的功能,其一般我們也是基于接口,然后有個set方法,通過set來注入,沒有太多的區別,不同的是webwork的ioc需要依賴xwork,而spring卻是依賴spring這個容器。
          webwork的ioc是怎么進行注入的了,我們從代碼中進行分析:
          首先看看攔截器的代碼:

          public class ComponentInterceptor extends AroundInterceptor {
          //~ Static fields/initializers /////////////////////////////////////////////

          public static final String COMPONENT_MANAGER = "com.opensymphony.xwork.interceptor.component.ComponentManager";

          //~ Methods ////////////////////////////////////////////////////////////////

          protected void after(ActionInvocation dispatcher, String result) throws Exception {
          }

          protected void before(ActionInvocation dispatcher) throws Exception {
          ComponentManager container = (ComponentManager) ActionContext.getContext().get(COMPONENT_MANAGER);

          if (container != null) {
          container.initializeObject(dispatcher.getAction());
          }
          }
          }

          主要的代碼用黑體標注出來了,container實際就是組件管理器,這里是一個ComponentManager接口的實現 DefaultComponentManager,然后調用了該類的方法initializeObject(dispatcher.getAction ());而dispatcher.getAction()實際就是所調用的action對象,我們再來看看 DefaultComponentManager做了什么。

          public void initializeObject(Object obj) {
          loadResource(obj, obj.getClass(), this);
          }


          private Class loadResource(Object resource, Class clazz, DefaultComponentManager dcm) {
          // ~由此來判斷是否要進行依賴注入
          boolean resourceNotLoaded = !dcm.loadOrder.contains(resource);

          if (resourceNotLoaded) {
          Map resources = getResourceDependencies(clazz);

          for (Iterator iterator = resources.entrySet().iterator();
          iterator.hasNext();) {
          Map.Entry mapEntry = (Map.Entry) iterator.next();
          Class depResource = (Class) mapEntry.getKey();
          DefaultComponentManager newDcm = (DefaultComponentManager) mapEntry.getValue();

          try {
          ResourceEnablerPair pair = setupAndOptionallyCreateResource(newDcm, depResource);
          setupResource(resource, pair.enabler, pair.resource);
          } catch (Exception e) {
          e.printStackTrace();

          if (log.isDebugEnabled()) {
          log.debug("Error loading or setting up resource: " + resources.getClass().getName(), e);
          }
          }
          }

          dcm.alreadyLoaded.add(clazz);

          if (resource instanceof Initializable) {
          Initializable initializable = (Initializable) resource;
          initializable.init();
          }

          dcm.resourceInstances.put(clazz, resource);
          dcm.loadOrder.add(resource);
          }

          // now return this class's enabler
          Class enabler = (Class) dcm.enablers2.get(clazz);

          return enabler;
          }

          private Map getResourceDependencies(Class resourceClass) {
          List interfaces = new ArrayList();
          //~ 將所有的interface放入interfaces鏈表中
          addAllInterfaces(resourceClass, interfaces);

          Map dependencies = new HashMap();

          for (Iterator iterator = interfaces.iterator(); iterator.hasNext();) {
          Class anInterface = (Class) iterator.next();

          DefaultComponentManager dcm = this;

          while (dcm != null) {
          Class possibleResource = (Class) dcm.enablers.get(anInterface);

          if (possibleResource != null) {
          dependencies.put(possibleResource, dcm);

          break;
          }
          dcm = dcm.fallback;
          }
          }
          return dependencies;
          }

          private void addAllInterfaces(Class clazz, List allInterfaces) {
          if (clazz == null) {
          return;
          }

          Class[] interfaces = clazz.getInterfaces();
          allInterfaces.addAll(Arrays.asList(interfaces));
          addAllInterfaces(clazz.getSuperclass(), allInterfaces);
          }

          重要的代碼都用黑體進行了標注,方法initializeObject中所調用的loadResource(obj, obj.getClass(), this);就執行了查找接口,并注入接口實現類整個過程。
          loadResource首先調用了getResourceDependencies(clazz);getResourceDependencies又調用了addAllInterfaces(resourceClass, interfaces);addAllInterfaces作用就是取得這個類包括這個類的父類的所有實現的接口,而getResourceDependencies方法就是對這個接口進行過濾,返回只是在配置中有的接口。setupAndOptionallyCreateResource(newDcm, depResource);進行的就是創建這些接口的實現類的對象,這個代碼的內容如下:

          private ResourceEnablerPair setupAndOptionallyCreateResource(DefaultComponentManager newDcm, Class depResource) throws Exception {
          ResourceEnablerPair pair = new ResourceEnablerPair();
          Object newResource = newDcm.resourceInstances.get(depResource);

          if (newResource == null) {
          newResource = ObjectFactory.getObjectFactory().buildBean(depResource);
          }

          pair.resource = newResource;

          Class enabler = loadResource(newResource, depResource, newDcm);
          pair.enabler = enabler;

          return pair;
          }

          因為準備創建出來的接口實現類對象的接口可能又實現了其他的接口,因此再調用了loadResource(newResource, depResource, newDcm)。對象創建了,然后就是注入這個對象,setupResource(resource, pair.enabler, pair.resource)就是起這個作用的。代碼如下:

          private void setupResource(Object resource, Class enabler, Object newResource) {
          if (enabler == null) {
          return;
          }

          try {
          enabler.getMethods()[0].invoke(resource, new Object[] {newResource});
          } catch (Exception e) {
          e.printStackTrace();

          if (log.isDebugEnabled()) {
          log.debug("Error invoking method for resource: " + resource.getClass().getName(), e);
          }
          }
          }

          每個接口只有一個set方法,通過反射機制調用這個方法將創建出來的接口實現對象注入進去。整個IOC就完成了。

          posted @ 2007-03-22 10:30 小小~咖啡豆 閱讀(337) | 評論 (0)編輯 收藏

          http://www.scriptviewer.com/story.php?title=IntelliJ-IDEA-60-aeae

          posted @ 2007-03-11 00:43 小小~咖啡豆 閱讀(349) | 評論 (0)編輯 收藏

          Idea6.0默認是用的自帶的jdk5啟動的,如果想用jdk6可以按如下辦法操作:


          1.將idea目錄下的jre目錄改名
          2.將安裝jdk6目錄中的jre目錄拷貝到idea目錄下,然后在將jdk6目錄中的jdk\lib\tools.jar拷貝到jre\lib下
          3.修改idea.exe.vmoptions文件中的啟動參數(這個不是必須的,只是提高idea的響應速度)
          posted @ 2007-02-01 08:09 小小~咖啡豆 閱讀(456) | 評論 (0)編輯 收藏

          C/C++ Source Add Block Comment Ctrl+Shift+/ C/C++ Editor
          C/C++ Source Add Include Ctrl+Shift+N C/C++ Editor
          C/C++ Source Comment Ctrl+/ C/C++ Editor
          C/C++ Source Find Declaration Ctrl+G C/C++ Editor
          C/C++ Source Find References Ctrl+Shift+G C/C++ Editor
          C/C++ Source Format Ctrl+Shift+F C/C++ Editor
          C/C++ Source Go to Matching Bracket Ctrl+Shift+P C/C++ Editor
          C/C++ Source Go to next C/C++ member Ctrl+Shift+向下鍵 C/C++ Editor
          C/C++ Source Go to previous C/C++ member Ctrl+Shift+向上鍵 C/C++ Editor
          C/C++ Source Open Declaration F3 C/C++ Editor
          C/C++ Source Open Definition Ctrl+F3 C/C++ Editor
          C/C++ Source Open Type Ctrl+Shift+T C/C++ Editor
          C/C++ Source Remove Block Comment Ctrl+Shift+\ C/C++ Editor
          C/C++ Source Show outline Ctrl+O C/C++ Editor
          C/C++ Source Uncomment Ctrl+\ C/C++ Editor
          Makefile Source Comment Ctrl+/ Makefile Editor
          Makefile Source Open declaration F3 Makefile Editor
          Makefile Source Uncomment Ctrl+\ Makefile Editor
          Refactor - C/C++ Redo - Refactoring Alt+Shift+Y C/C++ Editor
          Refactor - C/C++ Rename - Refactoring Alt+Shift+R C/C++ Editor
          Refactor - C/C++ Undo - Refactoring Alt+Shift+Z C/C++ Editor
          View Zoom In Ctrl+= 在窗口中
          View Zoom Out Ctrl+- 在窗口中
          搜索 工作空間中的聲明 Ctrl+G 在窗口中
          搜索 工作空間中的引用 Ctrl+Shift+G 在窗口中
          搜索 打開"搜索"對話框 Ctrl+H 在窗口中
          搜索 顯示"文件中的出現位置"快速菜單 Ctrl+Shift+U 在窗口中
          文件 "新建"菜單 Alt+Shift+N 在窗口中
          文件 保存 Ctrl+S 在窗口中
          文件 全部保存 Ctrl+Shift+S 在窗口中
          文件 全部關閉 Ctrl+Shift+F4 在窗口中
          文件 全部關閉 Ctrl+Shift+W 在窗口中
          文件 關閉 Ctrl+F4 在窗口中
          文件 關閉 Ctrl+W 在窗口中
          文件 刷新 F5 在窗口中
          文件 屬性 Alt+Enter 在窗口中
          文件 打印 Ctrl+P 在窗口中
          文件 新建 Ctrl+N 在窗口中
          文件 重命名 F2 在窗口中
          文本編輯 上一個詞語 Ctrl+左箭頭 編輯文本
          文本編輯 上滾行 Ctrl+向上鍵 編輯文本
          文本編輯 下一個詞語 Ctrl+右箭頭 編輯文本
          文本編輯 下滾行 Ctrl+向下鍵 編輯文本
          文本編輯 全部展開 Ctrl+Numpad_Multiply 編輯文本
          文本編輯 切換折疊 Ctrl+Numpad_Divide 編輯文本
          文本編輯 刪除上一個詞語 Ctrl+Backspace 編輯文本
          文本編輯 刪除下一個詞語 Ctrl+Delete 編輯文本
          文本編輯 刪除至行末 Ctrl+Shift+Delete 編輯文本
          文本編輯 刪除行 Ctrl+D 編輯文本
          文本編輯 在當前行上面插入行 Ctrl+Shift+Enter 編輯文本
          文本編輯 在當前行下面插入行 Shift+Enter 編輯文本
          文本編輯 復制行 Ctrl+Alt+向下鍵 編輯文本
          文本編輯 將行上移 Alt+向上鍵 編輯文本
          文本編輯 將行下移 Alt+向下鍵 編輯文本
          文本編輯 展開 Ctrl+Numpad_Add 編輯文本
          文本編輯 折疊 Ctrl+Numpad_Subtract 編輯文本
          文本編輯 改寫切換 Insert 編輯文本
          文本編輯 更改為大寫 Ctrl+Shift+X 編輯文本
          文本編輯 更改為小寫 Ctrl+Shift+Y 編輯文本
          文本編輯 選擇上一個詞語 Ctrl+Shift+左箭頭 編輯文本
          文本編輯 選擇下一個詞語 Ctrl+Shift+右箭頭 編輯文本
          文本編輯 重復行 Ctrl+Alt+向上鍵 編輯文本
          查看 Java 包資源管理器 Alt+Shift+Q,P 在窗口中
          查看 Java 聲明 Alt+Shift+Q,D 在窗口中
          查看 Java 類型層次結構 Alt+Shift+Q,T 在窗口中
          查看 Javadoc Alt+Shift+Q,J 在窗口中
          查看 變量 Alt+Shift+Q,V 在窗口中
          查看 同步 Alt+Shift+Q,Y 在窗口中
          查看 備忘單 Alt+Shift+Q,H 在窗口中
          查看 控制臺 Alt+Shift+Q,C 在窗口中
          查看 搜索 Alt+Shift+Q,S 在窗口中
          查看 斷點 Alt+Shift+Q,B 在窗口中
          查看 顯示視圖 (查看: 大綱) Alt+Shift+Q,O 在窗口中
          查看 顯示視圖 (查看: 問題) Alt+Shift+Q,X 在窗口中
          瀏覽 &Quick Cross References Alt+Shift+P 編輯 Java 源代碼
          瀏覽 Open AspectJ Type Alt+Shift+A 在窗口中
          瀏覽 Open AspectJ Type in Hierarchy Alt+Shift+H 在窗口中
          瀏覽 "顯示位置"菜單 Alt+Shift+W 在窗口中
          瀏覽 上一個編輯位置 Ctrl+Q 在窗口中
          瀏覽 下一頁 Ctrl+. 在窗口中
          瀏覽 前一頁 Ctrl+, 在窗口中
          瀏覽 前移歷史記錄 Alt+右箭頭 在窗口中
          瀏覽 后退歷史記錄 Alt+左箭頭 在窗口中
          瀏覽 在層次結構中打開類型 Ctrl+Shift+H 在窗口中
          瀏覽 快速大綱 Ctrl+O 編輯 Java 源代碼
          瀏覽 快速層次結構 Ctrl+T 編輯 Java 源代碼
          瀏覽 打開聲明 F3 在窗口中
          瀏覽 打開外部 Javadoc Shift+F2 在窗口中
          瀏覽 打開類型 Ctrl+Shift+T 在窗口中
          瀏覽 打開類型層次結構 F4 在窗口中
          瀏覽 打開結構 Ctrl+F3 編輯 Java 源代碼
          瀏覽 打開調用層次結構 Ctrl+Alt+H 在窗口中
          瀏覽 打開資源 Ctrl+Shift+R 在窗口中
          瀏覽 轉至上一個成員 Ctrl+Shift+向上鍵 編輯 Java 源代碼
          瀏覽 轉至下一個成員 Ctrl+Shift+向下鍵 編輯 Java 源代碼
          瀏覽 轉至匹配的方括號 Ctrl+Shift+P 編輯 Java 源代碼
          瀏覽 轉至行 Ctrl+L 編輯文本
          源代碼 切換 Ant 標記出現 Alt+Shift+O 編輯 Ant 構建文件
          源代碼 切換標記出現 Alt+Shift+O 編輯 Java 源代碼
          源代碼 切換注釋 Ctrl+/ 編輯 Java 源代碼
          源代碼 切換注釋 Ctrl+7 編輯 Java 源代碼
          源代碼 切換注釋 Ctrl+Shift+C 編輯 Java 源代碼
          源代碼 在文件中重命名 Alt+Shift+R 編輯 Ant 構建文件
          源代碼 快速輔助 - 在文件中重命名 Ctrl+2,R 編輯 Java 源代碼
          源代碼 快速輔助 - 指定給字段 Ctrl+2,F 編輯 Java 源代碼
          源代碼 快速輔助 - 指定給局部變量 Ctrl+2,L 編輯 Java 源代碼
          源代碼 打開外部文檔 Shift+F2 編輯 Ant 構建文件
          源代碼 顯示工具提示描述 F2 編輯 Ant 構建文件
          源代碼 顯示源代碼快速菜單 Alt+Shift+S 在窗口中
          源代碼 格式 Ctrl+Shift+F 編輯 Ant 構建文件
          源代碼 格式化 Ctrl+Shift+F 編輯 Java 源代碼
          源代碼 添加 Javadoc 注釋 Alt+Shift+J 在窗口中
          源代碼 添加塊注釋 Ctrl+Shift+/ 編輯 Java 源代碼
          源代碼 添加導入 Ctrl+Shift+M 編輯 Java 源代碼
          源代碼 組織導入 Ctrl+Shift+O 在窗口中
          源代碼 縮進行 Ctrl+I 編輯 Java 源代碼
          源代碼 除去出現注釋 Alt+Shift+U 編輯 Java 源代碼
          源代碼 除去塊注釋 Ctrl+Shift+\ 編輯 Java 源代碼
          源代碼 添加 try catch 塊 Alt+Shift+Z + Y 編輯 Java 源代碼

          窗口 上一個編輯器 Ctrl+Shift+F6 在窗口中
          窗口 上一個視圖 Ctrl+Shift+F7 在窗口中
          窗口 上一個透視圖 Ctrl+Shift+F8 在窗口中
          窗口 下一個編輯器 Ctrl+F6 在窗口中
          窗口 下一個視圖 Ctrl+F7 在窗口中
          窗口 下一個透視圖 Ctrl+F8 在窗口中
          窗口 切換至編輯器 Ctrl+Shift+E 在窗口中
          窗口 將活動視圖或編輯器最大化 Ctrl+M 在窗口中
          窗口 打開編輯器下拉列表 Ctrl+E 在窗口中
          窗口 顯示標尺上下文菜單 Ctrl+F10 編輯文本
          窗口 顯示系統菜單 Alt+- 在窗口中
          窗口 顯示視圖菜單 Ctrl+F10 在窗口中
          窗口 顯示鍵輔助 Ctrl+Shift+L 在對話框和窗口中
          窗口 激活編輯器 F12 在窗口中
          編輯 Add Block Comment Ctrl+Shift+/ Editing in Structured Text Editors
          編輯 Format Active Elements Ctrl+I Editing in Structured Text Editors
          編輯 Format Document Ctrl+Shift+F Editing in Structured Text Editors
          編輯 Move Alt+Shift+V Editing JSP Source
          編輯 Occurrences in File Ctrl+Shift+A Editing in Structured Text Editors
          編輯 Open Selection F3 Editing in Structured Text Editors
          編輯 Quick Fix Ctrl+1 Editing in Structured Text Editors
          編輯 Remove Block Comment Ctrl+Shift+\ Editing in Structured Text Editors
          編輯 Rename Alt+Shift+R Editing JSP Source
          編輯 Rename XSD element Alt+Shift+R Editing XSD context
          編輯 Restore Last Selection Alt+Shift+向下鍵 Editing in Structured Text Editors
          編輯 Select Enclosing Element Alt+Shift+向上鍵 Editing in Structured Text Editors
          編輯 Select Next Element Alt+Shift+右箭頭 Editing in Structured Text Editors
          編輯 Select Previous Element Alt+Shift+左箭頭 Editing in Structured Text Editors
          編輯 Show Tooltip Description F2 Editing in Structured Text Editors
          編輯 Toggle Comment Ctrl+Shift+C Editing in Structured Text Editors
          編輯 "快速差別"開關 Ctrl+Shift+Q 編輯文本
          編輯 上下文信息 Alt+? 在窗口中
          編輯 上下文信息 Alt+Shift+? 在窗口中
          編輯 內容輔助 Alt+/ 在對話框和窗口中
          編輯 切換插入方式 Ctrl+Shift+Insert 編輯文本
          編輯 刪除 Delete 在窗口中
          編輯 剪切 Ctrl+X 在對話框和窗口中
          編輯 剪切 Shift+Delete 在對話框和窗口中
          編輯 增量查找 Ctrl+J 編輯文本
          編輯 增量逆向查找 Ctrl+Shift+J 編輯文本
          編輯 復制 Ctrl+C 在對話框和窗口中
          編輯 復制 Ctrl+Insert 在對話框和窗口中
          編輯 復原上一個選擇 Alt+Shift+向下鍵 編輯 Java 源代碼
          編輯 快速修正 Ctrl+1 在窗口中
          編輯 撤消 Ctrl+Z 在窗口中
          編輯 文字補全 Ctrl+Alt+/ 編輯文本
          編輯 顯示工具提示描述 F2 編輯 Java 源代碼
          編輯 查找上一個 Ctrl+Shift+K 編輯文本
          編輯 查找下一個 Ctrl+K 編輯文本
          編輯 查找并替換 Ctrl+F 在窗口中
          編輯 粘貼 Ctrl+V 在對話框和窗口中
          編輯 粘貼 Shift+Insert 在對話框和窗口中
          編輯 選擇上一個元素 Alt+Shift+左箭頭 編輯 Java 源代碼
          編輯 選擇下一個元素 Alt+Shift+右箭頭 編輯 Java 源代碼
          編輯 選擇全部 Ctrl+A 在對話框和窗口中
          編輯 選擇外層元素 Alt+Shift+向上鍵 編輯 Java 源代碼
          編輯 重做 Ctrl+Y 在窗口中
          運行/調試 Debug AspectJ/Java Application Alt+Shift+D,C 在窗口中
          運行/調試 Debug on Server Alt+Shift+D,R 在窗口中
          運行/調試 EOF Ctrl+Z 在控制臺中
          運行/調試 Profile on Server Alt+Shift+P,R 在窗口中
          運行/調試 Run AspectJ/Java Application Alt+Shift+X,C 在窗口中
          運行/調試 Run on Server Alt+Shift+X,R 在窗口中
          運行/調試 切換單步執行過濾器 Shift+F5 在窗口中
          運行/調試 切換行斷點 Ctrl+Shift+B 在窗口中
          運行/調試 單步跳入 F5 調試
          運行/調試 單步跳入選擇的內容 Ctrl+F5 調試
          運行/調試 單步跳過 F6 調試
          運行/調試 單步返回 F7 調試
          運行/調試 執行 Ctrl+U 在窗口中
          運行/調試 顯示 Ctrl+Shift+D 在對話框和窗口中
          運行/調試 檢查 Ctrl+Shift+I 在對話框和窗口中
          運行/調試 繼續 F8 調試
          運行/調試 調試 Ant 構建 Alt+Shift+D,Q 在窗口中
          運行/調試 調試 Eclipse 應用程序 Alt+Shift+D,E 在窗口中
          運行/調試 調試 JUnit 插件測試 Alt+Shift+D,P 在窗口中
          運行/調試 調試 JUnit 測試 Alt+Shift+D,T 在窗口中
          運行/調試 調試 Java Applet Alt+Shift+D,A 在窗口中
          運行/調試 調試 Java 應用程序 Alt+Shift+D,J 在窗口中
          運行/調試 調試 SWT 應用程序 Alt+Shift+D,S 在窗口中
          運行/調試 調試上次啟動 F11 在窗口中
          運行/調試 運行 Ant 構建 Alt+Shift+X,Q 在窗口中
          運行/調試 運行 Eclipse 應用程序 Alt+Shift+X,E 在窗口中
          運行/調試 運行 JUnit 插件測試 Alt+Shift+X,P 在窗口中
          運行/調試 運行 JUnit 測試 Alt+Shift+X,T 在窗口中
          運行/調試 運行 Java Applet Alt+Shift+X,A 在窗口中
          運行/調試 運行 Java 應用程序 Alt+Shift+X,J 在窗口中
          運行/調試 運行 SWT 應用程序 Alt+Shift+X,S 在窗口中
          運行/調試 運行上次啟動 Ctrl+F11 在窗口中
          運行/調試 運行至行 Ctrl+R 調試
          重構 - Java 內聯 Alt+Shift+I 在窗口中
          重構 - Java 將局部變量轉換為字段 Alt+Shift+F 編輯 Java 源代碼
          重構 - Java 抽取局部變量 Alt+Shift+L 在窗口中
          重構 - Java 抽取方法 Alt+Shift+M 在窗口中
          重構 - Java 撤銷 - 重構 Alt+Shift+Z 在窗口中
          重構 - Java 顯示重構快速菜單 Alt+Shift+T 在窗口中
          重構 - Java 更改方法特征符 Alt+Shift+C 在窗口中
          重構 - Java 移動 - 重構 Alt+Shift+V 在窗口中
          重構 - Java 重做 - 重構 Alt+Shift+Y 在窗口中
          重構 - Java 重命名 - 重構 Alt+Shift+R 在窗口中
          項目 全部構建 Ctrl+B 在窗口中

          posted @ 2006-12-31 11:17 小小~咖啡豆 閱讀(237) | 評論 (0)編輯 收藏

          JDK的開源已經發布了
          https://openjdk.dev.java.net/

          posted @ 2006-11-14 08:08 小小~咖啡豆 閱讀(479) | 評論 (0)編輯 收藏

          common mail是一個小而方便的mail包,他實現了對Java Mail的封裝,使用起來十分的方便,但是我在使用他的時候發現,使用純文本的內容發送,結果是亂碼,代碼如下:
          public class TestCommonMail {
          public static void main(String[] args) throws EmailException, MessagingException {
          SimpleEmail email = new SimpleEmail();
          email.setCharset("GB2312");
          email.setHostName("smtp.163.com");
          email.setSubject("test");
          email.addTo("test@163.com");
          email.setFrom("test@163.com");
          email.setMsg("我的測試");
          email.setAuthentication("test", "test");
          email.send();
          }
          }

          分析了一下commons mail的源碼找到了原因。源碼如下:
          public class SimpleEmail extends Email
          {
          public Email setMsg(String msg) throws EmailException, MessagingException
          {
          if (EmailUtils.isEmpty(msg))
          {
          throw new EmailException("Invalid message supplied");
          }

          setContent(msg, TEXT_PLAIN);
          return this;
          }
          }

          Email代碼片段
          public void setContent(Object aObject, String aContentType)
          {
          this.content = aObject;
          if (EmailUtils.isEmpty(aContentType))
          {
          this.contentType = null;
          }
          else
          {
          // set the content type
          this.contentType = aContentType;

          // set the charset if the input was properly formed
          String strMarker = "; charset=";
          int charsetPos = aContentType.toLowerCase().indexOf(strMarker);
          if (charsetPos != -1)
          {
          // find the next space (after the marker)
          charsetPos += strMarker.length();
          int intCharsetEnd =
          aContentType.toLowerCase().indexOf(" ", charsetPos);

          if (intCharsetEnd != -1)
          {
          this.charset =
          aContentType.substring(charsetPos, intCharsetEnd);
          }
          else
          {
          this.charset = aContentType.substring(charsetPos);
          }
          }
          }
          }

          email.send();的send方法將調用
          public void buildMimeMessage() throws EmailException
          {
          try
          {
          this.getMailSession();
          this.message = new MimeMessage(this.session);

          if (EmailUtils.isNotEmpty(this.subject))
          {
          if (EmailUtils.isNotEmpty(this.charset))
          {
          this.message.setSubject(this.subject, this.charset);
          }
          else
          {
          this.message.setSubject(this.subject);
          }
          }

          // ========================================================
          // Start of replacement code
          if (this.content != null)
          {
          this.message.setContent(this.content, this.contentType);
          }
          // end of replacement code
          // ========================================================
          else if (this.emailBody != null)
          {
          this.message.setContent(this.emailBody);
          }
          else
          {
          this.message.setContent("", Email.TEXT_PLAIN);
          }

          if (this.fromAddress != null)
          {
          this.message.setFrom(this.fromAddress);
          }
          else
          {
          throw new EmailException("Sender address required");
          }

          if (this.toList.size() + this.ccList.size() + this.bccList.size() == 0)
          {
          throw new EmailException(
          "At least one receiver address required");
          }

          if (this.toList.size() > 0)
          {
          this.message.setRecipients(
          Message.RecipientType.TO,
          this.toInternetAddressArray(this.toList));
          }

          if (this.ccList.size() > 0)
          {
          this.message.setRecipients(
          Message.RecipientType.CC,
          this.toInternetAddressArray(this.ccList));
          }

          if (this.bccList.size() > 0)
          {
          this.message.setRecipients(
          Message.RecipientType.BCC,
          this.toInternetAddressArray(this.bccList));
          }

          if (this.replyList.size() > 0)
          {
          this.message.setReplyTo(
          this.toInternetAddressArray(this.replyList));
          }

          if (this.headers.size() > 0)
          {
          Iterator iterHeaderKeys = this.headers.keySet().iterator();
          while (iterHeaderKeys.hasNext())
          {
          String name = (String) iterHeaderKeys.next();
          String value = (String) headers.get(name);
          this.message.addHeader(name, value);
          }
          }

          if (this.message.getSentDate() == null)
          {
          this.message.setSentDate(getSentDate());
          }

          if (this.popBeforeSmtp)
          {
          Store store = session.getStore("pop3");
          store.connect(this.popHost, this.popUsername, this.popPassword);
          }
          }
          catch (MessagingException me)
          {
          throw new EmailException(me);
          }
          }
          由代碼可以知道純文本方式最終調用了Java Mail的
          message.setContent(this.content, this.contentType);
          content是內容
          contentType是類型,如text/plain,
          (我們可以試試直接用Java mail發郵件,設置文本內容不使用setText方法,也使用setContent("測試", "text/plain")方式,你可以看到內容也是亂碼)
          關鍵就在于text/plain,我們改成text/plain;charset=gb2312,ok亂碼解決了。在commons mail我們看SimpleEmail 類中setMsg方法調用的就是 setContent(msg, TEXT_PLAIN);我們只需要將Email類中的常量TEXT_PLAIN修改一下加入 charset=你的字符集 ,重新打包jar,這樣就可以了

          posted @ 2006-09-18 09:54 小小~咖啡豆 閱讀(1379) | 評論 (0)編輯 收藏

          package org.wzywjy.mail;

          import java.util.Date;
          import java.util.Properties;
          import javax.mail.Message;
          import javax.mail.MessagingException;
          import javax.mail.Session;
          import javax.mail.Transport;
          import javax.mail.internet.AddressException;
          import javax.mail.internet.InternetAddress;
          import javax.mail.internet.MimeMessage;

          public class TestMail {

          public final static String SMTPSERVER = "smtp.163.com";
          public final static String POPSERVER = "pop.163.com";
          public final static String ACCOUNT = "test";
          public final static String PWD = "test";
          public final static String MAILADDR = "test@163.com";

          public void sendMail(String to, String from, String subject, String body) throws AddressException, MessagingException {
          Properties pro = System.getProperties();
          pro.put("mail.smtp.host", SMTPSERVER);
          pro.put("mail.smtp.auth", "true");
          Session session = Session.getDefaultInstance(pro, null);

          Message msg = new MimeMessage(session);
          msg.setFrom(new InternetAddress(from));
          msg.setRecipient(Message.RecipientType.TO, InternetAddress.parse(to, false)[0]);
          msg.setSubject(subject);
          msg.setText(body);
          msg.setHeader("X-Mailer", "LOTONtechEmail");
          msg.setSentDate(new Date());

          Transport transport = session.getTransport("smtp");
          System.out.println("connecting...");
          transport.connect(SMTPSERVER, ACCOUNT, PWD);
          System.out.println("Sending message");
          transport.sendMessage(msg, msg.getAllRecipients());
          transport.close();
          }

          public static void main(String[] args) {
          TestMail test = new TestMail();
          try {
          test.sendMail(MAILADDR, MAILADDR, "test", "我的一個測試");
          } catch (AddressException e) {
          e.printStackTrace();
          } catch (MessagingException e) {
          e.printStackTrace();
          }
          }
          }

          posted @ 2006-09-15 10:30 小小~咖啡豆 閱讀(304) | 評論 (1)編輯 收藏

          注:這是我在網上找到的一篇文章,我按文章的步驟操作,還是有不少問題,在此我進行了修改. 原來文章地址 http://www.java-asp.net/java/200601/t_56635.html


          用eclipse+xdoclet+axis開發WebService

          在eclipse下開發基于axis的WebService其實很簡單,但也有不少步驟,以下對每個步驟進行說明

          /**
          *
          * @author honghao
          * @axis.service scope = "Request" urn = "TestService"
          */
          public class TestService {
          /**
          * @param name
          * @axis.method
          */
          public String test(String name){
          return "hello " + name;
          }
          }

          其中@axis.service表示TestService作為服務類,@axis.method表示輸出test()方法作為WebService

          4) 配置xdoclet
          打開project->properties,在XDoclet Configurations中添加一個新的配置,可以任意取名,此處為"webservice",在這個配置中添加ejbdoclet,在其中再添加fileset用于指定對哪些文件執行xdoclet(要指明文件的路徑),和axisdeploy,axisdeploy不用進行任何設置。在ejbdoclet中需要指定destdir生用于指示生成的文件所在的路徑。


          5) 運行xdoclet.右擊項目工程,執行Run Xdoclet菜單,如果配置沒有錯誤的話,應該在目標路徑下生成deploy-TestService.xml,這是一個axis用于生成server-config.wsdd文件的部署文件。
          這里需要注意的是,如果類文件是放在某個包下的時候,我無法生成deploy-TestService.xml文件,但是將類文件放在根路徑下就可以生成了,原因不名.

          6) 生成server-config.wsdd部署文件.這個文件其實是由axis的一個工具生成的,但是直接運行這個工具太麻煩,所以我們還需要生成一個ant構建文件,使其能自動生成:
          在web項目的根路徑下新建build.xml,輸入以下文本:
          <?xml version="1.0" encoding="UTF-8"?>
          <property name="axis_lib_path" value="${axis.lib}"/>
          <property name="wsdl.dir" location="wsdl" />
          <path id="axis.lib.path">
          <fileset dir="${axis_lib_path}">
          <include name="*.jar" />
          </fileset>
          </path>
          <path id="project.classpath">
          <pathelement location="wsdl"/>
          </path>
          <target name="deploy">
          <java classname="org.apache.axis.utils.Admin" fork="true" dir="WEB-INF"> <!--dir對應生成文件的位置-->
          <classpath refid="axis.lib.path" />
          <arg value="server" />
          <arg value="${wsdl.dir}/deploy-TestHandler.xml" /> <!--TestHandler對應上面的xml文件名TestService-->
          </java>
          </target>
          </project>
          注意:wsdl是剛才生成的deploy-TestService.xml所在的目錄,org.apache.axis.utils.Admin是axis提供的工具類,其他路徑設置請根據實際項目進行適當調整。
          保存后,右擊build.xml執行Run->Ant 構建,如果配置正確,則會在WEB-INF目錄下生成server-config.wsdd文件
          還有ant編譯的時候需要axis的lib。

          7) 仿造axis提供的sample工程在tomcat中進行部署,在瀏覽器中輸入
          http://localhost:8080/axis/services
          會列出所有的WebService
          輸入http://localhost:8080/axis/services/TestService?wsdl
          會輸出相應的wsdl內容
          然后就可以用相應的工具進行測試了。

          posted @ 2006-09-06 08:24 小小~咖啡豆 閱讀(2338) | 評論 (0)編輯 收藏
          僅列出標題
          共3頁: 上一頁 1 2 3 下一頁 
          主站蜘蛛池模板: 凤庆县| 巴东县| 四会市| 盐源县| 察哈| 长子县| 刚察县| 简阳市| 临汾市| 南郑县| 长春市| 徐水县| 东阿县| 井研县| 三河市| 抚松县| 上思县| 黔南| 义乌市| 璧山县| 水富县| 南涧| 葵青区| 磴口县| 华坪县| 长沙市| 石狮市| 交城县| 和顺县| 遂昌县| 陈巴尔虎旗| 桦甸市| 香格里拉县| 积石山| 会昌县| 陇川县| 大城县| 桦甸市| 富阳市| 庆城县| 嘉祥县|