Struts sixth: Struts Core class Actionform

Source: Internet
Author: User
Tags static class

static and dynamic Actionform

Actionform is one of the core classes of the struts framework, similar to an entity class, that collects and saves each data on the form. It is struts that resolves the struts-config.xml file to create the Actionmapping class, created from the Name property in the Actionmapping class.

Actionform is generally static, and if it is a static class, the use of methods is similar to the entity classes in object-oriented objects. Actionform can also be configured dynamically, that is, flexible configuration in struts-config.xml files to achieve dynamic functions.

When configuring dynamic Actionform, it is similar to configuring a static actionform to have a <form-beans> label first, which is configured as follows.

<form-beans>

<form-bean name= "DynaForm" type= "Org.apache.struts.action.DynaActionForm" >

<form-property name= "username" type= "java.lang.String"/>

<form-property name= "Age" type= "Java.lang.Integer"/>

</form-bean>

</form-beans>

And the configuration of static classes is not the same, which is a lot of attribute configuration, this is easy to understand. After the configuration file configuration is complete, struts places the configured attributes into a map whose value is its true values.

In the action class to get the value of the Actionform class, in the static class, directly hit the object's name can be configured to the value, in the dynamic actionform, it can not be directly obtained, said above, struts will put these values in the map, so can be obtained through the map. The specific usage is as follows

Dynaactionform DAF = (dynaactionform) Form;

String username = (string) daf.get ("username");

Integer age = (integer) daf.get (' age ');

Because they are inherited and actionform regardless of form or dynaactionform, they can be cast and can be retrieved directly from the after the strong transfer is complete.

how the interface obtains dynamic Actionform values through an ER expression:

User: ${dynaform.map.username}

Age: ${dynaform.map.age}

or use map to get in the dynamic Actionform map.

Dynamic Actionform is not recommended, after all, dynamic settings have advantages and disadvantages, can be flexible settings, but if set too much, easy to mess, configuration types may be the type of data typing error, here is a general understanding of the good.

Upload of Actionform

Use struts to upload and common upload very similar, the interface form must write Enctype= "Multipart/form-data", as shown below:

<form action= "upload.do" method= "post" enctype= "Multipart/form-data" >

Title: <inputtype= "Text" name= "title" ><br>

File: <inputtype= "file" Name= "MyFile" ><br>

<inputtype= "Submit" value= "submitted" >

</form>

Actionform use Formfile to receive uploaded files, see: Uploadactionform.java

Call Formfile in action to obtain upload file data, using stream output, that is, complete upload, see: Uploadtestaction.java

Use <controller/> tag to configure upload parameters, such as: <controllermaxfilesize= "10M"/>


Code in Actionform:

public class Uploadactionform extends Actionform {

	private String title;
	
	Uploaded files must be formfile declared
	private formfile myfile;

	Public String GetTitle () {return
		title;
	}

	public void Settitle (String title) {
		this.title = title;
	}

	Public Formfile Getmyfile () {return
		myfile;
	}

	public void Setmyfile (Formfile myfile) {
		this.myfile = myfile;
	}
}

Code in action

public class Uploadtestaction extends Action {public
	Actionforward execute (actionmapping mapping, Actionform form,< C1/>httpservletrequest request, HttpServletResponse response)
			throws Exception {uploadactionform UAF
		= ( uploadactionform) Form;
		System.out.println ("title=" + uaf.gettitle ());
		System.out.println ("filename=" + uaf.getmyfile (). GetFileName ());
		Using output stream output to D-Packing directory
		fileoutputstream fos = new FileOutputStream ("d:\\" + uaf.getmyfile (). GetFileName ());
		Fos.write (Uaf.getmyfile (). Getfiledata ());
		Fos.flush ();
		Fos.close ();
		Return Mapping.findforward ("Success");
	}




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.