終於把自己的這個測試做完了. 希望對想學struts的新手有所幫助.
(mystruts應用)目錄結構
mystruts
|-Web-INF
| |- lib
| | |- struts.jar
| |- classes
| | |- ConnectionPool
| | | |- getDbConnection.class
| | |-mystruts
| | |- user1Action.class
| | |- userAction.class
| | |- userActionForm.class
| | |- userdao.class
| |-struts-bean.tld
| |-struts-config.XML
| |-struts-html.tld
| |-struts-logic.tld
| |-struts-template.tld
|-adduser.JSP
|-index.jsp
|-viewuser.jsp
第一步: 開發環境配置
(如果你還沒有相關軟件. 請到http://www.apache.org 下載)
tomcat5.0 配置 (建立一個 mystruts 應用, 並拷貝相關文件)
請在你的tomcat安裝目錄中找到\conf\Catalina\localhost 子目錄. 新建 mystruts.xml 文件
文件內容如下. 在這個文件中. 我們將配置一個聯接池.請把相關的驅動.數據庫用戶,密碼改成你自己的本機配置. 並把JDBC驅動拷貝一份到 Tomcat 5.0\common\lib 目錄下. 請把 struts.jar \mystruts\WEB-INF\lib 目錄下.
<Context path="/mystruts" docBase="D:\www\mystruts\mystruts\mystruts" debug="0" privileged="true">
<Resource name="jdbc/TestDB"
auth="Container"
type="Javax.sql.DataSource"/>
<ResourceParams name="jdbc/TestDB">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>10</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>5</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<parameter>
<name>username</name>
<value>sa</value>
</parameter>
<parameter>
<name>password</name>
<value>test</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>net.sourceforge.jtds.jdbc.Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:jtds:sqlserver://192.168.10.223:1433/mystruts</value>
</parameter>
</ResourceParams>
</Context>
WEB-INF/web.xml (加入struts標記庫的支持)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<Servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-template.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-template.tld</taglib-location>
</taglib>
</web-app>