page

Under WEB-INF, create a new folder page and create a new page first.jsp.

<%@ page contentType="text/html; charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>${message}</h1> </body> </html>Copy the code

Spring core configuration file

Here you can configure a common prefix and suffix for the page; in the control class you only need to give the view name.

<? The XML version = "1.0" encoding = "UTF8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/page/"/> <property name="suffix" value=".jsp"/> </bean> </beans>Copy the code

Another person sorted out some information, friends in need can directly click to get.

Java Basics

22 Java Architect Core books

Learning routes and materials from 0 to 1Java

1000+ questions from 2021

Web.xml configuration file

This section is fixed, you don’t need to register each Servlet separately, just register a front-end controller DispatcherServlet.

<? The XML version = "1.0" encoding = "utf-8"? > <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" Version = "4.0" > < servlet > < servlet - name > for springmvc < / servlet - name > <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:SpringMVC-Servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>Copy the code

Controller

SpringMVC provides a number of ways to mark a class as a control class; This can be done by implementing the Controller interface or through Controller annotations.

The Controller interface

Write a control class and implement the Controller interface, override the handleRequest method that will accept the request and return a ModelAndView class.

package com.controller; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class InterfaceController implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView mv = new ModelAndView(); /* 1. Call the business layer 2. Encapsulate the data and give the name of the view to respond to 3. Return the ModelAndView object to the adapter */ mv.addobject ("message"," implement the Controller interface "); / / data mv. SetViewName (" first "); // The view name of the response return mv; }}Copy the code

The control class is registered and given an access path. Localhost :8080/ project name /inter/first; I’ll be able to conduct an interview.

<bean id="/inter/first" class="com.controller.InterfaceController"/>

Copy the code

The Controller annotations

I first learned about this annotation in Spring, but I had a rough idea that it could be used to register beans, not that it could mark a class as a control class. The Controller annotation registers the bean because it is decorated by the Component annotation, which has the ability to register the bean.

package com.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @controller public class AnnotationController {@requestMapping ("/Annota/first") // Register mapping path private String FirstServlet (Model Model){model.addattribute ("message", "Controller note "); Return "first"; // Give the view name}}Copy the code

Using the Controller annotation, you can mark a class as a control class, and each of its methods can be considered a Servlet. Annotations are more powerful than implementing the Controller interface. You only need to write multiple methods when you have multiple servlets!

The SpringMVC core configuration file just needs to extend the annotation driver and MVC annotation support a little bit.

<! <context:component-scan base-package="com.controller"/> <! --> < MVC :annotation-driven/> <! < MVC :default-servlet-handler/>Copy the code

RequestMapping annotations

The RequestMapping annotation registers a servlet-path mapping to indicate which method will handle the access/XXX path and that’s it; Using this annotation, you can get rid of the core configuration file and put the fixed template code into the configuration file each time. Use the Controller annotation + RequestMapping annotation in the control class.

RequestMapping annotations modify the class

When modifying a class, it means that each of its methods needs a prefix path.

@Controller @RequestMapping("/father") public class RequestMappingController { @RequestMapping("/test1") private String test1(){ return "test1"; } @RequestMapping("/test2") private String test2(){ return "test2"; }}Copy the code

Test1 True path: localhost:8080/ project name /father/test1 Test2 True path: localhost:8080/ project name /father/test2

Modification methods

When modifying methods, only concrete paths are used.

@Controller public class RequestMappingController { @RequestMapping("/test1") private String test1(){ return "test1"; } @RequestMapping("/father/test2") private String test2(){ return "test2"; }}Copy the code

The last

Thank you for reading this article. Please click “like” if you find it helpful. Thank you for your support!