When accessing static resources and importing jars in the javaweb project in idea, java.lang.ClassNotFoundException: com.mysql.jdbc.Driver occurs

Title: Accessing static resources in the javaweb project in idea, importing jar, java.lang.ClassNotFoundException: com.mysql.jdbc.Driver appears

Although maven is used now, take a look at the explanation below, you can deepen your understanding of what maven has done for us

1. For java.lang.ClassNotFoundException: com.mysql.jdbc.Driver,

Generally, it is because you imported the jar, but it is not associated with the module, or it is not redeployed to the project

  • 1. If our lib is under Project,

    • 1.1 First right click lib and then, add as libraries
  • insert image description here

    • 1.2 Then connect to the Module [click +, library, then join]
  • insert image description here

    • 1.3 Redeploy the project [first click -, then click +]
  • insert image description here

  • If our lib is under the Module, there is no need to do the above, and you can also follow the above.

  • 2. If it is a web project, it needs to be deployed to tomcat at the end

  • insert image description here

    • 1. Click ➖insert image description here
    • 2. Click + to select artifact
    • insert image description here
    • 3. Click Applyinsert image description here

3. Note: Our application context should correspond to the one in the url

  • eg: application context is: /, then the url can only be http://locahost:8080/XXX.html, [this html is any html under your web folder, you can also replace XXX.html with aa/ bb/XXX.html]

  • insert image description here

  • insert image description here

  • But if your application context is /pro09, then the url should be http://localhost:8080/pro09/xxx.html, which must be corresponding, otherwise, the url cannot be accessed when it automatically jumps

insert image description here
If this is /, it cannot be accessedinsert image description here

2. Import jar as above

3. Access static resources in javaweb project

eg: Access css/index.css, [index.css is in web/css/index.css] There is no problem at all, but it may not be accessible.

  • The reason may be: your css was added later and has not been deployed to the project, so you can redeploy it to the project or delete everything under the out folder (it will be regenerated every time you run it) [see above Deploy to the project].
    +insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/weixin_45986454/article/details/127922572