Struts2 注解基礎(chǔ)
The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag. - [unknown location]
先是報(bào)這個錯,因?yàn)閟truts2的filter是*.action的原因。改為以下即可。
<filter-mapping>
<filter-name>struts2Filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
1.convention plugin插件
convention plugin默認(rèn)result頁面存放在WEB-INF/content.(可以通過struts.convention.result.path屬性來設(shè)置)。
如 http://localhost:8080/hello-world該url將訪問WEB-INF/content/hello-world.jsp。
2.convention plugin查找類的規(guī)則
convention plugin會查找struts、struts2、action、actions等包里的滿足以下條件的類(好像可以設(shè)置)
- 實(shí)現(xiàn)或繼承com.opensymphony.xwork2.Action、ActionSupport的類。
- 或者action結(jié)尾的類名
<constant name="struts.convention.package.locators" value="test" />
<constant name="struts.convention.package.locators.basePackage" value="com.test" />
3.convention plugin類對應(yīng)URL規(guī)則
在struts、struts2、action、actions等包下生成“/”,更深層次則繼續(xù)以“包名/”,類名子目全小寫,按駝峰法分隔單詞添加“-”。舉例如下:
com.example.actions.MainAction -> /main
com.example.actions.products.Display -> /products/display
com.example.struts.company.details.ShowCompanyDetailsAction -> /company/details/show-company-details
4.result對應(yīng)頁面名稱
頁面命名與類名規(guī)則相同,再加上“-”與result值即可,如果找不到result的頁面,似乎會返回到省略result名稱的頁面,還有success可省略,如下:
URL | Result | File that could match | Result Type |
---|---|---|---|
/hello | success | /WEB-INF/content/hello.jsp | Dispatcher |
/hello | success | /WEB-INF/content/hello-success.htm | Dispatcher |
/hello | success | /WEB-INF/content/hello.ftl | FreeMarker |
/hello-world | input | /WEB-INF/content/hello-world-input.vm | Velocity |
/test1/test2/hello | error | /WEB-INF/content/test/test2/hello-error.html | Dispatcher |
5.chaining
例子:foo action找不到result頁面,會自動查找foo-bar action
package com.example.actions;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport {
@Action("foo")
public String foo() {
return "bar";
}
@Action("foo-bar")
public String bar() {
return SUCCESS;
}
}
參考:
struts2 convention-plugin文檔
http://www.vaannila.com/struts-2/struts-2-example/struts-2-annotation-example-1.html