SPRINGMVC Series one of the HelloWorld

Source: Internet
Author: User
Tags lenovo

1,SPRINGMVC Frame

SPRINGMVC is a module of the spring Framework, and SPRINGMVC and spring do not need to be consolidated through an intermediate integration layer.

SPRINGMVC is an MVC-based web framework.

2,SPRINGMVC Frame

 

The steps are explained as follows:

First step: initiating a request to the front controller (dispatcherservlet)

Step Two: Front controller request handlermapping find Handler

            You can find the

Based on the XML configuration and annotations The third step: Processor Mapper handlermapping return handler

to the front-end controller fourth step: The front controller calls the processor adapter to execute the handler

Fifth Step: Processor adapter to execute handler

Sixth step: Handler execution completes to adapter return Modelandview

Seventh Step: Processor Adapter returns to front controller Modelandview

             Modelandview is an underlying object of the SPRINGMVC framework, including model and view

Eighth step: Front controller Requests View resolver for view resolution

Resolves to a true view (JSP) based on the logical view name

Nineth Step: View resolver return view to Front controller

Step Tenth: Front controller for view rendering

View rendering fills the model data (in the Modelandview object) to the request field

11th Step: Front controller responds to user results

Component:

1. Front Controller dispatcherservlet (no programmer development required)

function receive request, response result, equivalent to transponder, CPU.

With the Dispatcherservlet, the coupling between the other components is reduced.

2, Processor mapper handlermapping (no programmer development required)

Function: Find handler based on the URL requested

3. Processor Adapter Handleradapter

Function: Execute handler According to the specific rules (Handleradapter requirements)

4. Processor handler (programmer development Required)

Note: When writing handler, follow Handleradapter's instructions so that the adapter can go to the correct execution handler

5. View Resolver views Resolver (no programmer development required)

Function: Perform a view resolution, resolve to a real view based on the logical View name (view)

6. Views view (requires programmer to develop JSP)

View is an interface that implements classes that support different view types (JSP, Freemarker, pdf ...). )

3, Environment preparation

(1) Jar package required

(2) Configuring the front-end controller-Configuring in Web. xml

 <servlet>  <servlet-name>Springmvc</servlet-name>  <servlet-class>Org.springframework.web.servlet.DispatcherServlet</servlet-class>  <!--contextconfiglocation Configure SPRINGMVC loaded configuration files (Configure processor mapper, adapters, and so on) if Contextconfiglocation is not configured, the default load is/web-inf/ Servlet name-serlvet.xml (springmvc-servlet.xml)--  <Init-param>  <param-name>Contextconfiglocation</param-name>  <param-value>Classpath:springmvc.xml</param-value>  </Init-param>  </servlet>    <servlet-Mapping>  <servlet-name>Springmvc</servlet-name>  

The first type: *.action, accessed with. Action ends resolved by Dispatcherservlet
The second:/, so the address of the access is resolved by Dispatcherservlet, the resolution of the static file needs to be configured not to let Dispatcherservlet parse
Use this method to implement restful style URLs
The Third Kind:/*, this configuration is not correct, using this configuration, eventually to forward to a JSP page,
will still be interpreted by Dispatcherservlet JSP address, can not find handler based on JSP page, will error.
-
  < URL-pattern>*.action</url-pattern>  < /servlet-mapping>

(3) Configuring the processor adapter

Configure the processor adapter in the Springmvc.xml under Classpath.

<!--processor Adapter All processor adapters implement Handleradapter interface -- <  Class="Org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> 

By viewing the original code:

This adapter can perform the handler that implements the Controller interface.

4, development handle

Note: The Controller interface needs to be implemented before it can be executed by the Org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter adapter.

 PackageCn.itcast.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;ImportCn.itcast.ssm.po.Items;/** * * <p>Title:ItemsController1</p> * <p>description: Processor to implement controller interface </p> * <p> Company:www.itcast.com</p> * @author wisdom. 燕青 * @date2015-4-13 a.m. 10:46:17 * @version 1.0 */ Public classItemsController1ImplementsController { PublicModelandview HandleRequest (httpservletrequest request,httpservletresponse response)throwsException {//Call service to find 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);//Return to ModelandviewModelandview Modelandview =NewModelandview ();//equivalent to request setattribut, fetching data through Itemslist in JSP pagesModelandview.addobject ("itemslist", itemslist);//Specify ViewModelandview.setviewname ("/web-inf/jsp/items/itemslist.jsp");returnModelandview;}}

5, view development

Note: This is the foreground page development.

<%@ page language= "Java"Contenttype="text/html; Charset=utf-8"Pageencoding="UTF-8"%><%@ taglib uri="Http://java.sun.com/jsp/jstl/core"Prefix="C"%><%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"Prefix="FMT"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD">Content-type"Content="text/html; Charset=utf-8"><title> Check Product list </title>${pagecontext.request.contextpath}/item/queryitem.action"Method="Post"> Search conditions: <table width="100%"Border=1><tr><td><input type="Submit"Value="Enquiry"/></td></tr></table> Product List: <table width="100%"Border=1><tr><td> Product name </td><td> commodity price </td><td> production date </td><td> Product Description </td><td> operation </td></tr><c:foreach items= "${itemslist}"var="Item"><tr><td>${item.name}</td><td>${item.price}</td><td><fmt:formatdate value= "${item.createtime}"Pattern="YYYY-MM-DD HH:mm:ss"/></td><td>${item.detail}</td><td><a href="${pagecontext.request.contextpath}/item/edititem.action?id=${item.id}"> Modify </a></td></tr></c:forEach></table></form></body>
 

6, Configuration Handler

The handler will be written to load in the spring container.

< Bean id="ItemsController1"name="/queryitems_test.action"class=" Cn.itcast.ssm.controller.ItemsController1 "/>

7, configuring the processor Mapper

Configure the processor mapper in the Springmvc.xml under Classpath.

<!--processor Mapper looks up the name of the bean as a URL and needs to specify Beanname (that is, URL) when configuring handler to implement Handlermapping interfaces for all mappers. --<class=" Org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping "/>

8, configuring the View resolver

You need to configure the view parser to parse the JSP.

< Bean class="Org.springframework.web.servlet.view.InternalResourceViewResolver">  </Bean>

9, deployment debugging

Next, you can run debugging in Tomcat.

Source code, extract code: a671

SPRINGMVC Series one of the HelloWorld

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.