选用纯 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>
<?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>
<%@ 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>
<%@ 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>
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.
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.messageresources;
import org.apache.commons.beanutils.propertyutils;
public final class logonaction extends action {
public actionforward execute(
actionmapping mapping,
actionform form,
httpservletrequest request,
httpservletresponse response)
throws exception {
locale locale = getlocale(request);
messageresources messages = getresources(request);
// validate the request parameters specified by the user
actionerrors errors = new actionerrors();
string username =
(string) propertyutils.getsimpleproperty(form, "username");
string password =
(string) propertyutils.getsimpleproperty(form, "password");
if ((username != "foo") (password != "bar"))
errors.add(actionerrors.global_error,
new actionerror("error.password.mismatch"));
// report any errors we have discovered back to the original form
if (!errors.isempty()) {
saveerrors(request, errors);
return (mapping.getinputforward());
}
// save our logged-in user in the session
httpsession session = request.getsession();
// do something with session...
// remove the obsolete form bean
if (mapping.getattribute() != null) {
if ("request".equals(mapping.getscope()))
request.removeattribute(mapping.getattribute());
else
session.removeattribute(mapping.getattribute());
}
// forward control to the specified success uri
return (mapping.findforward("success"));
}
}
logoffaction.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.actionform;
import org.apache.struts.action.actionforward;
import org.apache.struts.action.actionmapping;
import org.apache.struts.util.messageresources;
public final class logoffaction extends action {
public actionforward execute(
actionmapping mapping,
actionform form,
httpservletrequest request,
httpservletresponse response)
throws exception {
locale locale = getlocale(request);
messageresources messages = getresources(request);
httpsession session = request.getsession();
session.removeattribute("userattrib");
session.invalidate();
// forward control to the specified success uri
return (mapping.findforward("success"));
}
}
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器