选择显示字体大小

struts初体验之实施篇

开发环境 window2000+jbuilder7+tomcat4.1.12

   首先,从http://jakarta.apache.org下载jakarta-struts-1.0.2.zip,解开后有两个主要目录lib和webapps,在lib目录中是使用struts创建应用程序所必需的文件,在webapps目录下有几个后缀是war的文件,熟悉java web开发的人都比较清楚,将他们copy到tomcat/ webapps/目录下,然后再重新启动tomcattomcat会将这些web工程部署好的,其中struts-documentation.war是文档,struts-example.war是一个不错的例子,我主要就是参考这个例子来做的。然后在jbuilder安装目录/lib/下建立 struts目录,将上面的lib目录下的东西copy进去,然后启动jbuilder7,运行菜单tools->configure->libraries->new->name  输入struts,然后选择刚才copy过去的struts.jar文件,然后ok。

   其次,新建立一个工程,引用struts服务器选择tomcat4,然后建立一个jsp1.jsp文件,然后把lib/目录下的所有.tld文件copy到你建立的工程目录下的/defaultroot/web-inf/目录下面,在/defaultroot/web-inf/建立action.xml,内容如下:

<action-mappings>

    <forward name=&quot;logon&quot;        path=&quot;/logon.jsp&quot;/>

    <action    path=&quot;/logon&quot;

      actionclass=&quot;example.logonaction&quot;

    formattribute=&quot;logonform&quot;

        formclass=&quot;example.logonform&quot;

        inputform=&quot;/logon.jsp&quot;>

    <forward name=&quot;success&quot;    path=&quot;/jsp1.jsp&quot;/>

  </action>

</action-mappings>

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.0//en&quot;

          &quot;http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd&quot;>

<struts-config>

<form-beans>

<form-bean name=&quot;logonform&quot;type=&quot;example.logonform&quot;/>

    </form-beans>

<global-forwards>

    <forward name=&quot;logon&quot;   path=&quot;/logon.jsp&quot;/>

</global-forwards>

<action-mappings>

<action    path=&quot;/logon&quot;

               type=&quot;example.logonaction&quot;

               name=&quot;logonform&quot;

              scope=&quot;request&quot;

              input=&quot;/logon.jsp&quot;>

       <forward   name=&quot;success&quot;              path=&quot;/jsp1.jsp&quot;/>

    </action>

<action    path=&quot;/admin/addformbean&quot;

type=&quot;org.apache.struts.actions.addformbeanaction&quot;/>

    <action    path=&quot;/admin/addforward&quot;

   type=&quot;org.apache.struts.actions.addforwardaction&quot;/>

    <action    path=&quot;/admin/addmapping&quot;

type=&quot;org.apache.struts.actions.addmappingaction&quot;/>

    <action    path=&quot;/admin/reload&quot;

type=&quot;org.apache.struts.actions.reloadaction&quot;/>

    <action    path=&quot;/admin/removeformbean&quot;

type=&quot;org.apache.struts.actions.removeformbeanaction&quot;/>

    <action    path=&quot;/admin/removeforward&quot;

type=&quot;org.apache.struts.actions.removeforwardaction&quot;/>

    <action    path=&quot;/admin/removemapping&quot;

type=&quot;org.apache.struts.actions.removemappingaction&quot;/>

  </action-mappings>

</struts-config>


然后建立logon.jsp,内容如下:

<%@ page 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;logon.title&quot;/></title>
<html:base/>
</head>
<body bgcolor=&quot;white&quot;>
<html:errors/>
<html:form action=&quot;/logon&quot; focus=&quot;username&quot;>
<table border=&quot;0&quot; width=&quot;100%&quot;>
  <tr>
    <th align=&quot;right&quot;>
      <bean:message key=&quot;prompt.username&quot;/>
    </th>
    <td align=&quot;left&quot;>
      <html:text property=&quot;username&quot; size=&quot;16&quot; maxlength=&quot;16&quot;/>
    </td>
  </tr>
  <tr>
    <th align=&quot;right&quot;>
      <bean:message key=&quot;prompt.password&quot;/>
    </th>
    <td align=&quot;left&quot;>
      <html:password property=&quot;password&quot; size=&quot;16&quot; maxlength=&quot;16&quot; redisplay=&quot;false&quot;/>
    </td>
  </tr>
  <tr>
    <td align=&quot;right&quot;>
      <html:submit property=&quot;submit&quot; value=&quot;submit&quot;/>
    </td>
    <td align=&quot;left&quot;>
      <html:reset/>
    </td>
  </tr>
