使用Eclipse 3.1 WST编写Struts文件上传

可以在eclipse.org网站上下载WST with Eclipse3.1文件,该文件包括Eclipse3.1和WST以及它所需要的所有插件,基本上不用装其他插件了。

解包下载的文件后,建立新项目:Dynamic Web Project,这是需要配置调试用的Web服务器。我使用Tomcat 5.5,填写Tomcat 5.5的安装目录。

将Struts所需要的lib和Tld文件拷贝到WEB-INF目录

在WEB-INF/config目录里编写struts-config.xml如下

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<!--
     This is the Struts configuration file of Colimas
  Created By: Zhao Lei
  Created Date:2005/06/14
  Last Modified By: Zhao Lei
  Last Modified Date:2005/07/25
-->


<struts-config>
<!-- ================================================ Form Bean Definitions -->

    <form-beans> 
       <!--30 Upload File formbean-->         
       <form-bean
   name="UploadForm"
   type="com.nova.colimas.web.form.UploadForm"/>  

    </form-beans>


<!-- ========================================= Global Exception Definitions -->

    <global-exceptions>
        <!-- sample exception handler
        <exception
            key="expired.password"
            type="app.ExpiredPasswordException"
            path="/changePassword.jsp"/>
        end sample -->
    </global-exceptions>


<!-- =========================================== Global Forward Definitions -->

    <global-forwards>
        <!-- Default forward to "Welcome" action -->
        <!-- Demonstrates using index.jsp to forward -->
        <forward
            name="index"
            path="/index.do"/>
    </global-forwards>


<!-- =========================================== Action Mapping Definitions -->

    <action-mappings>
            <!-- Default "Welcome" action -->
            <!-- Forwards to Welcome.jsp -->
        <action  path="/index"
            forward="/pages/index.jsp"/>  

  <!-- 45 File Load Action-->
    <action    path="/protect/FileLoadAction"
              type="com.nova.colimas.web.action.protect.FunctionImportAction"
              name="UploadForm"
              scope="request">
              <forward name="success" path="/index.do"/>
  </action>
            

        
    </action-mappings>


<!-- ============================================= Controller Configuration -->

    <controller
       processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>


<!-- ======================================== Message Resources Definitions -->
    <message-resources parameter="resources.template" />
<!-- =============================================== Plug Ins Configuration -->

    <plug-in className="org.apache.struts.tiles.TilesPlugin" >

      <!-- Path to XML definition file -->
      <set-property property="definitions-config"
                       value="/WEB-INF/config/tiles-defs.xml" />
      <!-- Set Module-awareness to true -->
      <set-property property="moduleAware" value="true" />
    </plug-in>


  <!-- =================================================== Validator plugin -->

  <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    <set-property
        property="pathnames"
        value="/WEB-INF/config/validator-rules.xml,/WEB-INF/config/validation.xml"/>
  </plug-in>

</struts-config>

修改Web.xml将struts-config.xml填进去:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <display-name>
 colimas</display-name>
  <display-name>Component Library Management System</display-name>

 <servlet>
    <!-- Standard Action Servlet Configuration (with debugging) -->
   <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/config/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>
 <servlet-mapping>
    <!-- Standard Action Servlet Mapping -->
   <servlet-name>action</servlet-name>
   <url-pattern>*.do</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
  <welcome-file>welcome.jsp</welcome-file>
 </welcome-file-list>

 <jsp-config>
   <taglib>
     <taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri>
     <taglib-location>/WEB-INF/tld/c.tld</taglib-location>
   </taglib>
   <taglib>
     <taglib-uri>http://java.sun.com/jsp/jstl/fmt</taglib-uri>
     <taglib-location>/WEB-INF/tld/fmt.tld</taglib-location>
   </taglib>
   <taglib>
     <taglib-uri>http://java.sun.com/jsp/jstl/sql</taglib-uri>
     <taglib-location>/WEB-INF/tld/sql.tld</taglib-location>
   </taglib>
   <taglib>
     <taglib-uri>http://java.sun.com/jsp/jstl/xml</taglib-uri>
     <taglib-location>/WEB-INF/tld/x.tld</taglib-location>
   </taglib>
   <taglib>
     <taglib-uri>http://java.sun.com/jsp/jstl/functions</taglib-uri>
     <taglib-location>/WEB-INF/tld/fn.tld</taglib-location>
   </taglib>
   <taglib>
     <taglib-uri>/tags/struts-bean</taglib-uri>
     <taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>
   </taglib>
   <taglib>
     <taglib-uri>/tags/struts-html</taglib-uri>
     <taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
   </taglib>
   <taglib>
     <taglib-uri>/tags/struts-logic</taglib-uri>
     <taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>
   </taglib>
   <taglib>
     <taglib-uri>/tags/struts-tiles</taglib-uri>
     <taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location>
   </taglib>
   <taglib>
     <taglib-uri>/tags/struts-nested</taglib-uri>
     <taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location>
   </taglib>
 </jsp-config>
 
