Spring MVC in Spring 3.x

Source: Internet
Author: User
Http://howsun.blog.sohu.com/107477052.html

In 3.0, the famous Spring framework December 5 was published by Juergen Hoeller, one of its authors, in his blog, and was named a milestone version, which gave Spring fans a great pleasure. I opened the "express train" and pulled two packages back. Unfortunately, the reference documentation has not been provided yet (only the API documentation), which brings great difficulties to learning Spring 3.0, but it does not block the author's interest in new products.

Mr. Rod Johnson, father of Spring, predicted that EJB would die (controversial) as early as 2003, And the bloated attack on EJB was abusing programmers. However, when EJB 3.0 was released, it was almost sentenced to the death penalty by Spring. However, since the 2.0 version, Spring has become more popular than EJB, and the battle between the two remains uninterrupted, this is also the reason why the Spring 3.0 documents have not been sorted out. Of course, Spring and EJB have many unique advantages, such as distributed computing of EJB, standard specification, IoC of Spring, AOP aspect programming, coupling integration, MVC, etc, their respective growth will become even more powerful in the enterprise. Spring has already been added to the J2EE specification, and the J2EE world will be even more exciting ......

It may be that the rigid WEB framework of Struts1 is boring for Spring MVC. Especially after version 2.5, full annotation configuration is supported, so that no xml file has been written for a long time.

Version 3.0 is fully compatible with version 2.5, so it is easier to understand version 2.5 @ MVC. As Arjen Poutsma said in his blog, the 3.0 era will focus on expressing state transfer (REST, hope I didn't translate it wrong, Kingsoft is translated as "REST ") network services and easier network programming. Indeed, more controller types are added and the distributed architecture based on SOAP/WSDL/WS is enhanced.

Recall @ MVC in the 2.5 annotation method for an example:

@ Controller
Public class ArticleController {

@ RequestMapping ("/articleView ")
Public String getArticle (@ RequestParam ("id") String id, HttpServletRequest request ){
Request. setAttribute ("article", service. find (Article. class, id ));
Return "articleView ";
}

}

ArticleController does not implement any interfaces. It is the most common pojo. If the browser comes with articleView. do? For the Request id = xxx, Spring will find the getArticle () method. The first parameter of this method is bound to the request parameter on the URL, the second is the J2EE standard request object (it can be seen that Spring MVC is non-intrusive, unlike the abnormal Struts2). In fact, you can also specify HttpServletResponse, ModelMap, or even your own type, spring will pass in the value for you. Use a logic layer service component to search for the Article object at the underlying layer based on the id parameter value and put it into the request scope. The page view name is returned at the end. In this example, it is returned to the articleView. jsp.

Next, let's try again:

@ Controller
Public class ArticleController {

@ RequestMapping ("/articleView _*")
Public String getArticle (HttpServletRequest request ){

String id = StringUtil. getParam (request. getRequestURI (), "articleView _*");
Request. setAttribute ("article", service. find (Article. class, id ));
Return "articleView ";
}

}

For articleView_aaa.do, articleView_bbbb.do, articleView_c5h8j2.do, and articleView_xxx.do, all such requests are handled by the getArticle () method. Is it interesting?

Added@ PathVariableAnnotation to support variable request paths. Replace the above Code in version 3.0:

@ Controller
Public class ArticleController {

@ RequestMapping ("/articleView/$ {id}") // you can accept articleView/aaa. do, articleView/xxx. do...
Public String getArticle (@ PathVariable String id, HttpServletRequest request ){
Request. setAttribute ("article", service. find (Article. class, id ));
Return "articleView ";
}

}

It becomes more complex:

@ Controller
Public class ArticleController {

@ RequestMapping ("/articleList/$ {pageSize}/channel/*/category/$ {id }")

Public String getArticles (@ PathVariable Integer pageSize, @ PathVariable int id, HttpServletRequest request ){
Integer channelId = StringUtil. getParam (request. getRequestURI (), "channel /*/");
Request. setAttribute ("articles", service. findScroll (Article. class, pageSize, 50, "channel =? And category =? ", New Object [] {channelId, id }));

Return "articleList ";
}

}

It has been flexible to the URL address can be customized at will.

View parsing mechanism based on content negotiation:

What is the effect of the hosts file? The 3.0 version will bring incredible models.

HTTP Method conversion:

First look at the Html code on the front-end page

<Form: form method = "delete">
<P class = "submit"> <input type = "submit" value = "Delete Pet"/> </p>
</Form: form>

In the HTTP specification, form forms have only two methods: POST and GET, while 3.0 implements a filter, which can be converted to four methods: GET, PUT, POST, and DELETE. The Controller accepts the request:

@ Controller ("/deleteArticle ")
Public class ArticleController {

De style = "line-height: 28px;"> @ RequestMapping De> De style = "line-height: 28px;"> (method = RequestMethod. DELETE)
De> public String deleteArticle (@ PathVariable String id, HttpServletRequest request ){
Service. delete (Article. class, id );
Return "message ";
}

}
Version 3.0 only adds many new features in the MVC subset. If there is any change in the IoC, AOP, and other subsets, it can definitely be called the milestone version stated by the founder of Srping. The annotation list used by version 3.0 is as follows:

? @ Autowired
? @ Component
? @ Controller
? @ InitBinder
? @ ManagedAttribute
? @ ManagedOperation
? @ ManagedOperationParameters
? @ ManagedOperationParameter
? @ ManagedResource
? @ PathVariable
? @ PostConstruct
? @ PreDestroy
? @ Repository
? @ RequestMapping
? @ Required
? @ Resource
? @ Service
? @ Transactional
At present, the Spring 3 version has reached M2. It should be the final official version after the M3 is completed. I think it will come soon, according to the founder of Spring, Rod. johnson predicted that Spring + Tomcat will be dominant in J2EE applications in the future, and I would not dare to comment on it if it is controversial. However, after Oracle acquired Sun, the Java Community would be unknown, it seems rod. johnson was also nervous about the purchase case, because Oracle does not have the precedent of open technology as IBM, Sun's first negotiator (it can be recalled that the compatibility wave of IBM's early motherboard bus opening has not been eliminated so far ). At present, it is still slow to digest new things in China. I went to The Book City and read the Spring 2.5 documents. Many enterprises are developing with Struts1.x. Although this may cause a lot of controversy, the demise of the Struts1 era is only a matter of time. Although Struts2 has improved a lot, I think there are still many shortcomings compared with Spring MVC, especially the Abnormal Intrusion mode, let's see what it does with standard servlet components such as HttpServletRequest, HttpSession, and HttpServletResponse? In the open-source era, at least I don't want to accept the abnormal rules.

I used the Struts1.x Framework earlier. It works with a set of own ActionForm, which increases the programming workload. Although I can use my own Pojo as a work, I have no knowledge of the BeanUtil underlying tools) for developers, the type matching is very complicated. In fact, as early as 1.x, Spring MVC completely uses its own pojo to fill the corresponding form. Coupled with the Attribute Editor, it can solve the type conversion problem and fully implement the design mode driven by the domain model. Since the controller of the MVC layer is also the Bean of the Spring container, it is very easy to control and expand the entire project. At the same time, we also commented on Struts2, which shows the advantages of Spring MVC in various MVC frameworks. Mr. Rod Johnson is a master of design patterns. An excellent framework brings us far more than development efficiency, as well as more advanced development models and ideas...

The author has a superficial understanding of the Spring framework and will often talk about it in his blog later.

Zhang Ji (zhangjihao@sohu.com), please advise!

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.