2011-05-17 40 views
6

我知道如何使用的struts.xml中通配符方法調用,但有可能與註釋來做到這一點?如果是這樣如何?是否可以使用通配符方法調用Struts2中有約定只使用註解插件?

+0

你可以舉一個你想要做註釋的struts.xml映射的例子嗎? – 2011-05-19 16:16:02

+0

這是這麼久以來我......看着這個怎麼樣「*」是匹配任何這樣我可以在一個錯誤頁面,點的人,如果它不匹配任何其他模式。我會使用約定插件。 – Quaternion 2012-02-01 09:01:08

回答

8

你可能已經通過它現在已經解決了,但是對於那些尋找一個答案,是的,它是可能的。

通配符映射,請參閱:http://struts.apache.org/2.3.1.2/docs/wildcard-mappings.html

要閱讀從網址參數,這個註解你的方法:

private String id; 

@Action(value = "edit/{id}", 
     results={@Result(name = "success", location = "/WEB-INF/views/devices/edit.ftl")} 
)   
public String edit() { 
    // do something 
    return SUCCESS; 
} 

public void setId(String id) { 
    this.id = id; 
} 

public String getId() { 
    return id; 
} 
  • 您將需要id參數的getter/setter。
  • 你需要指定因爲struts2的結果會不知道用什麼成功的網址:...編輯/ 123,因此使用@Result需要指向您的文件。還挺defits公約插件存在的目的。

在我想重定向到一個特定的URL,使用此批註的情況:

@Action(value = "index", 
     results={@Result(name = "success", location = "/devices/edit/${entityId}", type = "redirect")} 
    ) 

您需要(在我的情況字符串)一個getter /爲ENTITYID制定者。

你也可以擁有先進的wilcard映射,命名空間通配符映射...

不要忘記改變struts.xml中添加以下常量。

<!-- Used for advanced wilcard mapping --> 
<constant name="struts.enable.SlashesInActionNames" value="true"/> 
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/> 
<constant name="struts.patternMatcher" value="regex" /> 
相關問題