西部村里人

          常用鏈接

          統計

          其它BLOG

          最新評論

          2006年3月16日 #

          WebWork標簽技術的簡化版本--JSP模板

             WebWork標簽非常出色,在有些應用中,可能不想使用其環境,但希望能夠使用其標簽思路。JSP模板的使用面更廣,隨簡化WebWork方式,提供思路供大家參考。

          1、開發標簽基礎類:
          import java.io.IOException;
          import java.io.OutputStream;
          import java.io.OutputStreamWriter;
          import java.io.PrintWriter;
          import java.io.RandomAccessFile;
          import java.io.Writer;
          import java.util.Iterator;
          import java.util.LinkedList;

          import javax.servlet.RequestDispatcher;
          import javax.servlet.ServletException;
          import javax.servlet.ServletOutputStream;
          import javax.servlet.ServletRequest;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;
          import javax.servlet.http.HttpServletResponseWrapper;
          import javax.servlet.jsp.JspException;
          import javax.servlet.jsp.tagext.BodyTagSupport;
          public abstract class AbstractTag extends BodyTagSupport {

              protected String templateName ;

              private final static String templatePath = "/WEB-INF/tags/";

              private static final long serialVersionUID = -1201668454354226175L;

              public String getTemplateName() {
                  return templateName;
              }

              public void setTemplateName(String templateName) {
                  this.templateName = templateName;
              }

              protected String getBody() {
                  if (bodyContent == null) {
                      return "";
                  } else {
                      return bodyContent.getString().trim();
                  }
              }
             
              protected abstract void prepareData ();

              public int doEndTag() throws JspException {
                  try {
                      prepareData ();
                      include(templatePath + this.getTemplateName(), pageContext.getOut(),
                              pageContext.getRequest(),
                              (HttpServletResponse) pageContext.getResponse());

                  } catch (Exception e) {
                      // e.printStackTrace();
                      throw new JspException(e);
                  }
                  return EVAL_BODY_INCLUDE;
              }

              public int doStartTag() throws JspException {
                  try {
                      pageContext.getOut().write(getBody());
                  } catch (IOException e) {
                      throw new RuntimeException("IOError: " + e.getMessage(), e);
                  }
                  return EVAL_PAGE;
              }

              public static void include(String aResult, Writer writer,ServletRequest request,
                      HttpServletResponse response) throws ServletException, IOException {
                  String resourcePath = aResult;
                  RequestDispatcher rd = request.getRequestDispatcher(resourcePath);
                  if (rd == null) {
                      throw new ServletException("Not a valid resource path:"
                              + resourcePath);
                  }
                  // Include the resource
                  PageResponse pageResponse = new PageResponse(response);

                  // Include the resource
                  rd.include((HttpServletRequest) request, pageResponse);

                  // write the response back to the JspWriter, using the correct encoding.
                  String encoding = "GB2312";

                  if (encoding != null) {
                      // use the encoding specified in the property file
                      pageResponse.getContent().writeTo(writer, encoding);
                  } else {
                      // use the platform specific encoding
                      pageResponse.getContent().writeTo(writer, null);
                  }
              }

              static final class PageResponse extends HttpServletResponseWrapper {

                  protected PrintWriter pagePrintWriter;

                  protected ServletOutputStream outputStream;

                  private PageOutputStream pageOutputStream = null;

                  /**
                   * Create PageResponse wrapped around an existing HttpServletResponse.
                   */
                  public PageResponse(HttpServletResponse response) {
                      super(response);
                  }

                  /**
                   * Return the content buffered inside the {@link PageOutputStream}.
                   *
                   * @return
                   * @throws IOException
                   */
                  public FastByteArrayOutputStream getContent() throws IOException {
                      // if we are using a writer, we need to flush the
                      // data to the underlying outputstream.
                      // most containers do this - but it seems Jetty 4.0.5 doesn't
                      if (pagePrintWriter != null) {
                          pagePrintWriter.flush();
                      }

                      return ((PageOutputStream) getOutputStream()).getBuffer();
                  }

                  /**
                   * Return instance of {@link PageOutputStream} allowing all data written
                   * to stream to be stored in temporary buffer.
                   */
                  public ServletOutputStream getOutputStream() throws IOException {
                      if (pageOutputStream == null) {
                          pageOutputStream = new PageOutputStream();
                      }

                      return pageOutputStream;
                  }

                  /**
                   * Return PrintWriter wrapper around PageOutputStream.
                   */
                  public PrintWriter getWriter() throws IOException {
                      if (pagePrintWriter == null) {
                          pagePrintWriter = new PrintWriter(new OutputStreamWriter(
                                  getOutputStream(), getCharacterEncoding()));
                      }

                      return pagePrintWriter;
                  }
              }

              static final class PageOutputStream extends ServletOutputStream {

                  private FastByteArrayOutputStream buffer;

                  public PageOutputStream() {
                      buffer = new FastByteArrayOutputStream();
                  }

                  /**
                   * Return all data that has been written to this OutputStream.
                   */
                  public FastByteArrayOutputStream getBuffer() throws IOException {
                      flush();

                      return buffer;
                  }

                  public void close() throws IOException {
                      buffer.close();
                  }

                  public void flush() throws IOException {
                      buffer.flush();
                  }

                  public void write(byte[] b, int o, int l) throws IOException {
                      buffer.write(b, o, l);
                  }

                  public void write(int i) throws IOException {
                      buffer.write(i);
                  }

                  public void write(byte[] b) throws IOException {
                      buffer.write(b);
                  }
              }
             
             
              static public class FastByteArrayOutputStream extends OutputStream {

                  // Static --------------------------------------------------------
                  private static final int DEFAULT_BLOCK_SIZE = 8192;


                  private LinkedList buffers;

                  // Attributes ----------------------------------------------------
                  // internal buffer
                  private byte[] buffer;

                  // is the stream closed?
                  private boolean closed;
                  private int blockSize;
                  private int index;
                  private int size;


                  // Constructors --------------------------------------------------
                  public FastByteArrayOutputStream() {
                      this(DEFAULT_BLOCK_SIZE);
                  }

                  public FastByteArrayOutputStream(int aSize) {
                      blockSize = aSize;
                      buffer = new byte[blockSize];
                  }


                  public int getSize() {
                      return size + index;
                  }

                  public void close() {
                      closed = true;
                  }

                  public byte[] toByteArray() {
                      byte[] data = new byte[getSize()];

                      // Check if we have a list of buffers
                      int pos = 0;

                      if (buffers != null) {
                          Iterator iter = buffers.iterator();

                          while (iter.hasNext()) {
                              byte[] bytes = (byte[]) iter.next();
                              System.arraycopy(bytes, 0, data, pos, blockSize);
                              pos += blockSize;
                          }
                      }

                      // write the internal buffer directly
                      System.arraycopy(buffer, 0, data, pos, index);

                      return data;
                  }

                  public String toString() {
                      return new String(toByteArray());
                  }

                  // OutputStream overrides ----------------------------------------
                  public void write(int datum) throws IOException {
                      if (closed) {
                          throw new IOException("Stream closed");
                      } else {
                          if (index == blockSize) {
                              addBuffer();
                          }

                          // store the byte
                          buffer[index++] = (byte) datum;
                      }
                  }

                  public void write(byte[] data, int offset, int length) throws IOException {
                      if (data == null) {
                          throw new NullPointerException();
                      } else if ((offset < 0) || ((offset + length) > data.length) || (length < 0)) {
                          throw new IndexOutOfBoundsException();
                      } else if (closed) {
                          throw new IOException("Stream closed");
                      } else {
                          if ((index + length) > blockSize) {
                              int copyLength;

                              do {
                                  if (index == blockSize) {
                                      addBuffer();
                                  }

                                  copyLength = blockSize - index;

                                  if (length < copyLength) {
                                      copyLength = length;
                                  }

                                  System.arraycopy(data, offset, buffer, index, copyLength);
                                  offset += copyLength;
                                  index += copyLength;
                                  length -= copyLength;
                              } while (length > 0);
                          } else {
                              // Copy in the subarray
                              System.arraycopy(data, offset, buffer, index, length);
                              index += length;
                          }
                      }
                  }

                  // Public
                  public void writeTo(OutputStream out) throws IOException {
                      // Check if we have a list of buffers
                      if (buffers != null) {
                          Iterator iter = buffers.iterator();

                          while (iter.hasNext()) {
                              byte[] bytes = (byte[]) iter.next();
                              out.write(bytes, 0, blockSize);
                          }
                      }

                      // write the internal buffer directly
                      out.write(buffer, 0, index);
                  }

                  public void writeTo(RandomAccessFile out) throws IOException {
                      // Check if we have a list of buffers
                      if (buffers != null) {
                          Iterator iter = buffers.iterator();

                          while (iter.hasNext()) {
                              byte[] bytes = (byte[]) iter.next();
                              out.write(bytes, 0, blockSize);
                          }
                      }

                      // write the internal buffer directly
                      out.write(buffer, 0, index);
                  }

                  public void writeTo(Writer out, String encoding) throws IOException {
                      // Check if we have a list of buffers
                      if (buffers != null) {
                          Iterator iter = buffers.iterator();

                          while (iter.hasNext()) {
                              byte[] bytes = (byte[]) iter.next();

                              if (encoding != null) {
                                  out.write(new String(bytes, encoding));
                              } else {
                                  out.write(new String(bytes));
                              }
                          }
                      }

                      // write the internal buffer directly
                      if (encoding != null) {
                          out.write(new String(buffer, 0, index, encoding));
                      } else {
                          out.write(new String(buffer, 0, index));
                      }
                  }

                  /**
                   * Create a new buffer and store the
                   * current one in linked list
                   */
                  protected void addBuffer() {
                      if (buffers == null) {
                          buffers = new LinkedList();
                      }

                      buffers.addLast(buffer);

                      buffer = new byte[blockSize];
                      size += index;
                      index = 0;
                  }
              }
          }

          2、定義一個具體的標簽類
          public class ListTag extends RiseAbstractTag {

              private static final long serialVersionUID = 3385568988234498913L;

              protected String templateName = "list.jsp";

              private String id;

              public String getId() {
                  return id;
              }

              public void setId(String id) {
                  this.id = id;
              }

              protected void prepareData() {
                  this.setTemplateName(this.templateName);
                  pageContext.getRequest().setAttribute("id", this.id);
              }
          }

          3、定義TLD文件
             參考TLD文檔
          4、定義list.jsp模板
          <%@ page contentType="text/html; charset=GBK" %>

          <%
          String id = (String)request.getAttribute("id");
          %>
          <table width="90%" border="0" cellpadding="0" cellspacing="2">

            <tr>
              <td>Id</td>
              <td align="right"><%= id %></td>
            </tr>
          </table>
          5、使用默認模板
             <WWTag:list id="Hello World!"/>
          6、使用自定義模板
             a: 定義模板
          <%@ page contentType="text/html; charset=GBK" %>

          <%
          String id = (String)request.getAttribute("id");
          out.println("Id is : " + id);
          %>
             b: use it , 模板名:testList.jsp,放在/WEB-INF/tags目錄下
             <WWTag:list id="Hello World!" templateName="testList.jsp"/>


          posted @ 2006-03-16 22:57 西部村里人 閱讀(1122) | 評論 (2)編輯 收藏

          團隊在局域網中共享ADSL方法(windows, linux)

             團隊在外地封閉開發,沒有帶交換機功能能的HUB來連接到ADSL。只好共享ADSL方式。
             1、windows下共享非常簡單,把ADSL共享即可,但不要把每一個連接都撥號給選上,否則無法使用。此時局域網內IP地址在192.168.0.1--192.168.0.2XX之間。問題是:容易壞,不穩定。隨改用Linux。
             2、Linux環境下使用。Red Hat Linux ES3版本。(文檔來自網絡收集,共享大家使用)

          http://www.chinalinuxpub.com/read.php?wid=558

           

           

          1、 網卡配置。
          我這里用的網卡是RTL80293com905。在系統中,RTL8029標記為eth03com905標記為eth1RTL80293com905IP地址分別是192.168.0.1192.168.1.1(其他的地址也可),掩碼均為255.255.255.0
          eth0用于連接網通,eth1用于連接內網,局域網網段為192.168.0.0
          注意:此處兩塊網卡均不能設網關。
          2 PPPoE軟件的升級與安裝
          1) 在 http://www.roaringpenguin.com/pppoe/#download 下載
          2) 安裝rp-pppoe。以root身份執行
          rpm Uvh rp-pppoe-3.5-1.i386.rpm
          3、 修改/etc/ sysctl.conf
          將其中的
          net.ipv4.ip_forward = 0
          改為
          net.ipv4.ip_forward = 1
          4、 去除ipchains模塊,只選擇iptables方法如下:
          1setup
          2)選擇system service
          3)去除ipchains
          4)選中iptables
          5)重啟機器
          5 PPPoE客戶端配置
          rp-pppoe-3.5-1.i386.rpm安裝完畢后,接下來就可進行PPPoE客戶端配置了。過程如下。
          #/usr/sbin/adsl-setup
          >>> Enter your PPPoE user name: ——此處輸入撥號帳號的用戶名
          >>> Enter the Ethernet interface connected to the ADSL modem For Solaris, this is likely to be something like /dev/hme0. For Linux, it will be ethn, where 'n' is a number. (default eth0): ——輸eth0
          >>> Enter the demand value (default no): ——輸no
          >>> Enter the DNS information here: ——輸210.83.130.18
          >>> Please enter your PPPoE password: ——輸網通用戶口令
          >>> Choose a type of firewall (0-2): ——輸0
          >>> Accept these settings and adjust configuration files (y/n)? ——輸y
          6、 啟動撥號連接
          /usr/sbin/adsl-start
          成功連接后,屏幕顯示Connected
          此時這臺linux已可以上網瀏覽了。
          7 IP偽裝
          為了使局域網中的其他機器能通過Linux服務器共享上網,至少須執行下面的命令:
          iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
          完成后,在192.168.0.0網段(網關為192.168.0.1)的PC機就可透過Linux上網了!
          8、 開機自啟動
          為了使Linux服務器能夠自動撥號,執行下面步驟。
          1chkconfig --add adsl
          2setup
          3)選擇system services
          4)選中ADSL
          5OK退出
          6)打開/etc/rc.d/rc.local,在該文件的末尾添上下面語句
          echo "[OK]"
          echo "Drop ICMP form anywhere"
          echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all
          echo "[OK]"
          iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
          說明:前面四句用于關閉ICMP,防止別人Ping
          9、 至此,一切OK,一個簡單的撥號建成了。重啟機器后,發現linuxinternet共享連接已經一切就緒了,好妙!!!

          為了建立更安全的撥號連接,請再設置各種安全機制吧,好事多磨嘛。
          另外,如果網關后面的客戶機無法通過linux上網,請留意一下linux的防火墻設置。

          REDHAT9ADSL最終解決方案

           

          發布于2005-05-29 被讀559 【字體:大 小】

           

          LINUXSIRLINUXFANS上看了很多關于ADSL的文章,都沒有解決我的REDHAT9ADSL上網的問題,今天實在是沒有辦法,重新建立連接,曲折的經歷,終于上網了(非常激動,可能表達的不是很好),特的寫下我的過程,作為參考:

           

          REDHAT默認的PPPOE有問題,需要RPM -E,然后,安裝這個 --實際在RedES3上沒必要按照這個共享包。

          http://www.roaringpenguin.com/pppoe/rp-pppoe-3.5.tar.gz(北南兄推薦)

          解壓和安裝:

          #tar zxvf rp-pppoe-3.5.tar.gz

          進入解壓目錄執行

          #sh ./go

           

           

          然后再來設置ADSL。這一處,我們要用命令。

           

          #adsl-setup

           

           

          Welcome to the Roaring Penguin ADSL client setup. First, I will run

          some checks on your system to make sure the PPPoE client is installed

          properly...

           

          Looks good! Now, please enter some information:

           

          USER NAME

           

          >>> Enter your PPPoE user name (default XXX): 在這里輸入ADSL的用戶名

           

          INTERFACE

           

          >>> Enter the Ethernet interface connected to the ADSL modem

          For Solaris, this is likely to be something like /dev/hme0.

          For Linux, it will be ethn, where 'n' is a number.

          (default eth0):如果一張網卡就設置寫上eth0

           

          Do you want the link to come up on demand, or stay up continuously?

          If you want it to come up on demand, enter the idle time in seconds

          after which the link should be dropped. If you want the link to

          stay up permanently, enter 'no' (two letters, lower-case.)

          NOTE: Demand-activated links do not interact well with dynamic IP

          addresses. You may have some problems with demand-activated links.

          >>> Enter the demand value (default no):不用寫什么

           

          DNS

          Please enter the IP address of your ISP's primary DNS server.

          If your ISP claims that 'the server will provide DNS addresses',

          enter 'server' (all lower-case) here.

          If you just press enter, I will assume you know what you are

          doing and not modify your DNS setup.

          >>> Enter the DNS information here:在這里寫上202.96.134.133

          下一個DNS202.96.168.68 //這里根據個人不同可以修改

           

          PASSWORD

           

          >>> Please enter your PPPoE password:輸入密碼

          >>> Please re-enter your PPPoE password:再輸入一次

           

          FIREWALLING

           

          Please choose the firewall rules to use. Note that these rules are

          very basic. You are strongly encouraged to use a more sophisticated

          firewall setup; however, these will provide basic security. If you

          are running any servers on your machine, you must choose 'NONE' and

          set up firewalling yourself. Otherwise, the firewall rules will deny

          access to all standard servers like Web, e-mail, ftp, etc. If you

          are using SSH, the rules will block outgoing SSH connections which

          allocate a privileged source port.

           

          The firewall choices are:

          0 - NONE: This script will not set any firewall rules. You are responsible

          for ensuring the security of your machine. You are STRONGLY

          recommended to use some kind of firewall rules.

          1 - STANDALONE: Appropriate for a basic stand-alone web-surfing workstation

          2 - MASQUERADE: Appropriate for a machine acting as an Internet gateway

          for a LAN

          >>> Choose a type of firewall (0-2):這里添寫為2

           

          ** Summary of what you entered **

           

          Ethernet Interface: eth0

          User name: XXX

          Activate-on-demand: No

          DNS: Do not adjust

          Firewalling: MASQUERADE

           

          >>> Accept these settings and adjust configuration files (y/n)?

           

          弄完后,就按一個y鍵。

          (以上為北南兄文章里面內容)

          不要急于連接,REBOOT -N

          然后進入網絡設置,停止ETH1(我的是用他)

          然后ADSL-START

          PING 你的DNS,如果可以,那么,恭喜你!

          其中部分內容可能不同,僅作參考,主要在連接后,能夠PINGDNS即可!

           

           

           

          ==========================================================================

          首先應該確定您是否安裝了pppoe的應用程序。

           

            如果確實已經安裝了,可以在終端用 adslsetup命令啟動adsl配置,提示過程為英文。

           

            大概為:

           

          [root@localhost root]# adsl-setup

          Welcome to the ADSL client setup. First, I will run some checks on

          your system to make sure the PPPoE client is installed properly...

           

          The following DSL config was found on your system:

           

          Device: Name:

          ppp0 DSLppp0

           

          Please enter the device if you want to configure the present DSL config

          (default ppp0) or enter 'n' if you want to create a new one: ppp0 //默認為ppp0

           

          LOGIN NAME

           

          Enter your Login Name (default SJ00411210A1): anthrax //這里用你自己的用戶名代替我的anthrax:)

           

          INTERFACE

           

          Enter the Ethernet interface connected to the ADSL modem

          For Solaris, this is likely to be something like /dev/hme0.

          For Linux, it will be ethX, where 'X' is a number.

          (default eth0): eth0 //默認網卡設備為eth0

           

          Do you want the link to come up on demand, or stay up continuously?

          If you want it to come up on demand, enter the idle time in seconds

          after which the link should be dropped. If you want the link to

          stay up permanently, enter 'no' (two letters, lower-case.)

          NOTE: Demand-activated links do not interact well with dynamic IP

          addresses. You may have some problems with demand-activated links.

          Enter the demand value (default no): no //這里使用默認no就可以了,斷線后不自動撥號。

           

           

          DNS

           

          Please enter the IP address of your ISP's primary DNS server.

          If your ISP claims that 'the server will provide dynamic DNS addresses',

          enter 'server' (all lower-case) here.

          If you just press enter, I will assume you know what you are

          doing and not modify your DNS setup.

          Enter the DNS information here: 202.96.134.133 //DNS地址設置,根據您的具體情況替換。

          Please enter the IP address of your ISP's secondary DNS server.

          If you just press enter, I will assume there is only one DNS server.

          Enter the secondary DNS server address here: 202.96.134.133 //第二DNS地址設置。

           

          PASSWORD

           

          Please enter your Password: //這里設置密碼,和unix規則一樣,密碼并不回顯,因此不要認為您的鍵盤出了毛病:)

          Please re-enter your Password:

          //確認密碼

          USERCTRL

           

          Please enter 'yes' (two letters, lower-case.) if you want to allow

          normal user to start or stop DSL connection (default yes): yes //是否允許普通用戶共享ADSL

           

          FIREWALLING

           

          Please choose the firewall rules to use. Note that these rules are

          very basic. You are strongly encouraged to use a more sophisticated

          firewall setup; however, these will provide basic security. If you

          are running any servers on your machine, you must choose 'NONE' and

          set up firewalling yourself. Otherwise, the firewall rules will deny

          access to all standard servers like Web, e-mail, ftp, etc. If you

          are using SSH, the rules will block outgoing SSH connections which

          allocate a privileged source port.

           

          The firewall choices are:

          0 - NONE: This script will not set any firewall rules. You are responsible

          for ensuring the security of your machine. You are STRONGLY

          recommended to use some kind of firewall rules.

          1 - STANDALONE: Appropriate for a basic stand-alone web-surfing workstation

          2 - MASQUERADE: Appropriate for a machine acting as an Internet gateway

          for a LAN

          Choose a type of firewall (0-2): 1 //配置防火墻等級,根據您的需要選擇。

           

          Start this connection at boot time

           

          Do you want to start this connection at boot time?

          Please enter no or yes (default no):no //是否允許開機自動加載,這里選擇no,否則系統啟動速度太慢!

           

          ** Summary of what you entered **

           

          Ethernet Interface: eth0

          User name: anthrax

          Activate-on-demand: No

          Primary DNS: 202.96.134.133

          Secondary DNS: 202.96.134.133

          Firewalling: STANDALONE

          User Control: yes

          Accept these settings and adjust configuration files (y/n)?

           

           

            選擇y,配置完成。您可以用 adslstart命令啟動,可以用adslstop命令停止。

           

            為了方便,可以在桌面建立一個應用程序鏈接,命令就使用adslstart。這樣每次雙擊那個快捷圖標就可以建立adsl鏈接了,跟windows中一樣方便。

           

          好了,現在就開始您的網絡之旅吧。(技巧:如果依據本內容操作扔不能鏈接網絡,可以嘗試在"系統設置->網路"中刪除當前的網卡,重新配置adsl項。)


          posted @ 2006-03-16 15:31 西部村里人 閱讀(382) | 評論 (0)編輯 收藏

          Eclipse CVS 在局域網中與防火墻共用

              在打開windows網絡防火墻的情況下,cvs的驗證過程非常慢,幾乎難以忍受。關閉防火墻雖然比較快捷,但計算機的安全性受到考驗。
              方法1、使用天網等防火墻產品,允許所有低端端口,允許局域網訪問所有端口。注:此時它關閉了Windows的防火墻,使用自己提供的功能。
              方法2、Windows配置,打開113,2401在局域網范圍內的TCP端口。

          posted @ 2006-03-16 15:11 西部村里人 閱讀(1185) | 評論 (0)編輯 收藏

          2006年3月12日 #

          補充:(Hibernate XDoclet 在Eclipse中的模版隨筆)

          原文:Hibernate XDoclet 在Eclipse中的模版
          補充:Hibernate對象關聯--UML基礎知識、XDoclet---- 5 XDoclet Template In Eclipse
                (系列內容參看:瀏覽)

          posted @ 2006-03-12 15:38 西部村里人 閱讀(1463) | 評論 (0)編輯 收藏

          一些Java技術網站--四處收集--沒有分類

          http://www.javaalmanac.com - Java開發者年鑒一書的在線版本. 要想快速查到某種Java技巧的用法及示例代碼, 這是一個不錯的去處.
          http://www.onjava.com - O'Reilly的Java網站. 每周都有新文章.
          http://java.sun.com - 官方的Java開發者網站 - 每周都有新文章發表.
          http://www.developer.com/java - 由Gamelan.com 維護的Java技術文章網站.
          http://www.java.net - Sun公司維護的一個Java社區網站.
          http://www.builder.com - Cnet的Builder.com網站 - 所有的技術文章, 以Java為主.
          http://www.ibm.com/developerworks/java - IBM的Developerworks技術網站; 這是其中的Java技術主頁.
          http://www.javaworld.com - 最早的一個Java站點. 每周更新Java技術文章.
          http://www.devx.com/java - DevX維護的一個Java技術文章網站.
          http://www.fawcette.com/javapro - JavaPro在線雜志網站.
          http://www.sys-con.com/java - Java Developers Journal的在線雜志網站.
          http://www.javadesktop.org - 位于Java.net的一個Java桌面技術社區網站.
          http://www.theserverside.com - 這是一個討論所有Java服務器端技術的網站.
          http://www.jars.com - 提供Java評論服務. 包括各種framework和應用程序.
          http://www.jguru.com - 一個非常棒的采用Q&A形式的Java技術資源社區.
          http://www.javaranch.com - 一個論壇,得到Java問題答案的地方,初學者的好去處。
          http://www.ibiblio.org/javafaq/javafaq.html - comp.lang.java的FAQ站點 - 收集了來自comp.lang.java新聞組的問題和答案的分類目錄.
          http://java.sun.com/docs/books/tutorial/ - 來自SUN公司的官方Java指南 - 對于了解幾乎所有的java技術特性非常有幫助.
          http://www.javablogs.com - 互聯網上最活躍的一個Java Blog網站.
          http://java.about.com/ - 來自About.com的Java新聞和技術文章網站.
          http://www.codechina.net 提供大量的java源代碼及教程。

          posted @ 2006-03-12 14:34 西部村里人 閱讀(283) | 評論 (0)編輯 收藏

          2006年3月10日 #

          Hibernate XDoclet 在Eclipse中的模版

          開發Hibernate很久,最終還是XDoclet提供的幫助最大。能夠處理復雜的對象關系,也最符合面向對象的原則。
          方法1:
          OO類圖--〉數據庫設計--〉MiddleGen (能夠處理基本的關聯關系--打開XDoclet標簽生成開關,但不能處理繼承概念,較為遺憾ing)-->在Eclipse手工更新Java類中 的XDoclet標簽,然后XDoclet生成Hbm文件。當然了再寫個JUnit測試一下關聯關系是否正確,必要的Lazy是否標注。

          下載Template: http://raibledesigns.com/wiki/Wiki.jsp?page=XDocletEclipse#hibcolidx 非常感謝MattRaible.

          在中文環境中,window xp的字符切換鍵與Eclipse模版的字符快捷鍵重合,需要修改之。為了避免麻煩,直接修改Template文件,簡單添加@標示符:全文如下:
          <?xml version="1.0" encoding="UTF-8"?>
          <templates>
              <template name="@hibarray" description="@hibernate.array" context="javadoc" enabled="true">@hibernate.array table=&quot;&quot; cascade=&quot;save-update&quot;</template>
              <template name="@hibbag" description="@hibernate.bag" context="javadoc" enabled="true">@hibernate.bag table=&quot;&quot; lazy=&quot;false&quot; cascade=&quot;none&quot; inverse=&quot;false&quot;</template>
              <template name="@hibclass" description="@hibernate.class" context="javadoc" enabled="true">@hibernate.class table=&quot;${enclosing_type}&quot;</template>
              <template name="@hibcolelm" description="@hibernate.collection-element" context="javadoc" enabled="true">@hibernate.collection-element column=&quot;&quot; type=&quot;&quot; length=&quot;&quot;</template>
              <template name="@hibcolidx" description="@hibernate.collection-index" context="javadoc" enabled="true">@hibernate.collection-index column=&quot;&quot; type=&quot;&quot; length=&quot;&quot;</template>
              <template name="@hibcolkey" description="@hibernate.collection-key" context="javadoc" enabled="true">@hibernate.collection-key column=&quot;&quot; generator-class=&quot;native&quot;</template>
              <template name="@hibcolmtm" description="@hibernate.many-to-many" context="javadoc" enabled="true">@hibernate.set name=&quot;${enclosing_method}&quot; table=&quot;link_table_name_here&quot; cascade=&quot;save-update&quot; inverse=&quot;true|false&quot; lazy=&quot;true&quot;
               * @hibernate.collection-key column=&quot;${enclosing_type}_ID&quot;
               * @hibernate.collection-many-to-many class=&quot;relationship_class_the_set_contains&quot; column=&quot;relationship_foreign_key&quot;
               * @return ${return_type}</template>
              <template name="@hibcolotm" description="@hibernate.one-to-many relationship" context="javadoc" enabled="true">@hibernate.set name=&quot;${enclosing_method}&quot; table=&quot;relationship_table&quot;
               *                     sort=&quot;comparator_class&quot; inverse=&quot;true|false&quot;
               *                     cascade=&quot;save-update&quot; lazy=&quot;true&quot;
               * @hibernate.collection-key column=&quot;${enclosing_type}_ID&quot;
               * @hibernate.collection-one-to-many class=&quot;relationship_class&quot;
               *
               * @return ${return_type}</template>
              <template name="@hibcomelm" description="@hibernate.collection-composite-element" context="javadoc" enabled="true">@hibernate.collection-composite-element class=&quot;&quot;</template>
              <template name="@hibcomp" description="@hibernate.component" context="javadoc" enabled="true">@hibernate.component class=&quot;component_class_name&quot;</template>
              <template name="@hibdisc" description="@hibernate.discriminator" context="javadoc" enabled="true">@hibernate.discriminator column=&quot;subclass&quot; type=&quot;character&quot;</template>
              <template name="@hibid" description="@hibernate.id" context="javadoc" enabled="true">Note: unsaved-value An identifier property value that indicates that an instance
               * is newly instantiated (unsaved), distinguishing it from transient instances that
               * were saved or loaded in a previous session.  If not specified you will get an exception like this:
               * another object associated with the session has the same identifier
               *
               * @hibernate.id generator-class=&quot;&quot; type=&quot;${return_type}&quot; column=&quot;${enclosing_type}_ID&quot;
               * unsaved-value=&quot;null&quot; length=&quot;&quot;
               * @return ${return_type}</template>
              <template name="@hiblist" description="@hibernate.list" context="javadoc" enabled="true">@hibernate.list table=&quot;relationship-table&quot; lazy=&quot;false&quot; cascade=&quot;none&quot;</template>
              <template name="@hibmap" description="@hibernate.map" context="javadoc" enabled="true">@hibernate.map name=&quot;${enclosing_method}&quot; table=&quot;relationship-table&quot; lazy=&quot;false&quot; cascade=&quot;none&quot;</template>
              <template name="@hibmto" description="@hibernate.many-to-one" context="javadoc" enabled="true">@hibernate.many-to-one column=&quot;${return_type}_ID&quot; class=&quot;package.${return_type}&quot;
               *
               * @return ${return_type}
               *</template>
              <template name="@hiboto" description="@hibernate.one-to-one" context="javadoc" enabled="true">hibernate.one-to-one cascade=&quot;none&quot; class=&quot;&quot; outer-join=&quot;auto&quot;</template>
              <template name="@hibprimarr" description="@hibernate.primitive-array" context="javadoc" enabled="true">@hibernate.primitive-array table=&quot;&quot; cascade=&quot;none&quot;</template>
              <template name="@hibprop" description="@hibernate.property" context="javadoc" enabled="true">@hibernate.property name=&quot;${enclosing_method}&quot; column=&quot;${enclosing_method}&quot; type=&quot;${return_type}&quot; not-null=&quot;false&quot; unique=&quot;false&quot;
               *
               * @return ${return_type}</template>
              <template name="@hibquery" description="@hibernate.query" context="javadoc" enabled="true">@hibernate.query name=&quot;&quot; query=&quot;&quot;</template>
              <template name="@hibset" description="@hibernate.set" context="javadoc" enabled="true">@hibernate.set name=&quot;${enclosing_method}&quot; table=&quot;relationship_table&quot;
               *                     sort=&quot;comparator_class&quot; inverse=&quot;true&quot;
               *                     cascade=&quot;save-update&quot; lazy=&quot;true&quot;</template>
              <template name="@hibsubc" description="@hibernate.subclass" context="javadoc" enabled="true">@hibernate.subclass name=&quot;&quot; discriminator-value=&quot;&quot;</template>
              <template name="@hibts" description="@hibernate.timestamp" context="javadoc" enabled="true">@hibernate.timestamp column=&quot;${enclosing_method}&quot;
               *
               * @return ${return_type}</template>
              <template name="@hibver" description="@hibernate.version" context="javadoc" enabled="true">@hibernate.version column=&quot;${enclosing_method}&quot;
               *
               * @return ${return_type}</template>
          </templates>

          使用時:先把XML內容單獨保存為文件,然后在Eclipse-->Windows-->Preferences
                                               在Preferences-->Java-->Editor-->Templates 點擊Import按鈕導入之前已經保存的XML文件。

          方法2:
          OO類圖--〉在Eclipse手工編寫屬性--〉生成Get/Set方法--〉更新Java類中 的XDoclet標簽,然后XDoclet生成Hbm文件。當然了再寫個JUnit測試一下關聯關系是否正確,必要的Lazy是否標注。
          要求先修改Get方法的模板:源代碼編輯器中鼠標右鍵--〉Source--〉Generate Getters And Setters..
          bb.PNG
                                
                         點擊打開面板中Code Template鏈接。
          aa1.PNG

          編輯Getter方法模板:
          /**
           * @hibernate.property name="${bare_field_name}" column="${field}" type="${field_type}" not-null="false" unique="false" length="128"
           * @return Returns the ${bare_field_name}.
           */
          然后生成代碼,手工微調部分屬性。也能夠節約大量時間。

          注意:在編寫Java POJO類時,java屬性用完整的帶包名的類,例如:
          /**
          * @author jdyao
           * @hibernate.class table="respri"
           * @version
           */
          public class Resource implements Serializable {

              private static final long serialVersionUID = 1505581058179605003L;

              private java.lang.String guid;

              private java.lang.String context;

           

              public
          Resource () {

              }

              /**
               * @return java.lang.String
               * @hibernate.property name="context" type="java.lang.String"
               *                     length="128"
               *
               */
              public java.lang.String getContext() {
                  return context;
              }

              public void setContext(java.lang.String context) {
                  this.context = context;
              }

              /**
               * @return java.lang.String
               * @hibernate.id generator-class="guid" type="java.lang.String" column="guid"
               *               unsaved-value="null" length="38"
               */
              public java.lang.String getGuid() {
                  return guid;
              }

              public void setGuid(java.lang.String guid) {
                  this.guid = guid;
              }

          }
          原因:XDoclet在生成的時候,如果type="string",有時會出現錯誤,無法生成Hbm文件,為了避免這個不必要的錯誤,務必要寫全類名。

          XDoclet build.xml文件:

          <?xml version="1.0" encoding="ISO-8859-1"?>

          <project name="XDoclet Examples" default="hibernate" basedir=".">
              <property name="xdoclet.root.dir" value="${basedir}"/>
              <property file="${xdoclet.root.dir}/build.properties"/>

              <!-- Include the build-dist properties. Since properties are immutable,
              this will not override available properties. You do not have to include
              this in your own build file. -->
              <property file="build-dist.properties"/>

              <!-- See CustomerBean. This is to demonstrate property substitution. -->
              <property name="ejb.prefix" value="blah"/>

              <!-- =================================================================== -->
              <!-- Define the class path                                               -->
              <!-- =================================================================== -->
              <path id="samples.class.path">
                  <fileset dir="${lib.dir}">
                      <include name="*.jar"/>
                  </fileset>
                  <fileset dir="${samples.lib.dir}">
                      <include name="*.jar"/>
                  </fileset>
                  <fileset dir="${dist.lib.dir}">
                      <include name="*.jar"/>
                  </fileset>
              </path>

              <!-- =================================================================== -->
              <!-- Initialise                                                          -->
              <!-- =================================================================== -->
              <target name="init">
                  <tstamp>
                      <format property="TODAY" pattern="d-MM-yy"/>
                  </tstamp>
                  <taskdef
                      name="xdoclet"
                      classname="xdoclet.DocletTask"
                      classpathref="samples.class.path"
                      />
                   <taskdef
                      name="hibernatedoclet"
                      classname="xdoclet.modules.hibernate.HibernateDocletTask"
                      classpathref="samples.class.path"
                      />
              </target>


              <!-- =================================================================== -->
              <!-- Prepares the directory structure                                    -->
              <!-- =================================================================== -->
              <target name="prepare" depends="init">
                  <mkdir dir="${samples.classes.dir}"/>
                  <mkdir dir="${samples.gen-src.dir}"/>
                  <mkdir dir="${samples.meta-inf.dir}"/>
              </target>



              <!-- =================================================================== -->
              <!-- Invoke XDoclet's hibernate                                          -->
              <!-- =================================================================== -->
              <target name="hibernate" depends="prepare" description="Generate mapping documents (run jar first)">

                  <echo>+---------------------------------------------------+</echo>
                  <echo>|                                                   |</echo>
                  <echo>| R U N N I N G   H I B E R N A T E D O C L E T     |</echo>
                  <echo>|                                                   |</echo>
                  <echo>+---------------------------------------------------+</echo>

                  <hibernatedoclet
                      destdir="${samples.gen-src.dir}"
                      mergedir="${samples.src.dir}"
                      excludedtags="@version,@author,@todo,@see"
                      addedtags="@xdoclet-generated at ${TODAY},@copyright The XDoclet Team,@author XDoclet,@version ${version}"
                      force="${samples.xdoclet.force}"
                      verbose="false">

                      <fileset dir="${samples.java.dir}">
                          <include name="**/**/*.java"/>
                      </fileset>

                      <hibernate version="3.0"/>

                  </hibernatedoclet>
              </target>

              <!-- =================================================================== -->
              <!-- Clean                                                               -->
              <!-- =================================================================== -->
              <target name="clean">
                  <delete dir="${samples.dist.dir}"/>
              </target>

          </project>
          build-dist.properties 文件:
          # These properties are only used when building the samples expanded from the distribution.

          lib.dir = ${xdoclet.root.dir}/lib
          dist.lib.dir = ${lib.dir}

          samples.dir = ${xdoclet.root.dir}
          samples.dist.dir = ${samples.dir}/target
          samples.lib.dir = ${samples.dir}/lib
          samples.src.dir = ${samples.dir}/src
          samples.java.dir = ${samples.src.dir}/java
          samples.gen-src.dir = ${samples.dist.dir}/gen-src

          samples.meta-inf.dir = ${samples.dist.dir}/meta-inf
          samples.web-inf.dir = ${samples.dist.dir}/web-inf
          samples.merge.dir = ${samples.src.dir}/merge
          samples.classes.dir = ${samples.dist.dir}/classes
          samples.web.dir = ${samples.src.dir}/web
          samples.xdoclet.force = false

          工程目錄結構:從XDoclet網站下載該包,解壓縮后,把Example目錄單獨copy出來,把這2個文件放在Example目錄下,同時建立lib目錄,把XDoclet目錄下--〉lib目錄下的*.jar拷貝到Example新建立的lib目錄下。

          posted @ 2006-03-10 00:28 西部村里人 閱讀(1451) | 評論 (0)編輯 收藏

          僅列出標題  
          主站蜘蛛池模板: 三穗县| 周宁县| 太白县| 固始县| 满城县| 祥云县| 金塔县| 叙永县| 靖西县| 彰武县| 玛沁县| 怀来县| 庄河市| 青州市| 金坛市| 施秉县| 瓦房店市| 新平| 滦南县| 鄂伦春自治旗| 远安县| 略阳县| 崇信县| 昆明市| 八宿县| 霍林郭勒市| 色达县| 志丹县| 四子王旗| 晴隆县| 赣榆县| 揭阳市| 家居| 长治市| 石嘴山市| 绍兴县| 永定县| 星座| 广饶县| 名山县| 土默特左旗|