struts2 註解配置

2021-06-18 21:24:57 字數 1710 閱讀 2995

struts2基於註解的action配置

使用註解來配置action的最大好處就是可以實現零配置,但是事務都是有利有弊的,使用方便,維護起來就沒那麼方便了。

要使用註解方式,我們必須新增乙個額外包:struts2-convention-plugin-2.x.x.jar。

雖說是零配置的,但struts.xml還是少不了的,配置如下:

<?xml version="1.0" encoding="utf-8" ?> 

"-//apache software foundation//dtd struts configuration 2.1.7//en" 

"">   

action類的註解:

package com.tjcyjd.web.action;   

import org.apache.struts2.convention.annotation.action;   

import org.apache.struts2.convention.annotation.namespace;   

import org.apache.struts2.convention.annotation.parentpackage;   

import org.apache.struts2.convention.annotation.result;   

import org.apache.struts2.convention.annotation.results;   

import com.opensymphony.xwork2.actionsupport;   

* struts2基於註解的action配置

@parentpackage("struts-default") 

@namespace("/annotation_test") 

@results( ) 

public class loginaction extends actionsupport else )   

public string add() throws exception {   

return success;   

public string getloginname() {   

return loginname;   

public void setloginname(string loginname) {   

this.loginname = loginname;   

public string getpassword() {   

return password;   

public void setpassword(string password) {   

this.password= password;   

這樣就完成了乙個基於註解的action配置。

總結常用的註解如下:

namespace:指定命名空間。

parentpackage:指定父包。

result:提供了action結果的對映。(乙個結果的對映)

results:「result」註解列表

resultpath:指定結果頁面的基路徑。

action:指定action的訪問url。

actions:「action」註解列表。

interceptorref:***引用。

interceptorrefs:***引用組。

struts2零配置 註解

一 配置web.xml struts2 org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter actionpackages com.test.action struts2 二 加入註解 namespace valu...

Struts2 註解配置 validations

任意乙個xml配置的validator都有乙個相應的註解。每乙個註解的屬性都和xml的配置很相似。還有些註解可以用來把乙個類定義為使用基於註解的驗證,或是配置自定義的validator,還可以把乙個屬性或類的驗證進行分組。註解 相應的 xml 描述 requiredfieldvalidator re...

Struts2 零配置註解

2 action中可指定乙個value屬性。類似於指定屬性值 3 action中還可以指定乙個params屬性,該屬性是乙個字串陣列,用於該acion指定的引數名和引數值。params屬性應遵守如下格式 4 actions 也用於修飾action類裡的方法,用於將該方法對映到多個url.action...