</web-app>
编写welcome.jsp和/pages/index.jsp文件

<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<logic:redirect forward="index"/>
<%--
Redirect default requests to Welcome global ActionForward.
By using a redirect, the user-agent will change address to match the path of our Welcome ActionForward.

--%>

index.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>

<html:html>
 <Title>Upload Files</Title>
 <body>
  <html:form action="/protect/FileLoadAction.do" enctype="multipart/form-data">
   <html:file property="theFile"/>
   <html:submit/>
  </html:form>
  <bean:write name="UploadForm" property="theText"/>
 </body>
</html:html>

注意红字属性必须填写,否则会出错。该<html:file>只能上传一个文件,如果要上传多个只能

<html:file property="theFile1"/>
<html:file property="theFile2"/>
<html:file property="theFile3"/>
不支持无定数文件上传。

编写Action和ActionForm

/*
 * Created on 2005/07/21
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.nova.colimas.web.action.protect;

import javax.servlet.http.HttpServletRequest;
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.action.ActionMessages;
import org.apache.struts.upload.FormFile;

import com.nova.colimas.common.doc.*;
import com.nova.colimas.web.form.UploadForm;

/**
 * @author tyrone
 *
 */
public class FunctionImportAction extends Action {
 public ActionForward execute(ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
 throws Exception{  
  /**
   * 1 File Upload the file
   * 2 get the value from WordProcess
   * 3 Analysis
   * 4 show to user
   */
  if (form instanceof UploadForm){
   String text=null;
   UploadForm theForm = (UploadForm ) form;
   FormFile file = theForm.getTheFile();//Ž擾
   if(file.getFileName().endsWith("doc")){
    //使用本站提供的Word和Excel分析类可以提取文本字符串,上传文件内容将显示在jsp网页上

     text=WordProcess.run(file.getInputStream());
   }else if(file.getFileName().endsWith("xls"))
     text=ExcelProcess.run(file.getInputStream());
    else
     text=TextProcess.run(file.getInputStream());
   theForm.setTheText(text);
   
  }
  return mapping.findForward("success");
 }
}

/*
 * Created on 2005/06/25
 * @Author Tyrone
 * Save the upload file info and stream
 * used by struts FormFile
 */
package com.nova.colimas.web.form;

import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.*;
/**
 * @author tyrone
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class UploadForm extends ActionForm {

 private FormFile theFile;
 private String theText;

 
 /**
  * @return Returns the theFile.
  */
 public FormFile getTheFile() {
  return this.theFile;
 }
 /**
  * @param theFile The theFile to set.
  */
 public void setTheFile(FormFile property1) {
  this.theFile = property1;
 }
 public void Reset() {
  this.theFile=null;
  this.theText="";
  return;
 }
 /**
  * @return Returns the theText.
  */
 public String getTheText() {
  return theText;
 }
 /**
  * @param theText The theText to set.
  */
 public void setTheText(String property1) {
  this.theText = property1;
 }

}

调试welcome.jsp。eclipse会启动Tomcat,在Console里打印启动log,其中会出现:web.xml cannot be found

这个错误可以忽略,不会有问题。

选择文件,点击index.jsp的submit。程序会跳转到FunctionImportAction 。如果在FunctionImportAction 设断点,可以停到断点出。非常方便。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值