Continued on the introduction to Servlet and Spring MVC library, the main
content of the third day is detailed illustration of web site using
Spring MVC. Another main point is the Hibernate ORM library which is used
to unify and simplify the process of database connection and data transaction.
Basic Spring MVC
Spring MVC use standard Servlet to handle web request. Instead of generate
response directly, Spring MVC dispatches the request to corresponding
registered handler which is a method of a controller class. By using the
AOP function of Spring Framework, there are some other action performed before
and after the request handler, including the model data parsing, view rendering, etc.
To config the Spring MVC framework, first step is download the required
dependencies. This is the same as the core module which is acquired form its
official site spring.io. Before the following steps, all
of the dependency JARs are all accessible from the classpath.
The standard web applications in Java are configured in web.xml, in which the
definitions and mappings of the servlet, the interceptor, and the listener
are located. There are also some other meta data affecting thre whole site are listed
in this configuration file. In this step, the web.xml is in the following form.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!-- web.xml : configuration for Java web application-->
<!-- use Spring MVC to handle all of the request -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
In this project, the annotation based configuration will be used. Therefore,
in the dispatcher-servlet.xml file, only the package names in which the
controllers are needed to resolve are listed. This configuration file will be loaded by
the first time the dispatcher servlet is loaded. The defualt name
of this file should be <servlet-name>-servlet.xml, which is free from specifying
explicitly. Otherwise, this file should be configured at web.xml.
With the xml configurations represented above, the core MVC components will be
implemented as follows. In the first test, there is only a simple page
without Model. This site is merely the routing from dispatcher to controller,
then from the controller to view.
The controllers are plain Java class which is annotated with @Controller.
Inside of the controller, each method with annotation @RequestMapping can be mapped
to a specified url pattern. Without explicit annotation, the meaning of return value
is determined by its type. The simplest one is a String representing the path to
view (which will be transformed by the rules defined in view resolver).
The following code is the most simple controller class.
With a JSP page placed at WEB-INF/view/home/index.jsp, the content will be shown in
browser after launching the site via web container like Tomcat, Jetty, etc.
At this point, the directory structure may similar to the following one:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/ root directory
└── WEB-INF
├── applicationContext.xml
├── dispatcher-servlet.xml
├── lib
│ └ .... spring jar files
├── classes
│ └── jackq
│ └── mvc
│ └── controller
│ └── Home.class
├── src
│ └── jackq
│ └── mvc
│ └── controller
│ └── Home.java
├── view
│ └── home
│ └── index.jsp
└── web.xml
Spring MVC with from
One of the most useful feature of Spring MVC is its mapping from html from to
Java object. Based on the previous example, this application will first add a
model class HelloMessage defined as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Model class for test, this is merely a POJO
package jackq.mvc.model;
publicclassHelloMessage{
private String name;
private String message;
publicHelloMessage(){
}
publicHelloMessage(String name, String message){
this.name = name;
this.message = message;
}
public String getName(){
return name;
}
publicvoidsetName(String name){
this.name = name;
}
public String getMessage(){
return message;
}
publicvoidsetMessage(String message){
this.message = message;
}
}
Then update the controller. Inside the Home controller class, add another
method Hello which get message from requset parameter
(either the request body or the url parameter). Then add the @ModelAttribute
annotation to the parameter of the method which is in the type of HelloMessage.
This annotation tells the Spring MVC to abstract parameter from request and
fill into the message object and then add this object to current Model
which can later be accessed in view. The updated Home controller shows as follows:
After launching this page in Servlet container, view the page in browser
and fill-in the form. Then the message will be shown in the browser.
Hibernate ORM & JPA
Hibernate ORM is an Object/Relational Mapping library.
It is configured in hibernate.cfg.xml by default.
The following sample is a simple program to perform CURD action with Hibernate.
Besides, the transaction and annotation based configuration are also used in this program.