Springmvc heard for a long time, as early as last year was asked by seniors about SPRINGMVC basic knowledge, then did not care. The main task is not to use the technology about SPRINGMVC, so free from the lack of time and energy to excuse the lack of contact and learning Springmvc.
I thought that Springmvc and spring were part of the MVC framework, and I said that part of MVC was not replacing the control part of struts, but the same kind of factory management as spring. Now found that the technology will be fool, really feel the technology this thing, the more learning will feel the need to learn a lot of things. Calm down, with an honest heart to face the unknown, the courage to challenge, good study.
The structure and description of the MVC framework are not the focus of this article. I believe that since I saw SPRINGMVC, I certainly have a certain understanding of the MVC framework. Therefore, the basics of the MVC framework will not be mentioned at this time. I only summed up their own learning SPRINGMVC in the process of a little bit, to motivate their own struggle, bearing in mind their growth, for the novice friends to provide a quick start case. This blog post for my study summary, please respect the fruits of labor. Welcome reprint, please keep the source of the blog:http://www.cnblogs.com/itred ; e-mail: [email protected] .
A theory that must be understood
The Spring Web MVC Framework is designed around Dispatcherservlet, which assigns requests to handlers with configurable handler mappings, view resolution, local language, topic resolution, and upload file support. The default handler is based on the controller interface and requestmapping, providing a wide range of flexible processing methods. Spring provides a controller hierarchy that can derive subclasses.
In fact, about the benefits of SPRINGMVC, I think there is no need to show too much, so to speak. SPRINGMVC can achieve the control function in struts, SPRINGMVC is the use of efficient servlet, although there is no struts in the filter, but it can also implement its own filtering function, and in the three frameworks now more applied to enterprise-class applications, More and more enterprises directly choose SPRINGMVC as the controller, because it no longer need to add additional jar package, if using struts, need to add some other packages, and SPRINGMVC can be shared with spring jar package, do not know this is not right, but easy to understand.
is directly taken from the API documentation in the Spring jar package, that is: official information for SPRINGMVC, I think there is no version can be better than this.
I do not write nonsense, the official theory moved to the line, just to get started to do some simple lessons. This makes it easier to understand the code behind.
Case walk up
A Demo1
- Create a new Web project, download the full package of spring, unzip and copy the jar package from the Libs directory to the Lib directory, and build the path; Because this is just a jar package, it is not necessary to copy the doc and source packages. You can also refer to the documentation in docs under the spring package, the version that I tested is 3.2;JDK version 1.6
- Because the SPRINGMVC is around the dispatcherservlet, you need to add this to Web. XML, the project's Web. XML is added, the source code is as follows:
<?XML version= "1.0" encoding= "UTF-8"?><Web-appversion= "2.5"xmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <Servlet-name>Springmvc</Servlet-name> <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class> <Load-on-startup>1</Load-on-startup> </servlet> <servlet-mapping> <Servlet-name>Springmvc</Servlet-name> <Url-pattern>/</Url-pattern> </servlet-mapping> <welcome-file-list> <Welcome-file>index.jsp</Welcome-file> </welcome-file-list></Web-app>
Note: the values in Servlet-name here must be highly valued, because the values here need to be matched with another configuration file for Springmvc. For example, here the Servlet-name is SPRINGMVC, the new configuration file must be the format: Servletname-servlet.xml, that is, the name is: Springmvc-servlet.xml.
3. Because the default mode is used, the file must be placed in the Web-inf directory, that is, a new XML file, note that the name needs to be the same as mentioned above, the source code is as follows:
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans- 3.0.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-c Ontext.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3. 0.xsd "> <Context:component-scanBase-package= "Org.springframework.samples.petclinic.web" /> <Beanname= "/he"class= "Com.red.controller.HelloController"></Bean> <!--name= "/he" This is the access path in the browser-- <BeanID= "Viewresolver"class= "Org.springframework.web.servlet.view.InternalResourceViewResolver"> < Propertyname= "prefix"value="/"></ Property> <!--This is the directory Convention -- < Propertyname= "suffix"value= ". jsp"></ Property> </Bean></Beans>
Here, the SPRINGMVC configuration file is fully configured, but if you want to test whether such an environment is appropriate for our development environment, we also need to encode the controller.
4. As a first entry procedure, it is clear that the process of understanding is far more useful than implementing complex logic. In the controller writing here, we just need to implement an interface method on the line. This interface is the controller, the method of implementation is HandleRequest, here will be exposed to a new thing----modelandview. This is the return of a view, but also can pass parameters, in the DEMO2 will be implemented in detail. Here is just the implementation of jumping to a hello.jsp page.
In the configuration here, you will not see the. JSP follows the page, because it is already isolated in the configuration file and the page is prefixed with. JSP, so the view here is not limited to JSP.
The controller source code is as follows:
PackageCom.red.controller;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.web.servlet.ModelAndView;ImportOrg.springframework.web.servlet.mvc.Controller; Public classHellocontrollerImplementsController { PublicModelandview HandleRequest (httpservletrequest request, httpservletresponse response)throwsException {System.out.println ("The first example about SPRINGMVC."); return NewModelandview ("/hello"); Actually jumps to the hello.jsp page}}
Here, the case has been realized. Run the program before you type in the browser: http://localhost:8080/20150505_SpringMVC01/he
Console output:
The display in the browser:
Two Demo2
Case two on the basis of the case of a further modification, the configuration file independent, because in the actual project development process, will not be some special configuration files for default processing, it seems very disorderly, maintenance also has some difficulties.
- In the SRC directory, the new package Com/red/config
- Copy Springmvc-servlet.xml to this directory
- Add the following code to the servlet in Web. xml:
< Init-param > < Param-name >contextconfiglocation</param-name> < Param-value>classpath*:com/red/config/springmvc-servlet.xml</ Param-value> </init-param>
After this configuration, you can specify the config file arbitrarily. Do it according to your actual project requirements.
4. After the configuration file shift, the next thing to do is to pass the parameters to the page, that is, from the controller to get parameters to the page upload. Look at the source code will know that there are a lot of modelandview construction methods, here to post some.
Parameters can be selected according to the need, there is a 7th. The parameter viewname represents a JSP page, modelname represents the parameter name, the page is taken, and Modelobject is the value passed.
The source code is as follows:
PackageCom.red.controller;ImportJava.util.HashMap;ImportJava.util.Map;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.web.servlet.ModelAndView;ImportOrg.springframework.web.servlet.mvc.Controller; Public classHellocontrollerImplementsController { PublicModelandview HandleRequest (httpservletrequest request, httpservletresponse response)throwsException {System.out.println ("This was the second example about SPRINGMVC."); String Firstparam= "First string argument"; Map<string, object> map =NewHashmap<string, object>(); Map.put ("Prm1", "first in map"); Map.put ("Prm2", "second in map"); Map.put ("Prm3", "third in map"); //return new Modelandview ("/hello", "PRM", firstparam);//String with return NewModelandview ("/hello", "PRM", map); } }
The implementation results are as follows:
Summary
Here, the first part of the SPRINGMVC primer is complete. As simple as the introduction of struts, for technology this thing, I always believe that the introduction is not difficult, it is rare to be able to calm down to analyze, to think about the reasons for doing so, the benefits of doing so, why will choose this technology to achieve. Therefore, thinking is far more important than copy and copy!
This article refers to the source code and so on for novice friends to learn:
Demo1 Demo2 Spring Full jar package (access password d498)
thanks to the struggle of their own!
Email:[email protected] Personal Blog: http://itred.cnblogs.com Copyright Notice: This article is copyrighted by the author and the blog Park, Welcome to reprint, but please mark the article in a conspicuous position. I reserve all rights to be held accountable for his use without my written consent.
SPRINGMVC Road Summary (i)