程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> Struts 1.1 安裝、示例篇

Struts 1.1 安裝、示例篇

編輯:JAVA編程入門知識

  在前一篇文章中,我們具體的介紹了Struts 1.1 的工作原理,接著將會介紹它的安裝,以及一個具體的應用。
  1. Struts 1.1 安裝
  安裝Struts 1.1 比較簡單。假設我們使用的是Tomcat 5.0,並且安裝在D: omcat下。首先,請到http://jakarta.apache.org/struts/ 下載Struts 1.1,解壓縮後,可以看到jakarta-struts-1.1目錄,在這個目錄下面,你可以看到這樣一個目錄:webapps.該目錄下面有多個*.war,我們copy struts-blank.war 文件到D: omcatwebapps下,restart Tomcat,在Tomcat 的webapps下就會生成一個struts-blank空白Struts開發環境。另外,在你解壓縮的目錄下的webapps中 struts-example.war 是一個很好的struts學習教程,假如你想深入學習請訪問http://jakarta.apache.org/struts 或者 下載 《Struts in Action》 E 文版。
  2. 一個簡單的例子
  2.1 Logon.jsp
  <%@ taglib uri="/WEB-INF/struts-Html.tld" prefix="html" %>
  <%@ page contentType="text/html; charset=gb2312" %>
  <%
  /**
   * This page shows Logon interface.
   * Title: LogoN SYSTEM
   * Description: Logon Page
   * Copyright: Copyright (c) 2004
   * Company: www.ewe.com.cn
   * @author bluesunny <[email protected]>
   * @version 1.0
   */
  response.setHeader("Pragma","No-cache");
  response.setHeader("Cache-Control","no-cache");
  response.setDateHeader("EXPires", 0);
  %>
  <html>
  <head>
  <title>Logon</title>
  </head>
  <body>
  <html:errors/>
  <html:form action="/Logon" focus="username">
  username:<html:text property="username" size="25"/>
  passWord:<html:password property="password" size="25"/>
  <html:submit property=”submit”/>
  </html:form>
  </body>
  </html>
  頁面為用戶登陸頁面,應用到Struts的html標簽。
  2.2 LogonForm.Java
  package com.Logon.app;
  import javax.servlet.http.*;
  import org.apache.struts.action.*;
  /**
  * This page shows LogonForm.java
  * Title: LogoN SYSTEM
  * Description: LogonForm Page
  * Copyright: Copyright (c) 2004
  * Company: www.ewe.com.cn
  * @author bluesunny
  * @version 1.0
  */
  public class LogonForm extends ActionForm{
  private String username;
  private String password;
  public LoginForm() {
  username = null;
  password = null;
  }
  public void setUsername(String username){
  this.username = username;
  }
  public String getUsername() {
  return this.username;
  }
  public void setPassword(String password){
  this.password = password;
  }
  public String getPassword(){
  return this.password;
  }
  public void reset(ActionMapping mapping, HttpServletRequest request)
  {
  username = null;
  password = null;
  }
  }
  這個頁面為ActionForm bean ,主要是用來封裝客戶端的用戶名與密碼。
  2.3 LogonAction.java
  package com.Logon.app;
  import java.io.*;
  import java.sql.*;
  import org.apache.struts.action.Action;
  import org.apache.struts.action.ActionForward;
  import org.apache.struts.action.ActionMapping;
  import org.apache.struts.action.ActionForm;
  import javax.servlet.*;
  import javax.servlet.http.*;
  import org.apache.struts.action.*;
  /**
  * This page shows LogonAction.java
  * Title: LogoN SYSTEM
  * Description: LogonAction Page
  * Copyright: Copyright (c) 2004
  * Company: www.ewe.com.cn
  * @author bluesunny
  * @version 1.0
  */
  public class LogonAction extends Action{
  public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)throws Exception{
  LogonForm form = (LogonForm)actionForm;
  ActionErrors errors = new ActionErrors();
  String username = form.getUsername();
  String password = form.getPassword();
  If(username.equals(“bluesunny”) && password.equals(“bluesunny”)){
  return actionMapping.findForward("sUCcess");
  }else{
  errors.add("login",new ActionError("Logon.failed"));
  saveErrors(request,errors);
  return actionMapping.findForward("failed");
  }
  }
  }
  取得ActionForm中的數據,進行處理,處理結果返回到Jsp頁面。
  2.4 web.XML
  <?xml version="1.0" encoding="ISO-8859-1"?>
  <!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>
   <display-name>Struts Logon Application</display-name>
   <!-- Standard Action Servlet Configuration (with debugging) -->
   <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>
   <init-param>
   <param-name>debug</param-name>
   <param-value>2</param-value>
   </init-param>
   <init-param>
   <param-name>detail</param-name>
   <param-value>2</param-value>
   </init-param>
   <load-on-startup>2</load-on-startup>
   </servlet>
   <!-- Standard 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>Logon.jsp</welcome-file>
   </welcome-file-list>
   <!-- Struts Tag Library Descriptors -->
   <taglib>
   <taglib-uri>/tags/struts-bean</taglib-uri>
   <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
   </taglib>
   <taglib>
   <taglib-uri>/tags/struts-html</taglib-uri>
   <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
   </taglib>
   <taglib>
   <taglib-uri>/tags/struts-logic</taglib-uri>
   <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
   </taglib>
   <taglib>
   <taglib-uri>/tags/struts-nested</taglib-uri>
   <taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
   </taglib>
   <taglib>
   <taglib-uri>/tags/struts-tiles</taglib-uri>
   <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
   </taglib>
  </web-app>
  
  2.5 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=" com.Logon.app.LoginForm"/>
  </form-beans>
  <action-mappings>
  <action
  path="/Logon" type="com.Logon.app.LogonAction" name="LogonForm" scope="request" input="Logon.jsp">
   <forward name="success" path="/success.jsp"/>
   <forward name="failure" path="/Logon.jsp"/>
  </action>
  <message-resources parameter="resources.application"/>
  </struts-config>
  2.6 application.properties
  Logon.failed = Logon failed!Please Logon in again!
  
  2.7 deploy Logon Application
  創建一個success.jsp,輸入:"successflly!".
  編譯源文件,並且把該應用程序部署在TOMCAT裡,假設部署在app目錄下面。我們可以啟動浏覽器訪問:http://localhost:8080/app/.
  2.8 目錄結構
  /WEB-INF :
  / WEB-INF/lib : Struts.jar
  / WEB-INF/classes :
  / WEB-INF/classes/com/Logon/app : *.class
  / WEB-INF/classes/resources : *.properties
  
  3. 總結
  本文具體講解了安裝方法,並且舉了一個登陸例子作為Struts框架應用。另外,本文對Struts的目錄結構也有說明,這大大減輕了初學者學習負擔,應該可以作為Struts入門之用。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved