Spring MVC construction and configuration details

Now the mainstream web MVC framework is spring MVC in addition to struts. Therefore, this is also the mainstream framework that a programmer needs to master. With more choices of frameworks, there are naturally more feasible schemes to deal with changing needs and businesses. However, if you want to flexibly use spring MVC to deal with most web development, you must master its configuration and principle.

1、 Construction of spring MVC environment: (spring 2.5.6 + hibernate 3.2.0)

1. Jar package import

Spring 2.5. 6:spring. jar、spring-webmvc. jar、commons-logging. jar、cglib-nodep-2.1_ 3.jar

Hibernate 3.6. 8:hibernate3. jar、hibernate-jpa-2.0-api-1.0. 1.Final. jar、antlr-2.7. 6.jar、commons-collections-3.1、dom4j-1.6. 1.jar、javassist-3.12. 0.GA. jar、jta-1.1. jar、slf4j-api-1.6. 1.jar、slf4j-nop-1.6. 4. Jar. Driver jar package of corresponding database

Spring MVC is an MVC framework based on dispatcherservlet. Each request is accessed first by dispatcherservlet. Dispatcherservlet is responsible for forwarding each request to the corresponding handler. After processing, the handler returns the corresponding view and model. The returned view and model can not be specified, That is, you can return only model or view or none.

Dispatcherservlet inherits from httpservlet. Since spring MVC is based on dispatcherservlet, let's configure dispatcherservlet first so that it can manage the content we want it to manage. Httpservlet is on the web Declared in the XML file.

spring-servlet. XML configuration

Spring servlet is named because of the above web The value of < servlet name > tag in XML is spring (< servlet name > spring < / servlet name >), and the file name of spring-servlet.xml is formed by adding "- servlet" suffix. If it is changed to springmvc, the corresponding file name is springmvc-servlet.xml.

Dispatcherservlet will use some special beans to process the request and generate the corresponding view return.

As for the return of views, the controller is only responsible for passing back a value, and then the view returned is controlled by the view parser. The commonly used view parser in JSP is internalresourceviewresolver, which requires a prefix and a suffix

In the above view parser, if the controller returns blog / index, the view parsed by the view parser is / JSP / blog / index jsp。

Mainly about controller

A class marked with @ controller is controller

The above four method examples are that a controller contains different request URLs, which can also be accessed by one URL. Different access methods can be distinguished by URL parameters. The code is as follows:

In fact, on the class, requestmapping can be regarded as the parent request URL, while on the method, requestmapping can be regarded as the child request URL. The parent-child request URL will eventually match the page request URL. Therefore, requestmapping can also be written as follows:

The commonly used annotations in spring MVC include @ pathvariable, @ requestparam, @ pathvariable marked on the parameters of the method. The marked parameters can be used to pass values through the request path. See the following example

In this example, the blogid is marked as the request path variable by @ pathvariable. If the request is / blog / comment / 1 Do means that the value of blogid is 1 Similarly, @ requestparam is also used to pass values to parameters, but it takes values from the parameter of request, which is equivalent to request Getparameter ("parameter name") method.

In the controller method, if you need web elements HttpServletRequest, httpservletresponse and httpsession, you only need to give the method a corresponding parameter, and then spring MVC will automatically pass the value to it during access. However, it should be noted that when you pass in the session, if you call the session for the first time, an error will be reported, Because the session has not been generated at this time.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>