锘??xml version="1.0" encoding="utf-8" standalone="yes"?> <!-- The "controller" element describes the ControllerConfig bean bufferSize The size of the input buffer used when processing catalog Name of the catalog to use when processing requests className Fully qualified Java class name of the command Name of the command to execute to process a request. contentType Default content type (and optional character encoding) to forwardPattern Replacement pattern defining how the "path" attribute of a inputForward Set to "true" if you want the "input" attribute of locale Set to "true" if you want a Locale object stored in the maxFileSize The maximum size (in bytes) of a file to be accepted as a memFileSize The maximum size (in bytes) of a file whose contents will multipartClass The fully qualified Java class name of the multipart nocache Set to "true" if you want the controller to add HTTP pagePattern Replacement pattern defining how the "page" attribute of processorClass The fully qualified Java class name of the tempDir Temporary working directory to use when processing -->
]]>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'converter_index.jsp' starting page</title>
</head>
<body>
<form action="typeconvert.do" method="post">
<table>
<tr>
<td>intValue:</td><td><input type="text" name="intValue"/></td>
</tr>
<tr>
<td>doubleValue:</td><td><input type="text" name="doubleValue"/></td>
</tr>
<tr>
<td>booleanValue:</td><td><input type="text" name="booleanValue"/></td>
</tr>
<tr>
<td>sqlValue:</td><td><input type="text" name="sqlValue"/></td>
</tr>
<tr>
<td>utilDateValue:</td><td><input type="text" name="utilDateValue"/></td>
</tr>
<tr>
<td>submit:</td><td><input type="submit" name="submit"/></td>
</tr>
</table>
</form>
</body>
</html>
<%@ page import="com.eplugger.struts.form.TypeConvertActionForm" %>
<%@ page isELIgnored="false" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'converter_success.jsp' starting page</title>
</head>
<body>
EL琛ㄨ揪寮忓彇鍊鹼細<br>
intValue:${typeConvertForm.intValue }<br>
doubleValue:${typeConvertForm.doubleValue }<br>
booleanValue:${typeConvertForm.booleanValue }<br>
java.sql.DateValue:${typeConvertForm.sqlValue }<br>
java.util.DateValue:${typeConvertForm.utilDateValue }<br>
jsp鍙栧鹼細<br>
<%
TypeConvertActionForm tcaf =(TypeConvertActionForm) request.getAttribute("typeConvertForm");
%>
<%
out.print(tcaf.getIntValue());
%><br>
<%
out.print(tcaf.getDoubleValue());
%><br>
<%
out.print(tcaf.getBooleanValue());
%><br>
<%
out.print(tcaf.getSqlValue());
%><br>
<%
out.print(tcaf.getUtilDateValue());
%><br>
</body>
</html>
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;
public class TypeConvertAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return mapping.findForward("success");
}
}
import org.apache.struts.action.ActionForm;
public class TypeConvertActionForm extends ActionForm {
private int intValue;
private double doubleValue;
private boolean booleanValue;
private java.sql.Date sqlValue;
private java.util.Date utilDateValue;
public int getIntValue() {
return intValue;
}
public void setIntValue(int intValue) {
this.intValue = intValue;
}
public double getDoubleValue() {
return doubleValue;
}
public void setDoubleValue(double doubleValue) {
this.doubleValue = doubleValue;
}
public boolean getBooleanValue() {
return booleanValue;
}
public void setBooleanValue(boolean booleanValue) {
this.booleanValue = booleanValue;
}
public java.sql.Date getSqlValue() {
return sqlValue;
}
public void setSqlValue(java.sql.Date sqlValue) {
this.sqlValue = sqlValue;
}
public java.util.Date getUtilDateValue() {
return utilDateValue;
}
public void setUtilDateValue(java.util.Date utilDateValue) {
this.utilDateValue = utilDateValue;
}
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.beanutils.Converter;
public class UtilDateConverter implements Converter {
public Object convert(Class type, Object value) {
System.out.println("value="+value);
if(value==null){
return value;
}
Date date=null;
if(value instanceof String){
SimpleDateFormat sdf = new SimpleDateFormat("yyy-mm-dd");
try{
date = sdf.parse((String)value);
}catch(ParseException e){
e.printStackTrace();
}
}
return date;
}
}
涓嬮潰濡傛灉鐢℉ttpServlet浠g爜濡備笅錛?br />
UtilDateConverterInitWithServlet.java
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.commons.beanutils.ConvertUtils;
import com.eplugger.struts.UtilDateConverter;
/**
* 娉ㄥ唽java.util.date杞崲鍣?br />
*/
public class UtilDateConverterInitWithServlet extends HttpServlet {
@Override
public void init() throws ServletException {
ConvertUtils.register(new UtilDateConverter(), Date.class);
}
}
<servlet-name>UtilDateConverterInitWithServlet</servlet-name>
<servlet-class>com.eplugger.struts.converter.UtilDateConverterInitWithServlet</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>
import java.util.Date;
import javax.servlet.ServletException;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
import com.eplugger.struts.UtilDateConverter;
public class UtilDateConverterInitWithPlugIn implements PlugIn {
public void destroy() {
}
public void init(ActionServlet servlet, ModuleConfig config)
throws ServletException {
ConvertUtils.register(new UtilDateConverter(), Date.class);
}
}
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="fileupload" type="com.eplugger.struts.form.FileUploadActionForm"/>
<form-bean name="blankfield" type="com.eplugger.struts.form.BlankFieldActionForm"/>
<form-bean name="typeConvertForm" type="com.eplugger.struts.form.TypeConvertActionForm"/>
</form-beans>
<action-mappings>
<action path="/typeconvert"
type="com.eplugger.struts.action.TypeConvertAction"
name="typeConvertForm"
scope="request"
>
<forward name="success" path="/converter_success.jsp"/>
</action>
</action-mappings>
<plug-in className="com.eplugger.struts.converter.UtilDateConverterInitWithPlugIn"/>
</struts-config>
]]>
]]>
1銆乽ploadIndex.jsp 娉ㄦ剰錛歠orm涓璵ethod="post" 錛沞nctype="multipart/form-data"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>嫻嬭瘯Strust鏂囦歡涓婁紶</title>
</head>
<body>
<form action="fileupload.do" method="post" enctype="multipart/form-data" >
<table>
鏍囬錛?/span><input type="text" name="title"/>
<input type="file" name="myfile"/>
<input type="submit" value="涓婁紶"/>
</table>
</form>
</body>
</html>
2 <%@ page isELIgnored="false" %>
3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <html>
5 <head>
6 <title>uploadSuccess.jsp</title>
7 </head>
8 <body>
9 鏂囦歡鏍囬錛?{fileupload.title }<br>
10 鏂囦歡鍚嶏細${fileupload.myfile.fileName }<br>
11 鏂囦歡澶у皬錛?{fileupload.myfile.fileSize}<br>
12 </body>
13 </html>
14
2 import org.apache.struts.action.ActionForm;
3 import org.apache.struts.upload.FormFile;
4 public class FileUploadActionForm extends ActionForm {
5 private String title;
6 private FormFile myfile; //娉ㄦ剰錛氫笂浼犳枃浠剁殑綾誨瀷蹇呴』鏄疐ormFile錛涘畠鏄痵truts鎻愪緵鐨勶紱
7 public String getTitle() {
8 return title;
9 }
10 public void setTitle(String title) {
11 this.title = title;
12 }
13 public FormFile getMyfile() {
14 return myfile;
15 }
16 public void setMyfile(FormFile myfile) {
17 this.myfile = myfile;
18 }
19 }
20
import java.io.FileOutputStream;
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.upload.FormFile;
import com.eplugger.struts.form.FileUploadActionForm;
public class FileUploadAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//鑾峰緱form
FileUploadActionForm fpaf =(FileUploadActionForm)form;
//鑾峰緱閫氳繃form浼犳潵鐨則itle鍊鹼紱
String title = fpaf.getTitle();
//鑾峰緱閫氳繃form浼犳潵鐨勬枃浠訛紱娉ㄦ剰綾誨瀷蹇呴』鏄疐ormFile錛?/span>
FormFile myFile = fpaf.getMyfile();
if(myFile != null ){
//鍒涘緩鏂囦歡杈撳嚭鐨勮礬寰?/span>
FileOutputStream fos= new FileOutputStream("e:\\"+myFile.getFileName());
//杈撳嚭錛堜竴涓猙ayt[]鏁扮粍錛?/span>
fos.write(myFile.getFileData());
//鎶婂唴瀛樹腑鐨勬枃浠跺彉鎴愮墿鐞嗙殑
fos.flush();
//鍏抽棴
fos.close();
}
request.setAttribute("title","title");
return mapping.findForward("success");
}
}
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="fileupload" type="com.eplugger.struts.form.FileUploadActionForm"/>
</form-beans>
<action-mappings>
<action path="/fileupload"
type="com.eplugger.struts.action.FileUploadAction"
name="fileupload"
scope="request"
>
<forward name="success" path="/uploadSuccess.jsp"/>
</action>
</action-mappings>
<controller maxFileSize="10M"/>
</struts-config>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<!-- Default -->
<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>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
鏈鍚庤涓涓嬶紝鍏充簬鏂囦歡鐨勫ぇ灝忕瓑鍙傛暟閮藉彲浠ュ啀struts-config.xml鏂囦歡涓殑<controller />涓厤緗紝鍙互閰嶇疆鐨勫睘鎬у彲浠ュ弬鑰僺truts-core-1.3.10.jar涓殑org.apache.struts.resources.struts-config_1_3.dtd
涓昏鍐呭濡備笅錛?br />
[org.apache.struts.config.ControllerConfig] that encapsulates
a module's runtime configuration. The following
attributes are defined:
file uploads.
[4096]
for this module.
[struts]
ControllerConfig subclass for this controller object.
If specified, the object must be a subclass of the
default class.
["org.apache.struts.config.ControllerConfig"]
[servlet-standard]
be set on each response. May be overridden by the Action,
JSP, or other resource to which the request is forwarded.
["text/html"]
<forward> element is mapped to a context-relative URL when
it starts with a slash (and when the contextRelative
property is false). This value may consist of any
combination of the following:
- "$M" - Replaced by the module prefix of this module
- "$P" - Replaced by the "path" attribute of the selected
"forward" element
- "$$" - Causes a literal dollar sign to be rendered
- "$x" - (Where "x" is any character not defined above)
Silently swallowed, reserved for future use
If not specified, the default forwardPattern is "$M$P",
which is consistent with the previous behavior of
forwards. Since Struts 1.1. ["$M$P"]
<action> elements to be the name of a local or global
ActionForward, which will then be used to calculate the
ultimate URL. Set to "false" (the default) to treat the
"input" parameter of <action> elements as a
module-relative path to the resource
to be used as the input form. Since Struts 1.1.
[false]
user's session if not already present.
[true]
file upload. Can be expressed as a number followed by a
"K", "M", or "G", which are interpreted to mean kilobytes,
megabytes, or gigabytes, respectively.
["250M"]
be retained in memory after uploading. Files larger than
this threshold will be written to some alternative storage
medium, typically a hard disk. Can be expressed as a number
followed by a "K", "M", or "G", which are interpreted to
mean kilobytes, megabytes, or gigabytes, respectively.
["256K"]
request handler class to be used with this module.
["org.apache.struts.upload.CommonsMultipartRequestHandler"]
headers for defeating caching to every response from
this module. [false]
custom tags using it is mapped to a context-relative URL
of the corresponding resource. This value may consist of
any combination of the following:
- "$M" - Replaced by the module prefix of this module
- "$P" - Replaced by the value of the "page" attribute
- "$$" - Causes a literal dollar sign to be rendered
- "$x" - (Where "x" is any character not defined above)
Silently swallowed, reserved for future use
If not specified, the default forwardPattern is
"$M$P", which is consistent with previous hard coded
behavior of URL evaluation for "page" attributes.
["$M$P"]
RequestProcessor subclass to be used with this module.
["org.apache.struts.chain.ComposableRequestProcessor"]
file uploads.
[{Directory provided by servlet container}]
]]>
2.鎵娑夊強閬撳痙鏂囦歡鏈夛細web.xml錛泂truts_config.xml錛涗袱涓〉闈紙index.jsp銆乨yna_actionform.jsp錛夛紱涓涓猘ction(DynaAction.java)錛?br />
3.榪欑鍔ㄦ佺殑DynaActionForm鏄皢Form緇х畫浼犻掋傝繚鑳屼簡鍒嗗眰鐨勮鍒欙紙灝戠敤錛夛紱
web.xml 榪欎釜鏂囦歡娌′粈涔堢殑鍒殑銆?br />