Using SPRINGMVC in your project

Source: Internet
Author: User
Tags http request lenovo
What is Springmvc

SPRINGMVC is a module of the spring Framework, and SPRINGMVC and spring do not need to be consolidated through the middle of the entire layer, which is an MVC based web framework. the difference between SPRINGMVC and Struts2 Springmvc is based on method development, and Struts2 is based on class development. SPRINGMVC URLs and Controller methods are mapped, the mapping succeeds Springmvc generates a handler object (that is, controller), the object contains only the mapping method, and the form parameter is destroyed after the execution. SPRINGMVC can be developed in a single case, and it is recommended to use single case development, STRUTS2 can only be developed (STRUTS2 receive data through class member variables, data in multiple threads may be different, so it is not possible to use single case development). After the actual test, the struts2 speed is slow because the struts tag is used, so it is recommended that jstl be used when using struts2 for development. SPRINGMVC Framework Execution Process

Learn SPRINGMVC with an introductory procedure SPRINGMVC Operating Environment


Jar Package Download address (mybatis+spring (including SPRINGMVC) all jar packages):
http://download.csdn.net/detail/jinzili777/9480604 Configuring the Front-End controller

In the Web.xml file,

<!--SPRINGMVC Front-End controller--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class >org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--contextconfiglocation configuration sp RINGMVC loaded profile path (configuration mapper, adapter, etc.) if this property is not configured, the/web-inf/servlet name-servlet.xml is loaded by default (here is the value above <servlet-name>: Springmvc-servlet.xml)--> <init-param> &LT;PARAM-NAME&GT;CONTEXTCONFIGLOCATION&LT;/PARAM-NAME&G
        T <param-value>classpath:spring/springmvc.xml</param-value> </init-param> </servlet> <se Rvlet-mapping> <servlet-name>springmvc</servlet-name> <!--the first: *.action, access to *.action end of the dispatcherservlet resolves the second:/, all accesses are resolved by Dispatcherservlet, such as access to static resources (Js,css ...) Need to configure to not allow Dispatcherservlet parsing, this method can implement the Resturl style of the URL of the third:/*, this configuration is not correct, use this method, when we want to forward to a JSP page will also be resolved by the Dispatcherservlet, will Error--> <url-pattern>*.action</url-pattern> </servlet-mapping>
Non-annotated processor mapper and adapters

In the Springmvc.xml under the Classpath

processor adapters that do not use annotations

This method can only perform the handler that implements the Controller interface, below is a small demo

Development Handler

public class ItemsController1 implements Controller {@Override public modelandview handlerequest (httpservletreque
        St request, httpservletresponse Response) throws Exception {//Call service lookup database, query commodity list, here use static data simulation
        list<items> itemslist = new arraylist<items> ();
        Populate the list with static data items Items_1 = new items ();
        Items_1.setname ("Lenovo Notebook");
        Items_1.setprice (6000F); Items_1.setdetail ("ThinkPad T430 Lenovo notebook computer.")

        ");
        Items items_2 = new items ();
        Items_2.setname ("Apple mobile");
        Items_2.setprice (5000F); Items_2.setdetail ("Iphone6 Apple phone.")

        ");
        Itemslist.add (Items_1);

        Itemslist.add (items_2);
        return Modelandview Modelandview Modelandview = new Modelandview ();

        Equivalent to request Setattribut, in the JSP page through itemslist fetch data modelandview.addobject ("Itemslist", itemslist);
        Specifies the view modelandview.setviewname ("/web-inf/jsp/items/itemslist.jsp"); Return modElandview; }

Setviewname () method is forwarded to the JSP page, the page is no longer to repeat, in this JSP page can be taken to the request field in the Itemslist.
loading Handler in spring container

<bean name= "/queryitems.action" class= "Cn.jzl.ssm.controller.ItemsController1" ></bean>

to configure a processor mapper that does not use annotations

<!--to 
    find the name of the bean as a URL, you need to specify Beanname (that is, url)
 -->
<bean class= when configuring handler Org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping "/>

Configure the View parser

The <!--view parser 
        resolves the JSP by default using the prefix of the JSTL package
        jsp path and the suffix
    -->
    <bean class= "of the JSP path under Jstl,classpath Org.springframework.web.servlet.view.InternalResourceViewResolver ">
        <property name=" prefix "value="/ web-inf/jsp/"/>
        <property name=" suffix "value=". jsp "/>
    </bean>
Processor Mapper and adapter for annotations

Use before spring3.1
org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping annotation Mapper.

Use after spring3.1
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping annotation Mapper.

Use before spring3.1
Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter annotation Adapter.

Use after spring3.1
Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter annotation Adapter.

Configuring note Mapper and Adapters

<!--annotation mapper-->
    <bean class= " Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping "/>
    <!--annotation adapter-- >
    <bean class= "Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/ >

Development Annotation Handler

Using Controller to identify it is a controller
@Controller public
class Itemscontroller {
    @Resource
    private Itemsservice Itemsservice;
    @RequestMapping implementation maps the Queryitems method and URL, a method corresponding to a URL
    //general recommendation to write the URL and method as
    @RequestMapping ("/queryitems") Public
    Modelandview Queryitems (Itemsqueryvo itemsqueryvo) throws exception{/
        *
            business logic/
        List <ItemsCustom> itemslist = itemsservice.finditemslist (ITEMSQUERYVO);
        Modelandview Modelandview = new Modelandview ();
        Modelandview.addobject ("Itemslist", itemslist);
        Modelandview.setviewname ("Items/itemslist");
        Return Modelandview
    }
}

@controller annotation must be added, the action identification class is a handler processor.
@requestMapping annotations must be added to the function:
1, the URL and handler method mapping.
2, can narrow request mapping, set handler root path, URL is the root path + sub path request way
3, can limit the HTTP request method
After the mapping succeeds, the SPRINGMVC framework generates a handler object containing only one method that maps successfully.

loading Handler in the Spring container

<!--handler for annotations can be configured individually-->
<!--<bean class= "Cn.jzl.ssm.controller.ItemsController"/>-->
<!--But in development, it is recommended to use scanning-->
<context:component-scan base-package= "Cn.jzl.ssm.controller"/>

Configuration View parser method does not change using Mvc:annotation-driven

Configuring the Mapper and adapter
You can substitute the adapter and mapper for annotations in the second method using the following configuration

<mvc:annotation-driven></mvc:annotation-driven>

Development Annotation Handler

Consistent with the second method development approach

Configuration View parser method does not change Summary

In the process of learning and using SPRINGMVC, it is important to understand its execution process. In the actual use of the process will encounter a variety of problems, nor a blog or a video can introduce complete, so learning no shortcuts, only through a line of code accumulation, precipitation, knock a few lines of code, understanding will deepen a bit. Share.

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.