Spring MVC implements multiple write methods for a controller

Source: Internet
Author: User
Spring MVC Controller

During the previous development and viewing of the framework, I mistakenly thought that an action must correspond to a controller, which makes me not very impressed with spring MVC, in use, I feel like the original servlet development is a model. I am a lazy person. How can I use this interest to write and develop relevant content? The following questions are raised.

Why does a framework like spring not support writing many different methods through a configuration file or a class like struts2.x, during access, we use parameters to identify which business method is called to process our related requests. After half a day of searching for information and reminders from colleagues, I finally learned how to write multiple methods in a class in spring MVC. It completely implements a convenient and sharp method like struts2.x. It can be implemented in the following two ways. And struts2.x.

1. inherit the old idea and implement the Controller method. Here, we only need to inherit springmvcMultiactioncontrollerYou can write the desired method in it. After writing the method, you need to make it dependent on this parametermethodnameresolver in the configuration file.

<Bean id = "parammethodresolver" class = "org. springframework. Web. servlet. MVC. multiaction. parametermethodnameresolver">

<Property name = "paramname"> <value> method </value> </property>

</Bean>

After relying on this bean, let it inherit when writing the ControllerMultiactioncontroller class.We can use XXX. do? Method = getxxx.

2. Use spring 2.5 and a new method for annotation development. The same functions can be implemented through annotation. The annotation method mainly uses the annotationmethodhandleradapter class for ing. The main ing method is through the configuration file xxx-servlet.xml <! --: Start the annotation function of spring MVC to map requests and annotation pojo. -->

<Bean class = "org. springframework. Web. servlet. MVC. annotation.

Annotationmethodhandleradapter "/>

After the class is injected into the configuration file, the annotation configuration is completed. When a request is initiated, the class will scan Based on the address to check whether there is a corresponding annotation in the controller. Perform Various forwarding and operations through annotations.

After the configuration file is configured, write the controller. The controller does not need to inherit or implement any classes or interfaces. In other words, there is no need to do anything else in this class. This class is the same as other common classes. All we need to do is add an annotation before this class and method. You can complete the ing.

The main annotations are: @ controller, which is the core annotation. After this annotation is added before the class, it becomes a controller. The springmvc container will monitor this class. The second core annotation is @ requestmapping. After this annotation is added, the method will be forwarded. You can access it directly through the address bar.

Several common Controllers

The core of spring MVC is the controller. When we write the controller, spring MVC has so many controllers. Which one is the best choice?

The Controller of spring MVC implements the Controller Interface directly or indirectly. There are mainly the following types of controllers.

1. Simple controller is suitable for access without or without parameters. For example, we want to show all messages and all posts. When using this controller, the handlerequestinternal method is generally rewritten.

2. The Command Controller (abstractcommandcontroller) can replace the self-perception with a simple controller and is suitable for transmitting access with fewer parameters. For example, show my favorite posts .....

3. The form controller (simpleformcontroller) is the most common controller, mainly used for form operations and control. For example, add or modify a post.

4. The Wizard controller is applicable to step-by-step registration and step-by-step data storage. There are many methods to choose from in this controller.

5. multiactioncontroller allows you to write multiple different methods in the same class and use parameters to differentiate which method to access. You can define your own methods in this controller class. The main definition methods are public (modeandview | map | string | void) actionname (httpservletrequest, httpservletresponse, response ).

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.