2013-08-23 29 views
1

我正在關注「Struts 2 in Action」一書,並且在該書的第一章下面有一個待開發的示例基本應用程序。我試圖遵循它,操作類沒有正確調用。但是我可以查看JSP頁面的成功結果。當我使用調試模式並嘗試調試操作類的execute方法時,它不會調用。我正在使用struts-2.3.15.1庫來開發此應用程序。Struts 2基於註釋的應用程序不能正常工作

我的Action類是如下:

package manning.chapterTwo; 

import org.apache.struts2.config.Result; 

import com.opensymphony.xwork2.ActionSupport; 



@Result(name="SUCCESS", value="/chapterTwo/HelloWorld.jsp") 
public class AnnotatedHelloWorldAction extends ActionSupport{ 


private static final long serialVersionUID = 1L; 

private static final String GREETING = "Hello "; 


public String execute() { 

    setCustomGreeting(GREETING + getName()); 

    return "SUCCESS"; 
} 

private String name; 


public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

private String customGreeting; 

public String getCustomGreeting() 
{ 
    return customGreeting; 
} 

public void setCustomGreeting(String customGreeting){ 
    this.customGreeting = customGreeting; 
} 
} 

JSP頁面來獲得輸入看起來像下面(NameCollector.jsp):

<%@ page contentType="text/html; charset=UTF-8" %> 
<%@ taglib prefix="s" uri="/struts-tags" %> 
<html> 

<head> 
<title>Name Collector</title> 
</head> 

<body> 
    <hr> 
    <h4>Enter your name so that we can customize a greeting just for you!</h4> 
    <s:form action="annotatedHelloWorld"> 
    <s:textfield name="name" label="Your name"/> 
    <s:submit/> 
    </s:form> 
    <hr> 
</body> 

    </html> 

輸出JSP:

<%@ page contentType="text/html; charset=UTF-8" %> 
<%@ taglib prefix="s" uri="/struts-tags" %> 
<html> 
<head> 
<title>HelloWorld</title> 
</head> 

<body> 

    <hr> 
    <h3>Custom Greeting Page</h3>  
    <h4><s:property value="customGreeting"/></h4> 
    <hr> 
</body> 
    </html> 

web.xml文件:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 
    <display-name>Struts2HelloWorldXML</display-name> 
    <welcome-file-list> 
    <welcome-file>index.html</welcome-file> 
    <welcome-file>index.htm</welcome-file> 
    <welcome-file>index.jsp</welcome-file> 
    <welcome-file>default.html</welcome-file> 
    <welcome-file>default.htm</welcome-file> 
    <welcome-file>default.jsp</welcome-file> 
    </welcome-file-list> 

    <filter> 
    <filter-name>struts2</filter-name> 
     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
    <init-param> 
    <param-name>actionPackages</param-name> 
    <param-value>manning</param-value> 
    </init-param> 
    </filter> 

    <filter-mapping> 
     <filter-name>struts2</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 

</web-app> 

如何解決?

+0

有什麼異常嗎?你可以請共享服務器日誌.. – Shashi

+0

這將是一個有點有用,如果你發佈[SSCCE](http://www.sscce.org/)這是不容易的,讀取所有... – Gerret

回答

0

您應該添加一個動作註釋應該作者或者使用約定來映射你的行動或勘誤表添加的註釋動作的動作類或方法

@ParentPackage("chapterTwo"); 
@Action(value="HelloWorld", [email protected](name="SUCCESS", value="/chapterTwo/HelloWorld.jsp")) 
public class AnnotatedHelloWorldAction extends ActionSupport { 

。還添加了操作所屬的父包註釋。並且不要忘記將添加到庫集中,因爲它在示例中未找到。

+0

我有也添加了struts2-convention-插件。羅馬告訴的修改不起作用 – chinthakarukshan

+0

什麼是錯誤? –

+0

它不會引發任何錯誤。但是動作類的執行方法在調試模式下不會調用,結果也不能正確顯示。 import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.ParentPackage; import org.apache.struts2.convention.annotation.Result; import com.opensymphony.xwork2.ActionSupport; @ParentPackage( 「chapterTwo」) @Action(值= 「/ HelloWorld」 的,結果= @結果(名稱= 「SUCCESS」,位置= 「/ chapterTwo /的helloWorld.jsp」)) – chinthakarukshan