This is an example from the Spring Guide (second edition), written by Gary Mak and others, in the spring @MVC, which is recorded for the purpose of learning.
Problem: To develop a simple Web application with spring MVC, learn the basic concepts and configuration of this framework.
Solution:
The core component of Spring MVC is a controller (most frameworks are more important than controllers).
In the simplest spring MVC application, the controller is the only servlet that needs to be configured in the Web. xml file.
The controller of Spring MVC is often referred to as the request distribution servlet (Dispatcher servlet), which implements the front controller (Front controllers) in Java EE design mode.
Each Web request passes through the front-end controller so that it can manage the entire request processing process.
When a Web request is sent to a spring MVC application, the controller first accepts the request, and then the Controller organizes the component that processes the request.
A controller class is defined in Spring 3.0 and must be marked with @control annotations. (Do not understand what the annotations are) in the past is to implement a framework-specific interface or extension framework-specific base class, 3.0 will only use to add annotations.
With the @controller annotated class (that is, the Controller Class), go back to someone (the appropriate handler method) to process the request.
The Controller method modified with @requestmapping annotations becomes the handler method.
The signature of these handler methods is like any standard class method, and can be arbitrarily modified. Simultaneous handler methods can also return a series of values based on the application logic.
After the handler finishes the request processing, the control is delegated to the view, which handles the return value of the program method, and the view can be in many forms.
In order to pass information from the controller to the view, the handler's method returns a logical view name , string or void is irrelevant, because the input parameters of the handler can be used by the view.
When a controller class accepts a view, it relies on the view parser to resolve the logical view name to a specific view implementation (for example, user.jsp).
A view resolver is a bean configured in the context of a Web application that implements the Viewresolver interface, whose task is to return the specific implementation of the logical name view.
The purpose of the view is to display the objects added to the logic of the handler method to the user.
Working principle:
A simple example of a Web application-developing a course reservation system.
First, create the following three classes in the domain sub-package:
The first is a predetermined class,Reservation.java
The second one is the reservation,Player.java .
The third one is the type of movement,Sporttype.java .
The following service interfaces are defined in the service sub-package:Reservationservice.java, which provides a predetermined service for the presentation layer.
This interface is implemented with classes that include hard-coded additions to several predetermined orders, i.e., Reservationserviceimpl.java. This interface should be implemented with database persistence in production.
Set up a spring MVC app
Creating a spring MVC app layout is the same as a standard Java Web app, but the exception is that you must add some spring MVC-specific configuration files and the necessary library programs.
Users can directly access files outside of the Web-inf directory via a URL, so the CSS files and images must be there, and when using spring MVC, the JSP file acts as a template.
The framework reads JSP files for generating dynamic content, so the JSP must be in the Web-inf directory to avoid direct access.
Creating a configuration file
Web. XML is an essential configuration file for the Java website, where you define the servlet for your application and how Web requests are mapped to those servlets.
For spring MVC applications, you only need to define a Dispatcherservlet instance as the front-end controller, and you can define multiple if needed.
In large applications, it may be more convenient to use multiple Dispatcherservlet instances to assign Dispatcherservlet instances to specific URLs.
Another purpose of the Dispatcherservlet name is to let Dispatcherservlet decide which spring MVC configuration file to load.
The file name that is found by default is the servlet name plus the-servlet.xml file.
Of course, you can also explicitly specify a configuration file in the servlet parameter contextconfiglocation.
As above court-servlet.xml is a standard spring bean configuration file.
Each layer declares a bean configuration file (such as the continuous layer court-persistence.xml and the court-service.xml of the service layer).
In order for spring to load a configuration file other than Court-servlet.xml, the servlet listener Contextloaderlistenermust be defined in Web. Xml.
(This configuration file the head of the dizzy, not suitable for me this low-cost group of small-business Ah! )
Contextloaderlistener loads the specified bean configuration file into the context of the root app, and each dispatcherservlet loads its configuration file into its own application context, and references the root application context for the duration of the hierarchy.
So each Dispatcherservlet instance loads a context that can access or even overwrite the bean in the context declaration of the root application (or vice versa).
(Here is what ah, what what what.) Root app context and your own application contexts? A local variable overrides a global variable? )
Activating the Spring MVC annotation Scan
(on radar?) )
Before you create a controller class, you configure the Web app to scan each class to determine the presence of @controller and @requestmapping annotations.
Only classes with these annotations can operate like controllers.
First, to let spring automatically monitor annotations, you must enable the Spring component scanning feature through the <context:component-scan> element.
In addition to this, Spring MVC's @RequestMapping annotations Map URL requests to the controller classes and their corresponding processing methods, which requires more declarations in the Web application context.
Registers a defaultannotationhandlermapping and an annotationmethodhandleradapter instance in the Web App context.
The above two instances process @requestmapping annotations at the class level and method level , respectively.
Create a Spring MVC controller
The annotation-based controller class can be any class without implementing a special interface or extending a particular base class.
This class can be annotated with @controller. A controller can define one or more handler methods to handle one or more actions.
@RequestMapping annotations can be applied at the class level or at the method level.
@RequestMapping annotations can also contain properties.
The first value used in this class ("/welcome") is used to specify the URL that embodies the controller action, meaning that the request accepted by the/welcome URL is handled by the Welcomecontroller class.
Once the controller starts processing the request, it invokes the default HTTP get handler method that is delegated to the controller.
This is because each initial request to the URL is an HTTP get type.
So when the controller processes the request to the/welcome URL, the request is then delegated to the default HTTP Get handler method for processing.
The @RequestMapping (method=requestmethod.get) annotation is used to decorate the Welcome method as the default HTTP GET handler method for the controller.
The new constructor @autowired annotation for the class is added, which allows the constructor of the class to declare its fields from the application configuration file Court-service.xml.
@Autowired annotations do not need to use XML injection properties.
The @RequestMapping ("/reservationquery") statement indicates that any request on the url/reservationquery is also handled by the controller.
A class-level annotation corresponds to two handler methods, one that occurs when an HTTP GET request is made on the/reservationquery URL, and another method is called when an HTTP POST request occurs.
Most requests in Web applications are HTTP Get,http Post requests that are typically emitted when a user submits an HTML form.
A @RequestParam ("Courtname") String Courtname declaration that extracts a request parameter named Courtname.
The other is the model Declaration, which defines the object that passes data to the returned view.
Create a JSP view
In spring MVC, views are often JSP templates written by Jstl. When the dispatcherservlet that is defined in the application's Web. xml file receives the name of the view returned by the handler, it resolves the logical view name to the view implementation for display.
Deploying Web Apps
Develop a simple Web application with spring MVC