| |
选用纯 jsp 还是纯 servlet 设计站点都有它的局限性,struts 就是把它们联系在一起的一种有力工具。采用 struts 能开发出基于 mvc 模式的应用,关于 mvc 的概念可以参见 gof 的《设计模式——可复用面向对象软件的基础》。 你现在要做的是,下载、安装、配置好以下的工具,版本不同的话操作可能会有些差异,具体的看它们的文档吧: tomcat 4.1.24 apache 2.0.43, w/ mod_jk2 2.0.43 java 2 sdk standard edition 1.4.0 struts 1.1 eclipse 2.1.0 struts 是用 java 写的,应此它需要 jdk 1.2 或者更高版本。如果你用的是 jdk 1.4,就像我,xml parser 和 jdbc 2.0 optional package binary 就已经被默认的包含了。 新项目 在这个例程中我们要开发一个简单的 web 应用,允许用户登录和注销。简单起见,数据被设定为常数,而不是保存在数据库中,毕竟这里要讲的是 struts,而不是 java。 首先在你的 tomcat 配置的应用主目录中创建一个目录,比方说 logonapp。在 logonapp 中创建目录 src 和 web-inf,在 web-inf 中创建目录 classes 和 lib,从 struts 的分发中拷贝 struts.jar 到 lib 目录,而且也把拷贝 $catalina_home/common/lib/servlets.jar 到 lib 目录。从 struts 的分发中拷贝所有的 struts*.tld 到 web-inf 目录。 现在打开 eclipse,你会看到四个 view。现在我们要建立一个新的项目,点击 file -> new project,打开了一个窗口,在第一个窗格中选择 java,在第二个窗格中选择 java project,点击 next。输入项目名称(为了好记,就也叫 logonapp 吧),去掉 use default 复选框的对勾,浏览到 logonapp 目录,点击 next。出现一个新的窗口,在 source tab 上点击 add folder,添加 $app_base/src,在 default output folder 中填入 $app_base/web-inf/classes,点击 finish。点击 window -> open perspective -> resource,看看 .project 文件是否已经自动包含了 lib 目录中所有的 jar 文件。 你的 logonapp/web-inf/web.xml 应该如下所示: <?xml version="1.0"?> <!doctype web-app public "-//sun microsystems, inc.//dtd web application 2.3//en" "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd";> <web-app> <!-- action servlet configuration --> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.actionservlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/web-inf/struts-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- action servlet mapping --> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- the welcome file list --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- struts tag library descriptors --> <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> </web-app> struts 的配置文件 logonapp/web-inf/struts-config.xml 如下: <?xml version="1.0" encoding="iso-8859-1" ?> <!doctype struts-config public "-//apache software foundation//dtd struts configuration 1.1//en" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd";> <struts-config> <form-beans> <form-bean name="logonform" type="org.apache.struts.validator.dynavalidatorform"> <form-property name="username" type="java.lang.string"/> <form-property name="password" type="java.lang.string"/> </form-bean> </form-beans> <global-forwards> <forward name="success" path="/main.jsp"/> <forward name="logoff" path="/logoff.do"/> </global-forwards> <action-mappings> <action path="/logon" type="org.monotonous.struts.logonaction" name="logonform" scope="session" input="logon"> </action> <action path="/logoff" type="org.monotonous.struts.logoffaction"> <forward name="success" path="/index.jsp"/> </action> </action-mappings> <controller> <!-- the "input" parameter on "action" elements is the name of a local or global "forward" rather than a module-relative path --> <set-property property="inputforward" value="true"/> </controller> <message-resources parameter="org.monotonous.struts.applicationresources"/> </struts-config> 创建 view 现在回到 eclipse,建立一个新页面 index.jsp: <%@ page contenttype="text/html;charset=utf-8" language="java" %> <%@ taglib uri="/web-inf/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/web-inf/struts-html.tld" prefix="html" %> <html:html locale="true"> <head> <title><bean:message key="index.title" /></title> <html:base/> </head> <body> <html:errors/> <html:form action="/logon"> <bean:message key="prompt.username"/> <html:text property="username"/> <br/> <bean:message key="prompt.password"/> <html:password property="password"/> <br/> <html:submit> <bean:message key="index.logon"/> </html:submit> </html:form> </body> </html:html> 成功登录后的页面 main.jsp: <%@ page contenttype="text/html;charset=utf-8" language="java" %> <%@ taglib uri="/web-inf/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/web-inf/struts-html.tld" prefix="html" %> <html:html> <head> <title><bean:message key="main.title"/></title> <html:base/> </head> <body> <html:link forward="logoff"> <bean:message key="main.logoff"/> </html:link> </body> </html:html> 你可能注意到这两个页面中都使用了方便国际化的特性,这至少需要一个默认的属性文件 applicationresources.properties: index.title=struts homepage prompt.username=username prompt.password=password index.logon=log on main.title=struts main page main.logoff=log off error.password.mismatch=invalid username and/or password. 创建 controller logonaction.java: package org.monotonous.struts; import java.util.locale; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpsession; import javax.servlet.http.httpservletresponse; import org.apache.struts.action.action; import org.apache.struts.action.actionerror; import org.apache.struts.action.actionerrors; import org.apache.struts.action.actionform; import org.apache.struts.action.actionforward; import org.apache.struts.action.actionmapping; import org.apache.struts.util.messagere
|
|