IDEA SpringMVC 404 error (solved after trying many ways)

After importing the springmvc maven web project into IDEA, I have been unable to access 404 all the time. I have tried various methods, but there is no clear solution on the Internet.

The value of the contextConfigLocation of web.xml has always been red, it turned out to be

The value of param-value is classpath: spring/springmvc.xml, and then use the online method to change it to classpath* or red. But just start without error. However, it is still 404, and the control layer cannot be entered.

Later, the solution drew on this idea: https://www.cnblogs.com/feng924249386/p/8661106.html

Change the name of the root file, and then automatically jump to the initial page after startup, but still can't enter the control layer. Later, I felt that I still had to change the path. The essence was the path problem, because it was always red!

Finally, put the entire resource folder under WEB-INF, and then change all paths in web.xml to /WEB-INF/resources/spring/springmvc.xml, or else remove the original classpath:

The modified web.xml is as follows:

<servlet>
   <servlet-name>zhiyin</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
   <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/resources/spring/springmvc.xml</param-value>
   </init-param>
   <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
   <servlet-name>zhiyin</servlet-name>
   <url-pattern>/</url-pattern>
</servlet-mapping>

The paths in red in other files must be modified

 

In this way, all paths are not red, and then you can enter the control layer.

 

Solution two:

Later, I found that it is not so troublesome as above. The normal resources folder is under src/main, which is the same level as the java folder.

Finally found that because IDEA imported the web project, the resources folder is a folder, not a resource folder,

You need to use File-->make dictionary as Resource roots to convert it, you can do it, the previous path does not need to be changed, or classpath: spring/springmvc.xml

The reason is that after IDEA imports eclipse or myeclipse, the resource folder cannot be recognized. The resource folder and the Source roots code folder need to be distinguished in IDEA. You can refer to the following link:

https://blog.csdn.net/weixin_45091011/article/details/113078180

 

 

Guess you like

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