Springmvc learning notes (9)-springmvc integrates controller of mybatis

Source: Internet
Author: User

Springmvc learning notes (9)-springmvc integrates controller of mybatis
Springmvc learning notes (9)-springmvc integrates controller of mybatis

 

This article describes how to configure springmvc configuration files and web. xml, and how to write controller, jsp

Springmvc. xml

Inresources/springCreate the springmvc. xml file under the file and configure the processor er, adapter, and view parser.

<code class="language-xml hljs "><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">   <!--{cke_protected}{C}%3C!%2D%2D%20%E5%AF%B9%E4%BA%8E%E6%B3%A8%E8%A7%A3%E7%9A%84Handler%20%E5%8F%AF%E4%BB%A5%E5%8D%95%E4%B8%AA%E9%85%8D%E7%BD%AE%0A%20%20%20%20%E5%AE%9E%E9%99%85%E5%BC%80%E5%8F%91%E4%B8%AD%E5%8A%A0%E4%BD%A0%E4%BD%BF%E7%94%A8%E7%BB%84%E4%BB%B6%E6%89%AB%E6%8F%8F%0A%20%20%20%20%2D%2D%3E-->    <!--{cke_protected}{C}%3C!%2D%2D%20%E5%8F%AF%E4%BB%A5%E6%89%AB%E6%8F%8Fcontroller%E3%80%81service%E3%80%81...%0A%20%20%20%20%E8%BF%99%E9%87%8C%E8%AE%A9%E6%89%AB%E6%8F%8Fcontroller%EF%BC%8C%E6%8C%87%E5%AE%9Acontroller%E7%9A%84%E5%8C%85%0A%20%20%20%20%20%2D%2D%3E-->    <context:component-scan base-package="com.iot.learnssm.firstssm.controller"></context:component-scan>    <!--{cke_protected}{C}%3C!%2D%2D%20%E4%BD%BF%E7%94%A8mvc%3Aannotation-driven%E4%BB%A3%E6%9B%BF%E4%B8%8A%E9%9D%A2%E4%B8%A4%E4%B8%AA%E6%B3%A8%E8%A7%A3%E6%98%A0%E5%B0%84%E5%99%A8%E5%92%8C%E6%B3%A8%E8%A7%A3%E9%80%82%E9%85%8D%E7%9A%84%E9%85%8D%E7%BD%AE%0A%20%20%20%20%20mvc%3Aannotation-driven%E9%BB%98%E8%AE%A4%E5%8A%A0%E8%BD%BD%E5%BE%88%E5%A4%9A%E7%9A%84%E5%8F%82%E6%95%B0%E7%BB%91%E5%AE%9A%E6%96%B9%E6%B3%95%EF%BC%8C%0A%20%20%20%20%20%E6%AF%94%E5%A6%82json%E8%BD%AC%E6%8D%A2%E8%A7%A3%E6%9E%90%E5%99%A8%E9%BB%98%E8%AE%A4%E5%8A%A0%E8%BD%BD%E4%BA%86%EF%BC%8C%E5%A6%82%E6%9E%9C%E4%BD%BF%E7%94%A8mvc%3Aannotation-driven%E5%88%99%E4%B8%8D%E7%94%A8%E9%85%8D%E7%BD%AE%E4%B8%8A%E9%9D%A2%E7%9A%84RequestMappingHandlerMapping%E5%92%8CRequestMappingHandlerAdapter%0A%20%20%20%20%20%E5%AE%9E%E9%99%85%E5%BC%80%E5%8F%91%E6%97%B6%E4%BD%BF%E7%94%A8mvc%3Aannotation-driven%0A%20%20%20%20%20%2D%2D%3E-->    <mvc:annotation-driven></mvc:annotation-driven>    <!--{cke_protected}{C}%3C!%2D%2D%20%E8%A7%86%E5%9B%BE%E8%A7%A3%E6%9E%90%E5%99%A8%0A%20%20%20%20%E8%A7%A3%E6%9E%90jsp%2C%E9%BB%98%E8%AE%A4%E4%BD%BF%E7%94%A8jstl%2Cclasspath%E4%B8%8B%E8%A6%81%E6%9C%89jstl%E7%9A%84%E5%8C%85%0A%20%20%20%20%2D%2D%3E-->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <!--{cke_protected}{C}%3C!%2D%2D%20%E9%85%8D%E7%BD%AEjsp%E8%B7%AF%E5%BE%84%E7%9A%84%E5%89%8D%E7%BC%80%20%2D%2D%3E-->        <property name="prefix" value="/WEB-INF/jsp/">        <!--{cke_protected}{C}%3C!%2D%2D%20%E9%85%8D%E7%BD%AEjsp%E8%B7%AF%E5%BE%84%E7%9A%84%E5%90%8E%E7%BC%80%20%2D%2D%3E-->        <property name="suffix" value=".jsp">    </property></property></bean></beans></code>
Configure web. xml

