Preface:

Another day of learning, not back-end development, but for the sake of the job. In case you ever learn it. My notes are only suitable for me to see. At present, I even need to mark how to build projects and classes.

Java, stepping into the grave. Really, just learned grammar to start this, pure is the work needs, to be the development of the elder brothers must not be so.

P.s Download spring and older versions of documentation (change the version number of the URL to view other versions)

First MVC application

1. Create a new Moudle, SpringMVC-02-Hellomvc, and add Web support!

2. Make sure you import SpringMVC dependencies!

3. Configure web. XML and register DispatcherServlet

Here you see springMVC-servlet.xml red because you need a configuration file.

4. Write SpringMVC configuration files!

Create a new file as shown in the following image and name it springMVC-servlet.xml

<? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" 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.xsd"> </beans>Copy the code

5. Add a processing mapper

<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
Copy the code

6. Add processor adapters

<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
Copy the code

7. Add a view parser

<! View parser :DispatcherServlet to its ModelAndView--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver"> <! Prefix -- - > < property name = "prefix" value = "/ WEB - INF/JSP/" / > <! -- suffix - > < property name = "suffix" value = "JSP" / > < / bean >Copy the code

Back here, it’s not going viral.

8. Write the business Controller we want to operate

Either implement the Controller interface or add annotations; We need to return a ModelAndView, load the data, and seal the view;

Create a new package

Create a new HelloController class // it will not look like this in real development

package com.kuang.controller; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; / / note: Public Class HelloController implements Controller {public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse Response) throws Exception {//ModelAndView mv = new ModelAndView(); // Encapsulate the object and place it in ModelAndView. Model mv.addObject("msg","HelloSpringMVC!" ); // Encapsulate the view to jump to and place it in ModelAndView mv.setViewName("hello"); //: /WEB-INF/jsp/hello.jsp return mv; }}Copy the code

Note: Shortcut key for package guide: Alt +Enter (select the following package here)

9. Give your class to the SpringIOC container and register the bean

Write a JSP

Write the JSP page to jump to, display the data stored in ModelandView, and our normal page

11. Configure Tomcat startup tests!

After confirming, click the green triangle in the upper toolbar to start it.

Request hello, 404 found

Possible problems: 404 appears in the access, troubleshooting steps:

  1. Check the console output to see if any JAR packages are missing.
  2. If the jar package exists and cannot be displayed, add lib dependencies in the project release of IDEA!
  3. Restart Tomcat!

Open the Project Structure as shown in the figure to see if there is a lib

No, so add one manuallyThen import all the packages and apply them. This is a problem with IDEA that Eclipse does not have

Restart the system