Springmvc Getting Started screenshot

Source: Internet
Author: User
Tags lenovo

----------------------------------------------------

Configuring the front-end controller in Web. XML (a servlet class provided by the system can only be configured without programmer development)

--------------------------------------------------------------

------------------------

Non-Annotated adapter

Use a adapter to perform the handler that implements the Controller interface

 PackageOrg.ssm.controller;Importjava.util.ArrayList;Importjava.util.List;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.web.servlet.ModelAndView;ImportOrg.springframework.web.servlet.mvc.Controller;ImportOrg.ssm.po.Items;//processor that implements the Controller interface Public classItemsController1Implementscontroller{ PublicModelandview handlerequest (httpservletrequest arg0, httpservletresponse arg1)throwsException {//TODO auto-generated Method Stub//Call Service Lookup database, query commodity list, use static data simulation hereList<items> itemslist =NewArraylist<items>(); //populating the list with static dataItems items_1=NewItems (); Items_1.setname ("Lenovo Notebook");        Items_1.setprice (6000F); Items_1.setdetail ("ThinkPad T430 Lenovo notebook computer! "); Items items_2=NewItems (); Items_2.setname ("Apple Phone");        Items_2.setprice (5000F); Items_2.setdetail ("Iphone6 Apple phone!" ");        Itemslist.add (Items_1);                Itemslist.add (items_2); //This method needs to return Modelandview so createModelandview modelandview=NewModelandview (); //This method of nine is equivalent to Request.setattributeModelandview.addobject ("Itemslist", itemslist); //Specify a ViewModelandview.setviewname ("/web-inf/jsp/items/itemslist.jsp"); returnModelandview; }    }

Use the B adapter to perform the handler that implements the Httprequesthandler interface

 PackageOrg.ssm.controller;Importjava.io.IOException;Importjava.util.ArrayList;Importjava.util.List;Importjavax.servlet.ServletException;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.web.HttpRequestHandler;ImportOrg.ssm.po.Items; Public classItemsController2Implementshttprequesthandler{//The adapter class that corresponds to this handler is Org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter     Public voidHandleRequest (httpservletrequest arg0, httpservletresponse arg1)throwsservletexception, IOException {//TODO auto-generated Method Stub//Call Service Lookup database, query commodity list, use static data simulation hereList<items> itemslist =NewArraylist<items>(); //populating the list with static dataItems items_1=NewItems (); Items_1.setname ("Lenovo Notebook");        Items_1.setprice (6000F); Items_1.setdetail ("ThinkPad T430 Lenovo notebook computer! "); Items items_2=NewItems (); Items_2.setname ("Apple Phone");        Items_2.setprice (5000F); Items_2.setdetail ("Iphone6 Apple phone!" ");        Itemslist.add (Items_1);        Itemslist.add (items_2); Arg0.setattribute ("Itemslist", itemslist); //early Servlet program forwarding operationsArg0.getrequestdispatcher ("/web-inf/jsp/items/itemslist.jsp"). Forward (arg0, arg1); //which adapter can be used, but this adapter can format the response data through the response object            }}

----------

JSP page

--------------------

Non-annotation Mapper

-----------------------------

View Resolver

-F---------------------

Annotation Mapper, adapter

------------------------------------

Handler developed using annotations

 PackageOrg.ssm.controller;Importjava.util.ArrayList;Importjava.util.List;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.servlet.ModelAndView;ImportOrg.ssm.po.Items;//handler developed with annotations does not need to implement interfaces this annotation is the same as the package imported by the spring stage when the action object is injected into the bean container.//you know, this is where you inject the headler into the spring container.@Controller//using annotations to identify this is a handler Public classItemsController3 {//here the side can write any number of methods without prior implementation of the specified interface can only overwrite the limitations of the specified interface//using annotations to identify the path to be accessed by the map path the recommended mapping path is the same as the class name, and the action does not write.//@RequestMapping implement a map of the Query1 method one method corresponds to a URL@RequestMapping ("/query1.action")     PublicModelandview Query1 ()throwsexception{//Call Service Lookup database, query commodity list, use static data simulation hereList<items> itemslist =NewArraylist<items>(); //populating the list with static dataItems items_1=NewItems (); Items_1.setname ("Lenovo Notebook");        Items_1.setprice (6000F); Items_1.setdetail ("ThinkPad T430 Lenovo notebook computer! "); Items items_2=NewItems (); Items_2.setname ("Apple Phone");        Items_2.setprice (5000F); Items_2.setdetail ("Iphone6 Apple phone!" ");        Itemslist.add (Items_1);        Itemslist.add (items_2); //Create ModelandviewModelandview modelandview=NewModelandview ();        Modelandview.addobject (itemslist); //Specify a ViewModelandview.setviewname ("/web-inf/jsp/items/itemslist.jsp"); returnModelandview; }}

The handler is then configured in the configuration file to recommend the use of the annotation scan method ( map the URL of the map in the @requestmapping that identifies the @requestmapping in the tag @controller class) )

Otherwise, a handler class needs to configure a bean

--------------------------------

Prefix and suffix configuration for view resolver

NOTE: If you configure the prefix and suffix then you cannot specify the full path when handler sets the view path, except for one handler

You cannot specify the full path of the handler:1. Implements the Controller interface and the implementation of the 2. Annotation Method Handler

1.

2.

The path is:

After you configure the prefix and suffix, you still need to specify the handler of the full path: implements the handler of the Httprequessthandler interface

The path is still:

----------------------------

Springmvc Getting Started

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.