简单的struts应用开发(1)
终于把自己的这个测试做完了. 希望对想学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>(未完待续)