配置Struts2動態Action報錯404:There is no Action mapped for namespace [/]

下午在eclipse中配置struts2時報:

There is no Action mapped for namespace [/] and action name [Login] associated with context path [/eprint]

錯誤

做如下檢查:

1、確保struts.xml文件名大小寫正確:struts.xml

2、確保struts.xml文件在src目錄下(很重要!後面就着重說這個)

附:

web.xml文件內容

示例:

<?xml version="1.0" encoding="UTF-8"?>  
<web-app version="3.0"   
    xmlns="http://java.sun.com/xml/ns/javaee"   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  
  <display-name></display-name>   
    
   <filter>  
     <filter-name>struts2</filter-name>  
     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  </filter>  
  <filter-mapping>  
     <filter-name>struts2</filter-name>  
     <url-pattern>/*</url-pattern>  
  </filter-mapping>  
    
  <welcome-file-list>  
    <welcome-file>index.jsp</welcome-file>  
  </welcome-file-list>  
</web-app>
struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE struts PUBLIC  
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
    "http://struts.apache.org/dtds/struts-2.3.dtd">  
<struts>  
    <package name="default" namespace="/" extends="struts-default">  
        <action name="LoginAction" class="com.xxx.control.LoginAction">  
            <!-- 定義邏輯視圖和物理資源之間的映射 -->  
            <result name="error">index.jsp</result>  
            <result name="success">/WEB-INF/main.jsp</result>  
        </action>  
    </package>  
</struts>
index.jsp:

<s:form action="LoginAction" namespace="/">  
        <s:textfield name="username" label="username"></s:textfield>  
        <s:password name="password" label="password"></s:password>  
        <div id="login_button">  
            <input type="submit" value="login"/>   
        </div>  
        <div  id="register_button">  
            <input type="button" value="register"/>  
        </div>  
  
      </s:form>

注意:使用s標籤記得引入taglib:<%@ taglib prefix="s" uri="/struts-tags" %>

編譯後classes文件默認卻是在build目錄下

不在WEB-INF下.......

這樣它是找不到struts.xml文件的,報如上錯誤,也是情有可原的

最後,明確一點,struts.xm位於src下是爲了編譯後能找到struts配置文件,確保其在WEB-INF下才是根本!!!

以上都沒有出現問題,還報錯的話,那麼就是使用動態Action時候沒有聲明,修改struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE struts PUBLIC  
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
    "http://struts.apache.org/dtds/struts-2.3.dtd">  
<struts>  
	<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
    <package name="default" namespace="/" extends="struts-default">  
        <action name="LoginAction" class="com.xxx.control.LoginAction">  
            <!-- 定義邏輯視圖和物理資源之間的映射 -->  
            <result name="error">index.jsp</result>  
            <result name="success">/WEB-INF/main.jsp</result>  
        </action>  
    </package>  
</struts>

更改eclipse web 項目默認編譯輸出路徑:

eclipse中只能針對項目更改,因爲其默認的是build目錄下的,只能以項目更改: 
項目右鍵 -》properties -》Java Build Path -》source -》Default output folder,選擇你的路徑,ok!





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章