1.Create a new Maven project
Go to File -> Project ->Maven -> Maven Project.
In the “Select project name and location” page of the wizard, make sure that “Create a simple project (skip archetype selection)” option is unchecked, hit “Next” to continue with default values.
Here the maven archetype for creating a web application must be added. Click on “Add Archetype” and add the archetype. Set the “Archetype Group Id” variable to , the “Archetype artifact Id” variable to and the “Archetype Version” to . Click on “OK” to continue.
In the “Enter an artifact id” page of the wizard, you can define the name and main package of your project. Set the “Group Id” variable to
and the “Artifact Id” variable to . The aforementioned selections compose the main project package as and the project name as . Set the “Package” variable to , so that a war file will be created to be deployed to tomcat server. Hit “Finish” to exit the wizard and to create your project.The Maven project structure is shown below:
-
It consists of the following folders:
- /src/main/java folder, that contains source files for the dynamic content of the application,
- /src/test/java folder contains all source files for unit tests,
- /src/main/resources folder contains configurations files,
- /target folder contains the compiled and packaged deliverables,
- /src/main/resources/webapp/WEB-INF folder contains the deployment descriptors for the Web application ,
- the pom.xml is the project object model (POM) file. The single file that contains all project related configuration.
2. Add Spring-MVC dependencies
Add the dependencies in Maven’s
file, by editing it at the “Pom.xml” page of the POM editor. The dependency needed for MVC is the package. The and the packages will be also used here for validation. The and packages are used for uploading the file.pom.xml
3. Create the model
is a simple Java class, with a property named field, This field is a , which is a representation of an uploaded file received in a multipart request. It has getters and setters, so that it is accessible from the view.
File.java
4. Create a Validator
The validator created here checks if there is a file uploaded. It specifically checks the size of the file and if it is equal to
, then a validation message is rendered in the view.In order to create a validator class, we are making use of the API provided by Spring MVC.
below implements the , and overrides the two methods it provides.The
method is used to check if the validator can validate instances of the .In the
method, an instance of the class is provided, and an object. The is used here, since it offers validation API methods to check the fields of the object. So in this method we can check if the the file size is equal to zero. All error messages are passed in the object. A file with error messages is used here to pass various validation messages to the object as shown below:FileValidator.java
The
file below is the file that contains the error message.validation.properties
5. Create the Controller
The
is where the will delegate requests. The annotation indicates that the class serves the role of a Controller. The annotation is used to map a URL to either an entire class or a particular handler method.A
is injected here, via the annotation, also making use of the annotation to specify that the implementation of the class is injected.The
annotation in method allows us to configure web data binding directly within the controller. With we can initialize the , that is used for data binding from web request parameters to JavaBean objects. Here, the is where the validator is set.The Controller consists of two basic methods, a GET method, which is
and a POST method, which is . The first method creates and returns to the view a new instance of the class. The second method also gets the , and the object created, which now has the uploaded file in its file param. is annotated with the annotation, which allows the file object to be validated with the validator. is where all validation errors are automatically passed, so it can be used to decide the next navigation step. If there are no errors, the validation is successful, so the method returns the String representation of the page, and the file object is passed at the . Otherwise, the returned String is the String representation of the page, which also has the error messages, as will be shown below.FileController.java
6. Create the view to upload the file
The view below is a simple example of how to create a form where a file can be uploaded. It is a simple html view consisting of the
and html tags.In order to create a form in Spring MVC, we make use of the
tag. Its property is set to POST, and the property is set to the name of the backing bean that is binded to the Model, which is the class.Inside the
tag, we are making use of the attribute, which lets the browser know how to encode the form as multipart request.The
tag with property set to is used to place the uploaded file. The tag defines where the error message of the specified field will be displayed in the view. Finally, the tag, with property set to is used for the upload button.file.jsp
Below is the page that will be rendered when the file upload succeeds:
successFile.jsp
7. Configure the application
The files that we must configure in the application are the
file and the file.The
file is the file that defines everything about the application that a server needs to know. It is placed in the directory of the application. The element declares the . When the is initialized, the framework will try to load the application context from a file named located in directory. So, we have created the file, that will be explained below. The element of file specifies what URLs will be handled by the .web.xml
The file is also placed in directory. The bean is used as internal resource views resolver, meaning that it will find the and files in the folder. We can also set properties such as or to the view name to generate the final view page URL. This is the file where all beans created, such as Controllers are placed and defined.
The
tag is used, so that the Spring container will search for all annotated classes under the package. The tag is used, so that the container searches for annotated classes, to resolve MVC. The class is also defined here as a bean, with an id.The
is also defined here as a bean. This class is needed beacause it is the resolver activated by Spring to hand over the file upload request.Finally, the
is used, to provide access to resource bundles using specified basenames. Its property is set to , thus pointing to the properties file that holds the validation messages.mvc-dispatcher-servlet.xml
8. Run the application
Now, let’s run the application. We first build the project with Maven. All we have to do is right click on the project and select -> Run As: Maven build. The goal must be set to package. The file produced must be placed in folder of tomcat. Then, we can start the server.
Hit on:
Then press on the upload button, before having chosen on a file.
As you can see, the validator message is displayed, since no file has been uploaded yet.
Now browse on your computer and choose a file to upload:
As a result the
view is rendered.