struts2.x如要想要使用annotation,則需要匯入asm-3.3.jar、asm-commons-3.3.jar和struts2-convention-plugin-2.3.37.jar。並且修改web.xml檔案,使其支援annotation。
在web.xml檔案裡的filter元素裡加入init-param元素,如下所示:
actionpages
com.bank.action
所有struts2.x專案的開發都離不開struts.xml檔案,如果使用annotation進行開發,則需要在struts.xml檔案中設定一些通用配置,示例如下:
<?xml version="1.0" encoding="utf-8"?>
pages/errors.jsp
pages/forward.jsp
使用註解配置action
struts2提供了四個與action相關的註解,分別為parentpackage、namespace、result和action。
parentpackage註解用於指定action所在包要繼承的父包,該註解只有乙個value引數,用於指定要繼承的父包。
namespace註解用於指定action所在包的命名空間。該註解只有乙個value引數,用於指定action所屬於的命名空間。
result註解用於定義乙個result對映,該註解包含四個引數,如下:
action註解對應於struts.xml檔案中的action元素。該註解可用於action類上,也可用於方法上,該註解包含以下屬性:
【範例】:在web.xml檔案中宣告的包下定義乙個action,並新增註解:
package com.bank.action;
import com.opensymphony.xwork2.actionsupport;
import org.apache.struts2.servletactioncontext;
import org.apache.struts2.convention.annotation.*;
@suppresswarnings
("serial"
)@parentpackage
("root"
)@namespace
(value=
"/pages"
)//定義了自己的命名空間
@action
(value=
"testaction"
)@results
(value =
)public
class
testaction
extends
actionsupport
@actions
(value=
)public
void
insert()
}
使用註解配置***
struts2中編寫的action如果想使用annotation的方式進行***的配置,則可以使用以下兩個註解:
interceptorref註解用於指定本action要配置的interceptor,該註解只有乙個value引數,用於指定要配置的interceptor名稱(如果是自己定義的interceptor,則需要先在struts.xml檔案中註冊)
interceptorrefs註解包含多個interceptorref註解,可以為本action配置多個***。該註解只有乙個value引數,用於指定要配置的多個interceptorref。
【範例】:
@interceptorref
(value=
"timer"
)//配置乙個interceptor
@interceptorrefs
(value=
)
在IntelliJ IDEA中使用struts2
因為老師都是在eclipse上寫,為了在idea上使用struts2折騰了好久,網上大都是在專案建立的時候使用,所有都幫你配置好了,而我是因為跟著課程所有要在現有專案上修改新增,手動配置,就遇到挺多坑。方法一 在建立專案時勾選 自動生成相關配置檔案 增加修改相關配置檔案web.xml struts2...
自定義annotation註解並對其做切面
編寫乙個註解 target elementtype.method retention retentionpolicy.runtime documented method requestmethod.get public inte ce requestget aliasfor class string...
SSH採用annotation注入
一 標註 dao 和 service 標註dao repository persondao public class persondaoimpl extends basehibernatedaosupport implements persondao public class personactio...