SPRINGMVC Study Notes (9)-SPRINGMVC Integrated MyBatis Controller
- SPRINGMVC Study Notes 9-SPRINGMVC integrated MyBatis Controller
- Springmvcxml
- Configure Webxml
- Writing a controller is handler
- Writing JSPs
This article describes how to configure the Springmvc configuration file and Web. XML, and how to write controller,jsp
Springmvc.xml
Under resources/spring files, create a springmvc.xml file, configure the Processor mapper, adapter, and view resolver.
<Beans xmlns="Http://www.springframework.org/schema/beans" Xmlns:context="Http://www.springframework.org/schema/context" Xmlns:mvc="Http://www.springframework.org/schema/mvc" Xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-4.0.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-4.0.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-4.0.xsd "> <!--for annotations handler can be individually configured in real development plus you use components to scan- <!--can scan controller, service 、... Here let's scan controller, specify the controller's package-- <context:component-scan base-package="Com.iot.learnssm.firstssm.controller" ></context:component-scan> <!--use Mvc:annotation-driven instead of the above two annotation mapper and annotation adaptation configuration Mvc:annotation-driven default load a lot of parameter binding methods, such as the JSON conversion parser is loaded by default, if you use M Vc:annotation-driven does not configure the above requestmappinghandlermapping and requestmappinghandleradapter using MVC when actually developing: Annotation-driven -- <mvc:annotation-driven></mvc:annotation-driven> <!--View Resolver to parse the JSP, by default use Jstl,classpath to have JSTL package-- <Bean class="Org.springframework.web.servlet.view.InternalResourceViewResolver" > <!--Configure JSP path prefixes-- < property name="prefix" value="/web-inf/jsp/"/> <!--Configure the suffix of the JSP path-- < property name="suffix" value=". jsp"/> </Bean></Beans>
Configure Web. xml
Refer to the Getting Started program, 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" xsi:schemalocation="Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3 _0.xsd "id=" webapp_id " version=" 3.0 "> <display-name>Firstssm</display-name> <!--loading Spring containers -- <context-param> <param-name>Contextconfiglocation</param-name> <param-value>Web-inf/classes/spring/applicationcontext-*.xml</param-value> <!--<param-value>classpath:spring/applicationContext-*.xml</param-value>--> </context-param> <listener> <listener-class>Org.springframework.web.context.ContextLoaderListener</listener-class> </listener><!--SPRINGMVC Front Controller -- <servlet> <servlet-name>Springmvc</servlet-name> <servlet-class>Org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--contextconfiglocation configuration Springmvc loaded configuration file (Configuration Processor mapper, adapter, etc.) if not configured, the Web-inf/servlet name is loaded by default-servlet (SPRINGMVC -servlet.xml) -- <init-param> <param-name>Contextconfiglocation</param-name> <param-value>Classpath:spring/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Springmvc</servlet-name> <!--The first type: *.action, access to. Action three endings, parsed by dispatcherservlet the second:/, all access addresses are parsed by Dispatcherservlet, the parsing of the static file needs The configuration does not allow Dispatcherservlet to parse, use this approach and implement the RESTful style of the URL of the third:/*, this configuration is incorrect, using this configuration, the final forwarding to a JSP page, will still be by dispatcher Servlet parsing JSP address, cannot find handler based on JSP page, will error-- <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>
This file has two functions:
- Configuring the front-end controller (
DispatcherServlet )
- Load Spring container: Add Spring container listener, load spring container, use
spring/ config file under wildcard Fugazai
- Applicationcontext-dao.xml
- Applicationcontext-service.xml
- Applicationcontext-transaction.xml
Write a controller (that is, handler)
PackageCom.iot.learnssm.firstssm.controller;ImportCom.iot.learnssm.firstssm.po.Items;ImportCom.iot.learnssm.firstssm.po.ItemsCustom;ImportCom.iot.learnssm.firstssm.service.ItemsService;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.servlet.ModelAndView;ImportJava.util.ArrayList;ImportJava.util.List;/** * Created by Brian on 2016/3/2. * *//Use @controller to identify it as a controller@Controller//In order to classify the URL, the root path can be defined here, and the final access URL is the root path + sub-path//For example: Product list:/items/queryitems.action Public class itemscontroller { @Autowired PrivateItemsservice Itemsservice;//Commodity query list @RequestMapping("/queryitems")//Implement mapping of Queryitems methods and URLs, one method corresponds to a URL //General recommendation to write URLs and methods as PublicModelandviewQueryitems()throwsexception{//Call service Lookup database, query product listlist<itemscustom> itemslist = Itemsservice.finditemslist (NULL);//Return to ModelandviewModelandview Modelandview =NewModelandview ();//equivalent to the SetAttribute method of request, the data is obtained through itemslist in the JSP pageModelandview.addobject ("Itemslist", itemslist);//Specify View ///Bottom path, if the path prefix and suffix of the JSP are configured in the View parser, modify to Items/itemslist //modelandview.setviewname ("/web-inf/jsp/items/itemslist.jsp"); ///The path configuration below can not specify the prefix and suffix of the JSP path in the programModelandview.setviewname ("Items/itemslist");returnModelandview; }}
Writing JSPs
The server path isWEB-INF/jsp/items/itemsList.jsp
<%@ 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" ><html><head><meta http-equiv="Content-type" Content="text/html; Charset=utf-8 "><title>Check Product List</title></head><body> <form Action="${pagecontext.request.contextpath}/item/queryitem.action" Method="POST">Query criteria:<table Width="100%" Border=1><tr><TD><input type="Submit" value="Query"/></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></html>
Author @brianway More articles: personal website | CSDN | Oschina
SPRINGMVC Study Notes (9)-SPRINGMVC Integrated MyBatis Controller