標籤:style blog class code java ext
Struts2出現錯誤,總是提示"There is no Action mapped for namespace / and action name"的錯誤。
錯誤碼如下:
09:36:13,515 WARN [Dispatcher] Could not find action or resultThere is no Action mapped for namespace [/] and action name [css] associated with context path [/ChinaUnicom]. - [unknown location] at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185) at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63) at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39) at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58) at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:534) at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Thread.java:619)
上網查詢後發現這是一個初學者經常碰到的問題,導致錯誤的原因主要有兩種。總結如下:
1.struts.xml檔案錯誤。這種錯誤又分為以下幾種:
1.1struts.xml檔案名稱錯誤。一定要注意拼字問題;
1.2struts.xml檔案放置路徑錯誤。一定要將此檔案放置在src目錄下。編譯成功後,要確認是否編譯到classes目錄中;
1.3struts.xml檔案內容錯誤。下面給出一個正確的struts.xml檔案以供參考。注意背景色部分。
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <package name="default" namespace="/" extends="struts-default"> <action name="login" class="com.wanggc.struts2.sample.Struts2Action"> <result name="success">/jsp/result.jsp</result> </action> </package></struts>
2.如果排除了struts.xml檔案的問題,還有一種可能就是,在web.xml檔案中的<welcome-file>資訊中是否配置了自己工程的啟動頁面。如果沒有配置,地址欄中要輸入完成的url,如:http://localhost:8080/Struts2Sample/jsp/login.jsp,後面的資來源詳細資料不輸入的時候也會報這個錯誤,我就是栽在這個問題上的(^_^)。