Explain in detail the implementation of uploading files and client verification in Java’s struts framework

File upload

The struts 2 framework provides built-in support for handling file uploads using HTML form based file uploads. When uploading a file, it is usually stored in a temporary directory. They should be processed by the action class or moved to a permanent directory to ensure that the data is not lost.

Please note that the server has a security policy that may prohibit writing to temporary directories other than directories and directories belonging to web applications.

The file upload in struts is through the pre-defined File Upload Interceptor, which can be accessed through org apache. struts2. interceptor. Part of the defaultstack of the fileuploadinterceptor class. It can still be used in struts Set various parameters in XML, as we will see below.

Create view file: let's start creating the selected file that we think will need to browse and upload. So let's create a pure HTML upload form that allows users to upload the file index jsp:

In the above example, it is worth noting several explanations. First, the enctype attribute of the form is set to multipart / form data. This should be set to enable file upload processing. The next point worth noting is the name of the action method upload and file upload fields of the form - myfile. We need this information to create operation methods and struts configurations.

Next, let's create a simple JSP file success JSP results show that our file upload is successful.

The result file error JSP may have some errors when uploading files:

Create action class: next, let's create a Java class called UploadFile Java this handles the uploaded file, which is stored in a secure location:

uploadFile. Java is a very simple class. It is important to note that using the fileUpload interceptor with the parameter intercetor does solve all the heavy work for us. The file Upload Interceptor enables three parameters, which are provided by default. They are named the following modes:

[your file name parameter] - this is the actual file upload. In this case, "myfile"

[your file name parameter] contenttype - this is the uploaded file and the content type of the file. In this example, it is "myfilecontenttype"

[your file name parameter] filename - this is the name of the uploaded file. In this case, "myfilename"

These three parameters are provided for us, thanks to the interceptor of struts. All we need to do is in our action class, these variables are automatically wired, and we create three parameters with the correct name. Therefore, in the above example, the operation method with three parameters simply returns "success". If everything goes well, otherwise, it returns "error".

Configuration file: the following are the configuration properties of struts 2, which can control the file upload process:

To change these settings, you can use constant tags in the application struts XML file, like me, change the maximum size of the file to be uploaded. Let's have our in struts The XML is as follows:

Since the fileUpload interceptor is part of the interceptor defaultstack, we do not need to explicitly configure it. However, the < interceptor ref > tag can be added to the < action >. The file Upload Interceptor requires two parameters: (a) maximumsize and (b) allowedtypes. The maximumsize parameter sets the maximum file size allowed (about 2MB by default). The content accepted by the allowedtypes parameter is a comma separated list (MIME) type, as shown below:

Here is the web Contents in XML file:

Now right-click the project name and click export > war file to create a war file. Then deploy the war in the webapps directory of Tomcat. Finally, start the Tomcat server and try to access the URL http://localhost:8080/HelloWorldStruts2/upload.jsp 。 This will give the following screen:

Now select the "contacts. TXT" of a file, use the "Browse" button, and then click the upload button to upload the file. You should see the page. You can check that the uploaded file is saved in C: apache-tomcat-6.0 33work.

Please note that using fileUpload to intercept and delete uploaded files automatically, so you need to program to save the uploaded files in some locations before they are deleted.

Error message: the fileuplaod interceptor uses several default error message keys:

Validation framework

Now we'll look at how to use the struts validation framework. At the core of struts, there is a verification framework to assist the application's running rules to execute the operation methods before verification.

JavaScript is usually used to implement client-side authentication. However, it should not rely solely on client authentication. Best practices show that validation should be introduced into application frameworks at all levels. Now, let's look at two ways to add validation to our struts project.

Here, we will take an example. The name and age of the employee will be captured using a simple page. We will verify the two to ensure that the use always enters a name and age should be between 28 and 65. So let's start with the example of the main JSP page.

Create the main page: let's write the index of the main JSP page file JSP, which will be used to collect relevant information about the above employees.

In index JSP uses struts tags, which we haven't covered yet, but we will study the chapters related to these tags. But now, suppose the s: textfield tag prints an input field and s: submit prints a submit button. We have used the label attribute tag, and each tag is created for each tag.

Create a view: we will use the success. JSP file JSP returns the action defined in the case of call to success.

Create action: therefore, let's define a small action class employee, and then add a method called validate (), as shown in employee Java file. Ensure that the action class extends the actionsupport class, otherwise the validate method will not be executed.

As shown in the above example, the validation method of the "name" field checks whether there is a value or not. If no value has been provided, we add an error field with a custom error message "age" field. Secondly, we check that if the entered value is between 28 and 65 or is not the "age" field, if this condition does not meet, we add an error to the above verification field.

Configuration file: finally, let's use struts. Net with everything The XML configuration file is as follows:

Here is the web Contents in XML file:

Now, right-click the project name and click export > war file to create a war file. Then deploy the war in the webapps directory of Tomcat. Finally, start the Tomcat server and try to access the URL http://localhost:8080/HelloWorldStruts2/index.jsp 。 This will give the following screen:

Now do not enter any required information, just click the "submit" button. You will see the following results:

Enter the required information, but input the wrong from field. Let's say "test" and age is 30. Finally, click the "submit" button. You will see the following results:

How does this validation work? When the user presses the submit button, struts 2 will automatically execute the verification method. If the methods in any if statement are listed, struts 2 calls the addfielderror method. If any errors have been added to struts 2, the execute method will not be called. The struts 2 framework will return input as the result of invoking the action.

Therefore, when the validation fails, struts 2 returns the input, and the struts 2 framework will redisplay the index JSP file. Because we use the form tag of struts 2, an error message will be automatically added in struts 2, which is only submitted in the above form.

These error messages are specified in our addfielderror method call. The addfielderror method has two parameters. The first is the form field name error, and the second is the error message, which displays the form field.

To process the return value input, we need to add the following results to our action node in struts xml。

XML based validation: the second method of validation is by putting the action class of an XML file. Struts 2 provides more options for XML based validation, such as e-mail validation, integer range validation, form validation fields, expression validation, regular expression validation, required validation, string required for validation, string length validation, etc.

The XML file needs to be named '[action class]' - validation xml。 So, in our example, we create a file called employee - validation The XML contains the following:

The above XML file will be saved in the classpath along the class file. Let's have our employee action class without the validate () method as follows:

The rest of the settings will remain because it is my previous example. Now, if you run the application, it will produce the same result. What is our previous example:

The advantage of using XML files to store configuration is to allow the separation of validation from application code. You can let developers write code and business analysts create validation XML files. Another thing to note is the authentication type provided by default. There is a lot of validation. By default, struts is used. Common validation includes validation date, regular expression validation, and string length validation. Check the links below for more details on Struts - XML - based validation

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>