struts串連MySql資料來源的寫法-Struts Tutorials

來源:互聯網
上載者:User
相信許多剛剛接觸struts的菜鳥都會對struts架構的博大精深所敬佩.俺也是大隊伍中的一員,最開始用struts內建的struts-config.xml裡的Data Sources串連MySql時,總是出現問題:錯誤404 action不可用......這樣的問題.由於是剛接觸struts,一遇到這樣的問題腦袋立刻就一片空白,感覺無處下手,於是在網上開始 GOOGLE,BAIDU,搜尋到的東西多多,不過copy來copy去的東西也就多多,這可能也是網上的一大通病吧.剛開始用搜尋來的帶碼做測試,可是無論怎麼,問題還是依舊,這就更加讓自己在一片空白的基礎上更加迷茫了.懷疑自己的瀏覽器有問題,懷疑自己裝的eclipse環境有問題,懷疑搜尋到的例子有問題......
      在網上經常會看見別人說,想要技術得到提高,就要多看些英文文檔,由於自己E文還是有限,所以一遇到E文方面的東西首先自己先打退堂鼓了,因此也錯失了許多.再無助的情況下,決心狠吃E文文章,也就開始了自己蹩腳的E文之旅.
      "吃"E文已經有一陣子時間了,現在看來,雖然好象E文的水平並沒有多大提高,但是在看E文過程中感覺,好的文檔,好的網站,講解的相當清晰,文法相當簡潔,除了個別單詞需要翻譯,意思一看就懂,天啊!爽:)
     我的資料來源的解決就是從E文網站中得到的協助,不說了,希望兄弟姐妹們有空多看看原版文檔(E文居多),不會??硬著頭皮也要看......:)

    在TOMCAT5上配置STRUTS資料來源:
1:下載並安裝Tomcat5.5.9    http://jakarta.apache.org/tomcat/http://struts.apache.org/download.cgi
3:下載MySql的JDBC驅動   mysql-connector-java-3.0.16-ga-bin.jar  (google一下)
4:建立MySql資料庫名字:strutsdatabase
5:建立MySql資料庫表:
      CREATE TABLE `test` (
        `username` varchar(20) NOT NULL default ''
         ) TYPE=MyISAM;
         /*Data for the table `test` */
         insert into `test` values ('rajesh'),('George'),('Vikas'),('Prakash'),('Mahesh');
6:在struts-config.xml的data-source中的元素應該是這樣:

<data-sources>
<data-source type="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<set-property
property="driverClassName"
value="com.mysql.jdbc.Driver" />
<set-property
property="url"
value="jdbc:mysql://localhost:3306/strutsdatabase?autoReconnect=true" />
<set-property
property="username"
value="root" />
<set-property
property="password"
value="" />
<set-property
property="maxActive"
value="10" />
<set-property
property="maxWait"
value="5000" />
<set-property
property="defaultAutoCommit"
value="false" />
<set-property
property="defaultReadOnly"
value="false" />
<set-property
property="validationQuery"
value="SELECT COUNT(*) FROM test" />
</data-source>
</data-sources>

7:建立action class來測試這個DataSource

package test;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import java.sql.*;

public class TestDataSource extends Action
{
  public ActionForward execute(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response) throws Exception{

       javax.sql.DataSource dataSource;
       java.sql.Connection myConnection=null;
       try {
        dataSource = getDataSource(request);
        myConnection = dataSource.getConnection();
        Statement stmt=myConnection.createStatement();
        ResultSet rst=stmt.executeQuery("select username from test");
        System.out.println("******************************************");
        System.out.println("********Out Put from TestDataSource ******");
        while(rst.next()){
        System.out.println("User Name is: " + rst.getString("username"));
        }
        System.out.println("******************************************");
        rst.close();
        stmt.close();
        // do what you wish with myConnection
       } catch (SQLException sqle) {
        getServlet().log("Connection.process", sqle);
       } finally {
        //enclose this in a finally block to make
        //sure the connection is closed
        try {
           myConnection.close();
        } catch (SQLException e) {
           getServlet().log("Connection.close", e);
        }
         }

      return mapping.findForward("success");
  }
}

8:在struts-config.xml裡添加action mapping元素.
<action
      path="/DataSource"
      type="test.TestDataSource">
      <forward name="success" path="/success.jsp"/>
</action>
9:建立一個success.jsp在你的strutsdatabase的根目錄下
   (隨便寫點東西像是:HELLO WORLD!:))
10:測試你的成果:http://localhost:8080/strutsdatabase/DataSource.do
11:看看你的瀏覽器是不是出現了:HELLO WORLD!了.呵呵!

 
2:下載struts1.2.7  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.