Introduction to Spring MVC

Introduction to Spring MVC It is better to ha...
Introduction to Spring MVC
Introduction to Spring MVC

It is better to have a spring foundation to learn Spring MVC, because Spring MVC also uses spring's IOC technology when creating objects.

The role of Spring MVC:
Spring MVC is the framework of presentation layer in ssm framework, which is mainly used for the purpose of interaction with clients (browsers).
In short:
Browser request (path) - >
Corresponding to the method in Java class (and can get the value from the page in the executed method) - >
Display the new page after processing (the new page should have the return value returned by background method processing)

Introduction to Spring MVC

One web.xml Configuration of files
(1) web.xml Deploying DispatcherServlet in
The role of dispatcher servlet (front-end controller): which request paths are intercepted and handed over to spring mvc framework for processing. (we usually submit all requests to spring mvc for processing. Without this configuration, we cannot execute methods.)
(2) web.xml Configuring to read spring MVC in- servlet.xml Configuration files (that is, in the spring framework beans.xml ), and web.xml When the file is read, the configuration file is read and the ioc container is generated.

<?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_3_0.xsd" version="3.0"> <display-name>springMVC</display-name> <!-- deploy DispatcherServlet --> <servlet> <servlet-name>springmvc</servlet-name> <!-- Fixed configuration of this class (front-end controller, is a servlet) --> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- read ioc The configuration file for the container object, --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <!-- Indicates that the container is loaded immediately upon restart servlet --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- "/"On behalf of all URL --> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

2. Create spring MVC- servlet.xml configuration file
The configuration file is used to create ioc container, enable annotation scanning, complete the injection of creating view parser object and object attributes, and make view parser object complete corresponding functions.

<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- Use scanning mechanism to scan controller classes, which are all in controller Package and its sub package --> <context:component-scan base-package="controller" /> <!-- Configure view resolver InternalResourceViewResolver --> <bean id="internalResourceViewResolver" > <!--Prefix, used to configure prefixes for all views--> <property name="prefix" value="/WEB-INF/jsp/"/> <!--Suffixes, used to configure suffixes for all views--> <property name="suffix" value=".jsp"/> </bean> </beans>

3. Create Controller class

package controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * "@Controller"Indicates that the annotated class is a controller */ @Controller //@RequestMapping is placed on the class. At this time, all requests starting with / index will pass through the class @RequestMapping(value = "/index") public class IndexController { //@RequestMapping is placed on the method. At this time, all requests starting with / index/login will pass through the method @RequestMapping(value = "/login") public String login() { //The return value "login" represents the logical view name //The full name of the view is the prefix and suffix of the configuration view parser InternalResourceViewResolver in xml: WEB-INF/jsp/login.jsp return "login"; } @RequestMapping(value = "/register") public String register() { return "register"; } }

4. Create index.jsp file
This jsp is used to initiate requests. There are two types of requests that are hyperlink binding: registration and login.

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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>Insert title here</title> </head> <body> Unregistered users, please<a href="$/register"> register</a>! <br/> Registered users, go to<a href="$/login"> Sign in</a>! </body> </html>

5. Create login.jsp and register.jsp , for the last jump

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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>Insert title here</title> </head> <body> <h2> Registration page </h2> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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>Insert title here</title> </head> <body> <h2> Login page </h2> </body> </html>

17 June 2020, 03:45 | Views: 1772

Add new comment

For adding a comment, please log in
or create account

0 comments