2013-06-04 26 views
1

我剛開始學習一些Struts2並嘗試創建簡單的網頁。我做了一個Action類,添加了一些註釋,他們我在web.xml文件中做了一些JSP並設置了過濾器,但是當我嘗試獲取頁面時,我只看到了JSP的工作,沒有任何Struts2。Struts2沒有使用帶註釋的動作

ClientListAction.java:

@Namespace("/") 
@ResultPath(value = "/") 
@Result(name = "success", location = "pages/clientList.jsp") 
public class ClientListAction extends ActionSupport { 

    private String username = "TEST"; 

    public String getUsername() { 
     return username; 
    } 

    public void setUsername(String username) { 
     this.username = username; 
    } 
} 

clientList.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %> 
<%@ taglib prefix="s" uri="/struts-tags" %> 
<html> 
<head> 
    <title>Test page Struts 2</title> 
</head> 
<body> 
<h4>Hello <s:property value="username"/></h4> 
</body> 
</html> 

的web.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app 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_2_5.xsd" 
     version="2.5"> 

    <display-name>Clients Application</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> 

</web-app> 

網址:

http://localhost:8080/pages/clientList.jsp 

結果:

Hello 
+0

URL有誤。您正在直接訪問jsp。您應該訪問該操作。 您的網址應該是這樣的:'localhost:8080 \ myapp \ clientListAction' –

+0

我試過了,但它沒有幫助 – Andrew

回答

1

刪除@ResultPath(value = "/")並設置絕對路徑location屬性。在默認情況下,結果註釋name = "success"也被使用,編寫它是沒有必要的。使用將struts2-convention插件添加到模塊依賴項所需的註釋配置。要執行的動作,你需要輸入網址

http://localhost:8080/client-list 

如果你的模塊將被部署到根上下文或動作名稱前添加上下文路徑。

+0

非常感謝 – Andrew