选择显示字体大小

实例学习 struts

选用纯 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=&quot;1.0&quot;?>
<!doctype web-app
  public &quot;-//sun microsystems, inc.//dtd web application 2.3//en&quot;
  &quot;http://java.sun.com/j2ee/dtds/web-app_2_3.dtd&quot;>

<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=&quot;1.0&quot; encoding=&quot;iso-8859-1&quot; ?>
<!doctype struts-config public
          &quot;-//apache software foundation//dtd struts configuration 1.1//en&quot;
          &quot;http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd&quot;>

<struts-config>
  <form-beans>
    <form-bean name=&quot;logonform&quot;
      type=&quot;org.apache.struts.validator.dynavalidatorform&quot;>
      <form-property name=&quot;username&quot; type=&quot;java.lang.string&quot;/>
      <form-property name=&quot;password&quot; type=&quot;java.lang.string&quot;/>
    </form-bean>
  </form-beans>

  <global-forwards>
    <forward   name=&quot;success&quot;              path=&quot;/main.jsp&quot;/>
    <forward   name=&quot;logoff&quot;               path=&quot;/logoff.do&quot;/>
  </global-forwards>

  <action-mappings>
    <action    path=&quot;/logon&quot;
               type=&quot;org.monotonous.struts.logonaction&quot;
               name=&quot;logonform&quot;
               scope=&quot;session&quot;
               input=&quot;logon&quot;>
    </action>

    <action    path=&quot;/logoff&quot;
               type=&quot;org.monotonous.struts.logoffaction&quot;>
      <forward name=&quot;success&quot;              path=&quot;/index.jsp&quot;/>
    </action>
  </action-mappings>

  <controller>
    <!-- the &quot;input&quot; parameter on &quot;action&quot; elements is the name of a
         local or global &quot;forward&quot; rather than a module-relative path -->
    <set-property property=&quot;inputforward&quot; value=&quot;true&quot;/>
  </controller>

  <message-resources parameter=&quot;org.monotonous.struts.applicationresources&quot;/>
</struts-config>

创建 view
现在回到 eclipse,建立一个新页面 index.jsp

<%@ page contenttype=&quot;text/html;charset=utf-8&quot; language=&quot;java&quot; %>
<%@ taglib uri=&quot;/web-inf/struts-bean.tld&quot; prefix=&quot;bean&quot; %>
<%@ taglib uri=&quot;/web-inf/struts-html.tld&quot; prefix=&quot;html&quot; %>

<html:html locale=&quot;true&quot;>
<head>
  <title><bean:message key=&quot;index.title&quot; /></title>
  <html:base/>
</head>

<body>
<html:errors/>
<html:form action=&quot;/logon&quot;>
  <bean:message key=&quot;prompt.username&quot;/>
  <html:text property=&quot;username&quot;/>
  <br/>
  <bean:message key=&quot;prompt.password&quot;/>
  <html:password property=&quot;password&quot;/>
  <br/>
  <html:submit>
    <bean:message key=&quot;index.logon&quot;/>
  </html:submit>
</html:form>
</body>
</html:html>

成功登录后的页面 main.jsp

<%@ page contenttype=&quot;text/html;charset=utf-8&quot; language=&quot;java&quot; %>
<%@ taglib uri=&quot;/web-inf/struts-bean.tld&quot; prefix=&quot;bean&quot; %>
<%@ taglib uri=&quot;/web-inf/struts-html.tld&quot; prefix=&quot;html&quot; %>

<html:html>
<head>
  <title><bean:message key=&quot;main.title&quot;/></title>
  <html:base/>
</head>
<body>
<html:link forward=&quot;logoff&quot;>
<bean:message key=&quot;main.logoff&quot;/>
</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.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, &quot;username&quot;);
        string password =
            (string) propertyutils.getsimpleproperty(form, &quot;password&quot;);

        if ((username != &quot;foo&quot;) (password != &quot;bar&quot;))
            errors.add(actionerrors.global_error,
                new actionerror(&quot;error.password.mismatch&quot;));

        // 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 (&quot;request&quot;.equals(mapping.getscope()))
                request.removeattribute(mapping.getattribute());
            else
                session.removeattribute(mapping.getattribute());
        }

        // forward control to the specified success uri
        return (mapping.findforward(&quot;success&quot;));
    }
}

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(&quot;userattrib&quot;);
        session.invalidate();

        // forward control to the specified success uri
        return (mapping.findforward(&quot;success&quot;));
    }
}

到浏览器里面欣赏一下吧,不过还不到开香槟的时候,也许你应该为这个应用考虑一些安全措施,下一次我再讲咯。


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

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   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons