Introduction and Getting Started with SpringMVC

Introduction to SpringMVC

What is SpringMVC?

Both SpringMVC and Struts2 belong to the framework of the presentation layer. It is part of the Spring framework. We can see from the overall structure of Spring: 


SpringMVC processing flow

The SpringMVC processing flow is shown in the following figure: 


This picture roughly describes the entire processing flow of SpringMVC. At first glance, it is a bit dizzy, and after I analyze it step by step, I will finally understand it with a flow chart.

SpringMVC starter

This series of tutorials uses the SpringMVC4.1.3 version. Next, I will teach you how to get started with the SpringMVC framework. 
There is such a requirement: use the SpringMVC framework to realize the display of the product list. This is my analysis of this requirement: I assume here that the requested url is /itemList.action, because I want to display the product list, so I don't need to pass parameters. Again, this is just an introductory applet of SpringMVC, and it will not Integrating with MyBatis will not query the product list information from the database table, so the query product list data is only some static data. The following is the official start of the SpringMVC introductory applet.

Development steps of SpringMVC starter program

[First step], create a javaweb project, such as springmvc-first. 
[Step 2], import the jar package that SpringMVC runs independently, as follows: 

[The third step], create a jsp page - itemList.jsp, the content is as follows:

<%@ 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>Query product list</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/item/queryitem.action" method="post">
Query conditions:
<table width="100%" border=1>
<tr>
<td><input type="submit" value="查询"/></td>
</tr>
</table>
Product list:
<table width="100%" border=1>
<tr>
    <td>Product name</td>
    <td>Item Price</td>
    <td>Date of manufacture</td>
    <td>Item description</td>
    <td>操作</td>
</tr>
<c:forEach items="${itemList }" 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 }/itemEdit.action?id=${item.id}">修改</a></td>

</tr>
</c:forEach>

</table>
</form>
</body>

</html>
And copy the jsp page to the /WEB-INF/jsp directory of the project. 

[Step 4], create an Item class to describe the product information, the content is as follows:

public class Items {

    private int id;
    private String name;
    private double price;
    private Date createtime;
    private String detail;

    public Items(int id, String name, double price, Date createtime, String detail) {
        super();
        this.id = id;
        this.name = name;
        this.price = price;
        this.createtime = createtime;
        this.detail = detail;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public Date getCreatetime() {
        return createtime;
    }
    public void setCreatetime(Date createtime) {
        this.createtime = createtime;
    }
    public String getDetail() {
        return detail;
    }
    public void setDetail(String detail) {
        this.detail = detail;
    }

}
And copy the class to the com.itheima.springmvc.pojo package in the project src directory. 

[Step 5], create ItemController. ItemController is a common java class, which is similar to Action in Struts2, and does not need to implement any interface, just add @Controller annotation to the class. The @RequestMapping annotation specifies the requested url, where ".action" can be added or not. In the ModelAndView object, set the view to "/WEB-INF/jsp/itemList.jsp".

@Controller
public class ItemController {
    // .action can be omitted (the requested url address)
    @RequestMapping("/itemList.action")
    public ModelAndView itemList() {
        // Query the product list and use static data to generate a product list
        List<Items> itemList = new ArrayList<Items>();
        itemList.add(new Items(1, "imac", 20000, new Date(), "Apple is expensive"));
        itemList.add(new Items(2, "imac1", 20000, new Date(), "Apple is expensive"));
        itemList.add(new Items(3, "imac2", 20000, new Date(), "Apple is expensive"));
        itemList.add(new Items(4, "imac3", 20000, new Date(), "Apple is expensive"));
        itemList.add(new Items(5, "imac4", 20000, new Date(), "Damn, Apple is very expensive!"));
        // Pass the item list to jsp
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("itemList", itemList);
        // Set the view to display the data, that is, jsp
        modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");
        // return result
        return modelAndView;
    }
}
Finally, copy the ItemController class to the com.itheima.springmvc.controller package in the project src directory. 

[Step 6], create springmvc.xml, the content is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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">

    <context:component-scan base-package="com.itheima.springmvc.controller"/>
</beans>
The scanning package (the package where the Controller class is located) is configured above, then it will scan all @Controller-annotated classes under this package, and create objects and put them in the springmvc container. 

[Step 7], configure the front-end controller. Add the configuration of DispatcherServlet in web.xml, that is, add the following configuration in the web.xml file:

<!-- Configure Front Controller-->
<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <!-- Specifies the path to the springmvc configuration file. If not specified, the default is: /WEB-INF/${servlet-name}-servlet.xml -->
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springmvc.xml</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.action</url-pattern>
</servlet-mapping>

[The eighth step], the entry program test. Enter the url address in the browser address bar - http://localhost:8080/springmvc-first/itemList.action, press Enter, you can see the following effect: 



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324815644&siteId=291194637