Use eclipse 3.1 WST to compile struts File Upload

Source: Internet
Author: User
Tags file info tld

You can download the WST with eclipse3.1 file on eclipse.org. This file includes eclipse3.1 and WST and all the plug-ins it requires. Basically, no other plug-ins are required.

After unpacking the downloaded file, create a new project: Dynamic Web project, which is the Web server for configuration and debugging. In Tomcat 5.5, enter the installation directory of Tomcat 5.5.

Copy the lib and TLD files required by struts to the WEB-INF directory

Write the WEB-INF in the struts-config.xml/config directory as follows

<? 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 = "definitions-config"
Value = "/WEB-INF/config/tiles-defs.xml"/>
<! -- Set Module-awareness to true -->
<Set-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>

Modify web. XML to fill in the 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>
Compile the welcome. jsp and/pages/index. jsp files.

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

Note that the red letter attribute must be filled in; otherwise, an error occurs. Only one file can be uploaded.

<HTML: file property = "thefile1"/>
<HTML: file property = "thefile2"/>
<HTML: file property = "thefile3"/>
You cannot upload a non-fixed number of files.

Write Action and actionform

/*
* Created on 2005/07/21
*
* Todo to change the template for this generated file go
* 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 (); // example
If (file. getfilename (). endswith ("Doc ")){
// Use the Word and Excel analysis classes provided on this site to extract text strings. the uploaded file content will be displayed on the JSP webpage.

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
* 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;
}

}

Debug welcome. jsp. Eclipse will start Tomcat and print the startup log in the console, where: Web. xml cannot be found appears.

This error can be ignored.

Select a file and click Submit of index. jsp. The program jumps to functionimportaction. If you set a breakpoint in functionimportaction, you can stop at the break point. Very convenient.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.