</table>
</html:form>
</body>
</html:html>


然后建立一个class  logonform.java,继承自actionform,打包在example内容如下

package example;

import org.apache.struts.action.actionform;
import javax.servlet.http.httpservletrequest;
import org.apache.struts.action.actionerror;
import org.apache.struts.action.actionerrors;
import org.apache.struts.action.actionform;
import org.apache.struts.action.actionmapping;

public class logonform extends actionform {

  private string password = null;
  private string username = null;

  public string getpassword() {
    return password;
  }
  public string getusername() {
    return username;
  }
  public void setpassword(string password) {
    this.password = password;
  }
  public void setusername(string username) {
    this.username = username;
  }

  public void reset(actionmapping mapping, httpservletrequest request) {

        this.password = null;
        this.username = null;

    }

  public actionerrors validate(actionmapping mapping,
                               httpservletrequest request) {

      actionerrors errors = new actionerrors();
      if ((username == null) (username.length() < 1))
          errors.add(&quot;username&quot;, new actionerror(&quot;error.username.required&quot;));
      if ((password == null) (password.length() < 1))
          errors.add(&quot;password&quot;, new actionerror(&quot;error.password.required&quot;));

      return errors;

    }
}


然后建立一个class  logonaction.java,继承自action,打包在example内容如下

package example;

import java.io.ioexception;
import java.util.hashtable;
import java.util.locale;
import javax.servlet.requestdispatcher;
import javax.servlet.servletexception;
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.action.actionservlet;
import org.apache.struts.util.messageresources;

public class logonaction extends action {

  public actionforward perform(actionmapping mapping,
                             actionform form,
                             httpservletrequest request,
                             httpservletresponse response)
throws ioexception, servletexception {

        locale locale = getlocale(request);
//messageresources messages = getresources();
        actionerrors errors = new actionerrors();
        string username = ((logonform) form).getusername();
string password = ((logonform) form).getpassword();

        // report any errors we have discovered back to the original form
        if (!errors.empty()) {
            saveerrors(request, errors);
            return (new actionforward(mapping.getinput()));
        }

        // save our logged-in user in the session
        httpsession session = request.getsession();
        session.setattribute(&quot;user&quot;, &quot;ok&quot;);
        if (servlet.getdebug() >= 1)
            servlet.log(&quot;logonaction: user :&quot;  +
                        &quot;' logged on in session &quot; + session.getid());

        // 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;));


  }
}


然后修改/defaultroot/web-inf/web.xml文件为

<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
<!doctype web-app public &quot;-//sun microsystems, inc.//dtd web application 2.3//en&quot; &quot;http://java.sun.com/dtd/web-app_2_3.dtd&quot;>
<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>
    <init-param>
      <param-name>application</param-name>
      <param-value>example.applicationresources</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>2</param-value>
    </init-param>
    <init-param>
      <param-name>validate</param-name>
      <param-value>true</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet>
    <servlet-name>debugjsp</servlet-name>
    <servlet-class>org.apache.jasper.servlet.jspservlet</servlet-class>
    <init-param>
      <param-name>jspcompilerplugin</param-name>
      <param-value>com.borland.jbuilder.webserverglue.tomcat.jsp.jaspersunjavacompiler</param-value>
    </init-param>
    <load-on-startup>3</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>debugjsp</servlet-name>
    <url-pattern>*.jsp</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>logon.jsp</welcome-file>
  </welcome-file-list>
  <taglib>
    <taglib-uri>/web-inf/app.tld</taglib-uri>
    <taglib-location>/web-inf/app.tld</taglib-location>
  </taglib>
  <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-example.war,安装后把\struts-example\web-inf\src\org\apache\struts\webapp\applicationresources.properties放到/defaultroot/web-inf/classes/example/目录下,然后就可以运行了,当你运行jbuilder后,在ie上键入http://localhost:8080/就会看到效果了,具体的解释请参见下一 解释篇

刚开始研究struts,所以把自己的经过给大家分享,欢迎大家批评指正



bill-转自:csdn


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

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