SPRINGMVC Road Summary (ii)

Source: Internet
Author: User

Rome cold Day. The study of technology has to be followed in particular by such a rationale. In the "Springmvc Road Summary (a)", a clear summary of the SPRINGMVC of the introductory case, for this kind of introductory demo, sorting out the routines, to organize the thoughts is the most important. It is possible to learn from the case that the characteristics and application of SPRINGMVC in frame technology are the main functions of the first part.

As can be seen from the previous section, only one method can be written in each controller, which is the method handlerequest to implement the controller. However, for this form of distribution and configuration, once the function of the project increased, it is difficult to accurately locate the request of the method, and maintenance is very cumbersome, so the actual application characteristics are not obvious. This part of today is the main solution to this problem. 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] .

So, here's the problem! How do I write multiple methods in a controller?

In fact, in the SPRINGMVC has been written, only need to inherit the Multiactioncontroller, this specific class in the Org.springframework.web.servlet.mvc.multiaction directory. In spring's jar package, this class can be found clearly.

Or from the case, to achieve such a multi-method controller, the basic principle and the first part of the same case. Just need to simply modify some configuration files and re-write the controller on the line.

The configuration of the Springmvc-servlet.xml is:

<BeanID= "Parammethodresolver"class= "Org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">       < Propertyname= "ParamName"value= "Action"></ Property>    </Bean>    <Beanname= "/test"class= "Com.red.controller.MultiController">       < Propertyname= "Methodnameresolver">           <refBean= "Parammethodresolver" />       </ Property>    </Bean>

The ID of parammethodresolver in the bean above is the focus of configuring a controller class to implement multiple methods.

The source code for inheriting Multiactioncontroller in the controller is as follows:

 PackageCom.red.controller;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.web.servlet.ModelAndView;ImportOrg.springframework.web.servlet.mvc.multiaction.MultiActionController; Public classMulticontrollerextendsMultiactioncontroller { PublicModelandview First (httpservletrequest request, httpservletresponse response) { String Method= "First"; System.out.println ("Using first Method::" +method); return NewModelandview ("/test", "param", method); }          PublicModelandview Second (httpservletrequest request, httpservletresponse response) { String Method= "Second"; System.out.println ("Using Second Method::" +method); return NewModelandview ("/test", "param", method); }}

Enter address in browser: Http://localhost:8080/20150507_SpringMVC01/test?action=First

The implementation results are as follows:

    

Enter address in browser: Http://localhost:8080/20150507_SpringMVC01/test?action=Second

The implementation results are as follows:

    

Results from the console output:

In fact, in the development process encountered the most problems, often not these technical issues. Need to pay attention to the details of the place, a lot of people in the first contact with SPRINGMVC, pictures, JS and CSS class files are basically a problem, on the page can not be normal in the browser to display the ideal effect. However, this is actually because there are some fewer configurations in the configuration file, or the configuration is not configured correctly.

That is, the following source code needs to be added to the springmvc-servlet.xml:

<location= "/img/"  mapping= "/img/**"/>

Location is the actual storage path of the file, mapping refers to the map picture folder. For other files such as CSS and JS class, the same method can be used.

The actual results are as follows:

===================================================================

The implementation is configured in this and the previous sections in the form of XML, but XML is not the best way to implement SPINGMVC, in fact annotations are its characteristics. It is clear to see the advantages of the SPRINGMVC framework, simple, and applicable, if implemented using annotations. The form of annotations will be presented in a subsequent summary of the case.

Demo download

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 (ii)

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.