When DispatcherServlet is configured in the project web.xml, the red wavy line reports an error (Cannot resolve Servlet 'DispatcherServlet')

Compilation software: IntelliJ IDEA 2019.2.4 x64
Operating system: win10 x64-bit Home Edition
Maven version: apache-maven-3.6.3
spring mvc version: 5.3.1



Project case scenario

Restful CRUD demo exercise under SpringMvc framework

demand analysis

1: Get all employee information

  1. URL:/emps
  2. Request method: GET
  3. The display effect is as follows:
    insert image description here

2: Add an operation to add an employee's page

  1. URL:/toAddPage
  2. Request method: GET
  3. display effect
    insert image description here

3: Add operation - add employee

  1. URL:/emps
  2. Request method: POST
  3. After the addition is successful [redirect to the UR of querying all employees]

ps: The idea I implemented here is to add employees first, then call and return the return value response of the method of querying all employees to the browser

The relevant implementation code of the Controller layer is as follows:

	
   @GetMapping("/emps")
   /**
    * 配置URL [/emps],服务器使用ModelAndView对象处理响应数据
    *  查询所有员工信息
    */
    public ModelAndView selectAllEmps(){
    
    

       ModelAndView mv=new ModelAndView();
       mv.setViewName("emps");
       //将list集合employees存放进请求域中
       Collection<Employee> employees = employeeService.showAllEmps();
       mv.addObject("employees",employees);
       return mv;
   }


 /**
     * 添加员工
     * @param employee
     * @return
     */
   @PostMapping("/emps")
   //使用pojo入参
   public ModelAndView addEmps(Employee employee){
    
    
       boolean b = employeeService.addEmp(employee);
       System.out.println(b==true?"添加成功":"添加失败");
       return selectAllEmps();
   }




  1. demand display effect

insert image description here

4: Delete employee

  1. URL:emps/id

  2. Request method: DELETE,

  3. Click the delete button to pop up a prompt box

  4. After the deletion is successful, it is also redirected to the URL for querying all employees

ps: The idea I implemented here is to delete employees first, then call and return the return value of the method of querying all employees to respond to the browser [forward], not redirect

  1. Display effect: the corresponding employee information is directly deleted from the front-end page

5: Modify the operation to modify the employee's page

  1. URL:/toUpdatePage/{id)
  2. Request method: GET
  3. Query employee information from the database based on employee id
  4. The employee id cannot be modified, it is set to read-only
  5. The effect is as follows:

insert image description here


Description of the problem: When configuring DispatcherServlet in the project web.xml, the red wavy line reports an error (Cannot resolve Servlet 'DispatcherServlet')

As follows:
insert image description here


Cause Analysis

This alarm occurs because the Spring MVC framework cannot find a bean named "dispatcherServlet".

The Spring MVC framework will look for a Servlet Bean named dispatcherServlet at startup and register it in the Servlet container.如果该 Bean 无法正常创建或者未能被正确地扫描和加载,就会引发 "Cannot resolve Servlet 'dispatcherServlet'" 的异常。

Inquiring about relevant information, I found that there are several common reasons that may cause this problem:

  • web.xml 中的配置错误 The mapping relationship between servlet and filter is defined in the web.xml file of the web application . If it does not match the corresponding configuration in Spring MVC, the front controller DispatcherServlet will not be able to find the corresponding Bean.
  • ②The relevant configuration code in web.xml is correct,可能是 Spring MVC框架不能正确加载或扫描该web.xml文件
  • ③If the version of the Spring MVC framework is updated to a newer version, but the relevant configuration is not modified accordingly, it is easy to cause this error to appear
  • The relevant jar package is not imported

To sum up, first of all, the jar package of my Spring Mvc framework is based on the 5.3.1 version, and other dependencies are also adapted to the 5.3.1 Spring Mvc version and have been imported correctly. Therefore, the reasons ③ & ④ are excluded .
insert image description here
insert image description here

The relevant configuration codes of DispatcherServlet in web.xml are copied directly from the previous project file, so the configuration of web.xml is correct. Eliminate the reason ① , check and analyze the reason ③, and check whether Spring Mvc can correctly scan and load to web.xml document. Open "Project Structure", 我发现我的Spring Mvc模块中的web工程引用了项目中另一个web工程的web.xml文件, no wonder an error is reported! ! !


Solution: Check whether Spring Mvc can correctly scan and load the web.xml file, if not, reset and select the web.xml path

① Find the icon below, click to open "Project Structure", or press the shortcut key "Ctrl+Alt+Shift+S" to open.

insert image description here

②"Modules" -> select the spring mvc module you want to check -> select 'Web' -> check whether the two paths are correct [whether the path where web.xml is located and the root path of the web project associated with web.xml are correct]( ps,我是这里引用了项目中另一个web工程的web.xml文件而导致的飘红)

insert image description here

Re-modify the path that references web.xml so that Spring Mvc can load the web.xml correctly

③Click on clear and compile in Maven in turn to solve the problem

insert image description here

Guess you like

Origin blog.csdn.net/siaok/article/details/130655473