使用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如下

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





name="UploadForm"
type="com.nova.colimas.web.form.UploadForm"/>








name="index"
path="/index.do"/>





forward="/pages/index.jsp"/>


type="com.nova.colimas.web.action.protect.FunctionImportAction"
name="UploadForm"
scope="request">






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





value="/WEB-INF/config/tiles-defs.xml" />




property="pathnames"
value="/WEB-INF/config/validator-rules.xml,/WEB-INF/config/validation.xml"/>

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




colimas

Component Library Management System



action
org.apache.struts.action.ActionServlet

config
/WEB-INF/config/struts-config.xml


debug
2


detail
2

2



action
*.do


welcome.jsp



http://java.sun.com/jsp/jstl/core
/WEB-INF/tld/c.tld


http://java.sun.com/jsp/jstl/fmt
/WEB-INF/tld/fmt.tld


http://java.sun.com/jsp/jstl/sql
/WEB-INF/tld/sql.tld


http://java.sun.com/jsp/jstl/xml
/WEB-INF/tld/x.tld


http://java.sun.com/jsp/jstl/functions
/WEB-INF/tld/fn.tld


/tags/struts-bean
/WEB-INF/tld/struts-bean.tld


/tags/struts-html
/WEB-INF/tld/struts-html.tld


/tags/struts-logic
/WEB-INF/tld/struts-logic.tld


/tags/struts-tiles
/WEB-INF/tld/struts-tiles.tld


/tags/struts-nested
/WEB-INF/tld/struts-nested.tld




编写welcome.jsp和/pages/index.jsp文件

<%@ taglib uri="/tags/struts-logic" prefix="logic" %>

<%--
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" %>


Upload Files







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




不支持无定数文件上传。

编写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 设断点,可以停到断点出。非常方便。


你可能感兴趣的:(Struts,Eclipse,JSP,Web,Servlet)