Refer to the Getting Started program, web. xml

<code class="language-xml hljs "><!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%20%3F%2D%2D%3E--><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>    <!--{cke_protected}{C}%3C!%2D%2D%20%E5%8A%A0%E8%BD%BDspring%E5%AE%B9%E5%99%A8%20%2D%2D%3E-->    <context-param>        <cke:param-name>contextConfigLocation</cke:param-name>        <cke:param-value>WEB-INF/classes/spring/applicationContext-*.xml</cke:param-value>        <!--{cke_protected}{C}%3C!%2D%2D%20%20%3Cparam-value%3Eclasspath%3Aspring%2FapplicationContext-*.xml%3C%2Fparam-value%3E%2D%2D%3E-->      </context-param>    <listener>      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener><!--{cke_protected}{C}%3C!%2D%2D%20springmvc%20%E5%89%8D%E7%AB%AF%E6%8E%A7%E5%88%B6%E5%99%A8%20%20%2D%2D%3E-->    <servlet>        <servlet-name>springmvc</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <!--{cke_protected}{C}%3C!%2D%2D%20contextConfigLocation%E9%85%8D%E7%BD%AEspringmvc%E5%8A%A0%E8%BD%BD%E7%9A%84%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6(%E9%85%8D%E7%BD%AE%E5%A4%84%E7%90%86%E5%99%A8%E6%98%A0%E5%B0%84%E5%99%A8%E3%80%81%E9%80%82%E9%85%8D%E5%99%A8%E7%AD%89%E7%AD%89)%0A%20%20%20%20%20%20%20%20%20%20%E8%8B%A5%E4%B8%8D%E9%85%8D%E7%BD%AE%EF%BC%8C%E9%BB%98%E8%AE%A4%E5%8A%A0%E8%BD%BDWEB-INF%2Fservlet%E5%90%8D%E7%A7%B0-servlet(springmvc-servlet.xml)%0A%20%20%20%20%20%20%20%20%2D%2D%3E-->        <init-param>            <cke:param-name>contextConfigLocation</cke:param-name>            <cke:param-value>classpath:spring/springmvc.xml</cke:param-value>        </init-param>    </servlet>    <servlet-mapping>        <servlet-name>springmvc</servlet-name>        <!--{cke_protected}{C}%3C!%2D%2D%0A%20%20%20%20%20%20%20%20%E7%AC%AC%E4%B8%80%E7%A7%8D%3A*.action%2C%E8%AE%BF%E9%97%AE%E4%BB%A5.action%E4%B8%89%E7%BB%93%E5%B0%BE%EF%BC%8C%E7%94%B1DispatcherServlet%E8%BF%9B%E8%A1%8C%E8%A7%A3%E6%9E%90%0A%20%20%20%20%20%20%20%20%E7%AC%AC%E4%BA%8C%E7%A7%8D%3A%2F%2C%E6%89%80%E6%9C%89%E8%AE%BF%E9%97%AE%E7%9A%84%E5%9C%B0%E5%9D%80%E7%94%B1DispatcherServlet%E8%BF%9B%E8%A1%8C%E8%A7%A3%E6%9E%90%EF%BC%8C%E5%AF%B9%E9%9D%99%E6%80%81%E6%96%87%E4%BB%B6%E7%9A%84%E8%A7%A3%E6%9E%90%E9%9C%80%E8%A6%81%E9%85%8D%E7%BD%AE%E4%B8%8D%E8%AE%A9DispatcherServlet%E8%BF%9B%E8%A1%8C%E8%A7%A3%E6%9E%90%EF%BC%8C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E4%BD%BF%E7%94%A8%E6%AD%A4%E7%A7%8D%E6%96%B9%E5%BC%8F%E5%92%8C%E5%AE%9E%E7%8E%B0RESTful%E9%A3%8E%E6%A0%BC%E7%9A%84url%0A%20%20%20%20%20%20%20%20%E7%AC%AC%E4%B8%89%E7%A7%8D%3A%2F*%2C%E8%BF%99%E6%A0%B7%E9%85%8D%E7%BD%AE%E4%B8%8D%E5%AF%B9%EF%BC%8C%E4%BD%BF%E7%94%A8%E8%BF%99%E7%A7%8D%E9%85%8D%E7%BD%AE%EF%BC%8C%E6%9C%80%E7%BB%88%E8%A6%81%E8%BD%AC%E5%8F%91%E5%88%B0%E4%B8%80%E4%B8%AAjsp%E9%A1%B5%E9%9D%A2%E6%97%B6%EF%BC%8C%E4%BB%8D%E7%84%B6%E4%BC%9A%E7%94%B1DispatcherServlet%E8%A7%A3%E6%9E%90jsp%E5%9C%B0%E5%9D%80%EF%BC%8C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E4%B8%8D%E8%83%BD%E6%A0%B9%E6%8D%AEjsp%E9%A1%B5%E9%9D%A2%E6%89%BE%E5%88%B0handler%EF%BC%8C%E4%BC%9A%E6%8A%A5%E9%94%99%0A%20%20%20%20%20%20%20%20%2D%2D%3E-->        <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></code>

