IDEA JavaWeb專案啟動執行後出現404錯誤的解決方法
阿新 • • 發佈:2020-01-07
404這個錯誤真的是一言難盡!不過大多是配置檔案出錯,認真修改還是可以的
1.web.xml配置錯誤:
預設首頁沒有寫的,在web.xml新增一個就行(前提是你有jsp頁面,名字不要寫錯):
比如這個:
<welcome-file-list> <welcome-file>Login.jsp</welcome-file> </welcome-file-list>
2.IDEA JavaWeb專案執行找不到對應Servlet對映的JSP頁面
在servlet3.0以後提供了註解配置,這樣就不需要在web.xml中配置。使用IDEA生成的servlet檔案的註解配置如下(第一行):
@WebServlet(value = "/AdminServlet") public class AdminServlet extends HttpServlet { protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { doPost(request,response); } protected void doPost(HttpServletRequest request,IOException { } }
對於怎樣配置請看:
路徑:File > Settings > Editor > File and Code Templates > Other > Web > Java code templates 下第二個點開右邊編輯模板
如下(這是我的模板):
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end #parse("File Header.java") @javax.servlet.annotation.WebServlet(value = "/${Entity_Name}") public class ${Class_Name} extends javax.servlet.http.HttpServlet { protected void doGet(javax.servlet.http.HttpServletRequest request,javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException,java.io.IOException { doPost(request,response); } protected void doPost(javax.servlet.http.HttpServletRequest request,java.io.IOException { } }
3.建立了一個和以前專案名稱相同的專案(專案名稱結構一樣),把以前的覆蓋了 然後執行就404了;
第一時間想到的就是配置檔案有錯就開始找 :這個是我剛發現的解決辦法:
啟動執行後跳轉的頁面發現不是你定義的頁面報404錯誤,這就需要你在workspace.xml裡面找了:
你的專案名 > .idea > workspace.xml,找到裡面不是你首次跳轉的jsp的名字,修改為你定義的jsp名字
具體在這裡:把名字修改一下就好了
<component name="RunManager"> <configuration name="tomcat" type="#com.intellij.j2ee.web.tomcat.TomcatRunConfigurationFactory" factoryName="Local" APPLICATION_SERVER_NAME="Tomcat 7.0.47" ALTERNATIVE_JRE_ENABLED="true" ALTERNATIVE_JRE_PATH="C:\Program Files\Java\jdk1.8.0_191\jre"> <option name="OPEN_IN_BROWSER_URL" value="http://localhost:8080/Login.jsp" /> <option name="UPDATING_POLICY" value="restart-server" /> <deployment> <artifact name="SuShe:war exploded"> <settings> <option name="CONTEXT_PATH" value="/" /> </settings> </artifact> </deployment> <server-settings> <option name="BASE_DIRECTORY_NAME" value="Login_jsp_SuShe" /> </server-settings> <predefined_log_file id="Tomcat" enabled="true" /> <predefined_log_file id="Tomcat Catalina" enabled="true" /> <predefined_log_file id="Tomcat Manager" enabled="false" /> <predefined_log_file id="Tomcat Host Manager" enabled="false" /> <predefined_log_file id="Tomcat Localhost Access" enabled="false" /> <RunnerSettings RunnerId="Debug"> <option name="DEBUG_PORT" value="61238" /> </RunnerSettings> <ConfigurationWrapper VM_VAR="JAVA_OPTS" RunnerId="Cover"> <option name="USE_ENV_VARIABLES" value="true" /> <STARTUP> <option name="USE_DEFAULT" value="true" /> <option name="SCRIPT" value="" /> <option name="VM_PARAMETERS" value="" /> <option name="PROGRAM_PARAMETERS" value="" /> </STARTUP> <SHUTDOWN> <option name="USE_DEFAULT" value="true" /> <option name="SCRIPT" value="" /> <option name="VM_PARAMETERS" value="" /> <option name="PROGRAM_PARAMETERS" value="" /> </SHUTDOWN> </ConfigurationWrapper> <ConfigurationWrapper VM_VAR="JAVA_OPTS" RunnerId="Debug"> <option name="USE_ENV_VARIABLES" value="true" /> <STARTUP> <option name="USE_DEFAULT" value="true" /> <option name="SCRIPT" value="" /> <option name="VM_PARAMETERS" value="" /> <option name="PROGRAM_PARAMETERS" value="" /> </STARTUP> <SHUTDOWN> <option name="USE_DEFAULT" value="true" /> <option name="SCRIPT" value="" /> <option name="VM_PARAMETERS" value="" /> <option name="PROGRAM_PARAMETERS" value="" /> </SHUTDOWN> </ConfigurationWrapper> <ConfigurationWrapper VM_VAR="JAVA_OPTS" RunnerId="Run"> <option name="USE_ENV_VARIABLES" value="true" /> <STARTUP> <option name="USE_DEFAULT" value="true" /> <option name="SCRIPT" value="" /> <option name="VM_PARAMETERS" value="" /> <option name="PROGRAM_PARAMETERS" value="" /> </STARTUP> <SHUTDOWN> <option name="USE_DEFAULT" value="true" /> <option name="SCRIPT" value="" /> <option name="VM_PARAMETERS" value="" /> <option name="PROGRAM_PARAMETERS" value="" /> </SHUTDOWN> </ConfigurationWrapper> <method v="2"> <option name="BuildArtifacts" enabled="true"> <artifact name="SuShe:war exploded" /> </option> </method> </configuration> </component>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。