1 Starter Programs 1 . 1 Requirements for Product List query 1 . 2 The jar package required uses SPRING3. 2.0(with SPRINGWEBMVC module)
1the. 1 Front-end controller is configured in Web. xml:<?xml version="1.0"encoding="UTF-8"? ><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="Http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemalocation="Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"Id="webapp_id"version="2.5"> <display-name>springfirst1110</display-name> <!--front-end controllers--<servlet> <servlet-nam E>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <!--load SPRINGMVC configuration--<init-param> <param-name>contextconfiglocation</param- Name> <!--address of the configuration file if you do not configure Contextconfiglocation, the default lookup for the profile name Classpath under: servlet name+"-serlvet.xml"namely: springmvc-Serlvet.xml-<param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!--can be configured/, this project all requests all by SPRINGMVC parsing, this kind of way can realize the restful way, needs the special processing to the static file parsing cannot by the Springmvc parse. can be configured*.do or *. Action, the URL extension for all requests is parsed by the. Do or. Action, which is commonly used by SPRINGMVC. Not to/*, if you configure/*, the return JSP is also parsed by SPRINGMVC, which is not correct. -<url-pattern>*.action</url-pattern> </servlet-mapping> <welcome-file-list> < Welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> < Welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> < Welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </ Welcome-file-list></web-app> 1.2springmvc.xml configuration files similar to spring are configured in Springmvc.xml with the SPRINGMVC architecture three components (processor mapper handlermapping, adapter handleradapter, View Resolver Viewresolver) 1.3 Engineering structure
1.1The processor mapper is configured in Springmvc.xml: beannameurlhandlermapping: Matches the spring container bean based on the request URL (xxxx.action) Name to find the corresponding bean (program-written handler)<!--lookup based on the Bean's name handler the URL of the action in the bean's name-<Beanclass="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>All processor mappers implement the Handlermapping interface. 1. 2 Processor Adapter in Springmvc.xml configuration:<beanclass="Org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>all adapters are implemented with the Handleradapter interface. Program writing handler (similar to action) is written according to the requirements of the adapter. Simplecontrollerhandleradapter Adapter Requirements: You know through the Supports method which interface handler must implement:
1 . 1Handler authoring requires the Controller interface to be implemented:
ItemController1: Public Abstract classItemController1 implements Controller {@Override PublicModelandview HandleRequest (httpservletrequest request, httpservletresponse response) throws Exception { //use static data to display a list of commodity information on a JSP page//Product ListList<items> itemslist =NewArraylist<items>(); Items Items_1=NewItems (); Items_1.setname ("Lenovo Notebook"); Items_1.setprice (6000F); Items_1.setcreatetime (NewDate ()); 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); Modelandview Modelandview=NewModelandview (); //populating data into the request//Request.setattribute ("Itemslist", itemslist);Modelandview.addobject ("itemslist", itemslist); //specify a forwarded JSP pageModelandview.setviewname ("/web-inf/jsp/itemslist.jsp"); returnModelandview; }}1. 1 configuration handler in Springmvc.xml configuration handler by spring management handler. <!--configuration handler because the Beannameurlhandlermapping processing mapper is used, name is configured as a URL--<bean id="ItemController1"Name="/itemlist.action" class="Cn.itcast.springmvc.first.ItemController1"/>1. 2 Configure the View resolver to configure view resolution to parse the JSP view:<!--Configure the view resolver to add JSTL packages to Classpath-<!--viewresolver--<Beanclass="Org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"Value="/web-inf/jsp/"/> <property name="suffix"Value=". JSP"/> </bean>1. 3 Project Deployment access: http://localhost:8080/springfrist1110/itemlist.action
page:<c:foreach items="${itemslist}" var="Item"><tr> <td>${item.name}</td> <td>${item.price}</td> <td><fmt:formatdat E 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>
SpringMVC2 1 Starter Program