This file has two functions:

Configure the front-end controller ( DispatcherServlet) Load the spring container: add the spring container listener, load the spring container, and load the container with wildcards. spring/Configuration file under
ApplicationContext-dao.xml applicationContext-service.xml applicationContext-transaction.xml write Controller (is Handler)
Package com. iot. learnssm. firstssm. controller; import com. iot. learnssm. firstssm. po. items; import com. iot. learnssm. firstssm. po. itemsCustom; import com. iot. learnssm. firstssm. service. itemsService; import org. springframework. beans. factory. annotation. autowired; import org. springframework. stereotype. controller; import org. springframework. web. bind. annotation. requestMapping; import org. springframework. web. servlet. modelAndView; import java. util. arrayList; import java. util. list;/*** Created by brian on 2016/3/2. * /// use @ Controller to identify it as a Controller @ Controller // to manage URLs by category, you can define the root path here, the final access url is the root path + sub-path // For example: product list:/items/queryItems. actionpublic class ItemsController {@ Autowired private ItemsService itemsService; // item query list @ RequestMapping ("/queryItems") // map the queryItems method and url, A method corresponds to a url // It is generally recommended to write the url and method as public ModelAndView queryItems () throws Exception {// call the service to find the database and query the product List
  
   
ItemsList = itemsService. findItemsList (null); // return ModelAndView modelAndView = new ModelAndView (); // It is equivalent to the setAttribute method of the request. You can use itemsList to retrieve data modelAndView on the jsp page. addObject ("itemsList", itemsList); // specify the path below the view. If you configure the jsp path prefix and Suffix in the view parser, change to items/itemsList // modelAndView. setViewName ("/WEB-INF/jsp/items/itemsList. jsp "); // The path configuration below can not specify the prefix and suffix modelAndView of the jsp path in the program. setViewName ("items/itemsList"); return modelAndView ;}}
  
Compile jsp

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"%>

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.