<a target="_blank" href="http://blog.csdn.net/javaeeteacher/article/details/4202741">struts 2基礎</a>
struts現在分兩個版本:struts 1.x和struts 2.x。struts 1.x已經有很多年了,可以說非常流行,但是因為其他架構的快速發展以及自身存在的問題,struts 2誕生了,struts 2與struts 1的差別非常大,實際上struts 2的核心思想是基于另外一個非常成功的web架構webwork。兩者的差別如表20.1所示。下面主要針對struts 2進行介紹。
表2.1 struts1和struts2的比較
feature
struts 1
struts 2
action類
在struts 1中要求action類繼承抽象的基類。在struts 1中一個普遍存在的問題就是面向抽象類程式設計,而不是面向接口程式設計。
struts 2中的action可以實作一個action接口,同時可以實作其他的接口,這樣可以使使用者有選擇性地使用其它自定義的服務。struts 2提供了基礎類actionsupport,該類實作了一些通用的接口。action接口不是必須的。任何具有execute方法的pojo對象都可以用作struts 2的action對象。
線程模型
struts 1的actions是單例的,因為隻有一個類的執行個體來處理所有對這個action的請求,是以必須是線程安全的。單例政策對struts 1的action的能夠完成的功能有很大限制,有些功能需要額外的努力才能完成。action資源必須是線程安全的或者synchronized
struts 2的action對象是為每個請求執行個體化的,是以沒有線程安全的問題。(在實踐中,servlet容器會為每個請求生成多個throw-away對象,增加的對象不會對性能産生太大影響或者對垃圾回收産生影響)
servlet依賴
struts 1的action依賴servlet api,因為當調用action的execute方法時需要傳參數httpservletrequest和httpservletresponse。
struts 2的action與容器不是緊密結合在一起的。多數情況下,servlet上下文被表示為map對象,允許對action進行獨立的測試。如果需要,struts 2的action仍然可以通路原始的request和response對象。 然而,其它架構元素可以減少或者消除對httpservetrequest和httpservletresponse對象進行直接通路的必要。
可測試性
測試struts 1 action的一個主要障礙就是execute方法使用了servlet api。1個第三方擴充struts testcase,為struts 1提供了一組模拟(mock)對象。
struts 2的action可以通過執行個體化、設定屬性和調用方法進行測試。依賴注入支援使測試更簡單。
擷取輸入
struts 1使用actionform對象來擷取輸入。像action一樣,所有的actionform必須繼承一個基類。因為其它的javabean不能用作actionform,開發人員經常需要建立多餘的類來擷取輸入。可以使用動态form來替換傳統的actionform類,但是開發人員同樣可能需要重新描述已有的javabean。
struts 2使用action的屬性作為輸入屬性,不用建立第二個輸入對象。輸入屬性可以是複雜的對象類型,還可以有自己的屬性。可以在頁面中通過taglib通路action屬性。struts 2也支援actionform模式,以及pojo表單對象和pojo action。複雜對象類型,包括業務或者域對象,都可以作為輸入/輸出對象。模型驅動的特性簡化了标簽庫對pojo輸入對象的引用。
表達式語言
struts 1內建了jstl,是以可以使用jstl的el語言,el提供了基本的對象結構周遊(object graph traversal),但是集合以及索引屬性支援比較弱。
struts 2可以使用jstl,同時struts還支援另外一種功能更強大、使用更靈活的表達式語言,這種語言是object graph notation language,簡稱ognl。
值與視圖的綁定
struts 1使用了标準的jsp機制把對象與要通路的頁面上下文綁定。
struts 2使用了一種valuestack技術,這樣标簽庫不用把視圖與要呈現的對象類型關聯就可以通路值。valuestack政策允許重用涉及多個類型的視圖,這些類型可能有相同的屬性名,但是屬性類型不同。
類型轉換
struts 1的actionform屬性通常都是字元串類型。struts 1 使用commons-beanutils進行類型轉換。轉換器是針對每個類的,而不能為每個執行個體配置。
struts 2使用ognl進行類型轉換,架構包含了常用對象類型和基本資料類型的轉換器。
驗證
struts 1支援手動驗證,通過actionform的validate方法或者通過繼承通用的驗證器來完成。對于同一個類可以有不同的驗證上下文環境,但是不能連結到對子類型的驗證。
struts 2支援通過驗證方法進行手工驗證和xwork驗證架構。xwork驗證架構支援對子屬性的連結驗證,使用為屬性類型定義的驗證規則和上下文。
action執行的控制
struts 1支援為每個子產品提供獨立的請求處理器(生命周期),但是同一個子產品中的所有action具有相同的生命周期。
struts 2通過攔截器棧支援為每個action建立不同的生命周期。必要的時候,可以使用不同的actio建立和使用自定義棧。
注:來自struts的官方網站:http://struts.apache.org/2.0.11.2/docs/comparing-struts-1-and-2.html
strust 2結構圖如圖2.1(原圖來自strust 2文檔)所示:
圖2.1 struts2結構圖
在處理一個請求的時候,主要使用3個類:action、interceptor和result
處理流程:
u 請求到達伺服器之後,首先經過一系列過濾器,有的是可選的,最主要的過濾器是filterdispatcher。所有的請求都會送出給它處理,該過濾器是在web.xml中配置的。配置代碼如下:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filterdispatcher</filter-class>
</filter>
<filter-mapping>
<url-pattern>/*</url-pattern>
</filter-mapping>
u filterdispatcher過濾器接收到請求之後調用actionmapper檢視是否需要調用action。actionmapper提供了httprequest與action調用請求之間的映射關系,可以決定目前請求是否需要調用action。如果actionmapper傳回的資訊表明需要調用action。filterdispatcher過濾器把控制前交給actionproxy;
u actionproxy調用配置檔案管理器configurationmanager,該管理器從struts.xml配置檔案中擷取配置資訊,擷取的資訊主要包括目前請求對應哪個action(對使用者的請求進行處理),對應哪些result(決定了如何對使用者響應),有時候還涉及攔截器。然後根據這些資訊建立actioninvocation對象,該對象負責具體的調用過程。struts.xml是使用者需要提供的最主要的配置檔案。下面是一個struts.xml配置檔案的部分内容。
<struts>
<package name="default" extends="struts-default">
<action name="logon" class="mailreader2.logon">
<result name="input">/pages/logon.jsp</result>
<result name="cancel" type="redirectaction">welcome</result>
<result type="redirectaction">mainmenu</result>
<result name="expired" type="chain">changepassword</result>
</action>
<action name="logoff" class="mailreader2.logoff">
<result type="redirectaction">welcome</result>
</package>
</struts>
u actioninvocation對象按照順序執行目前請求所對應的攔截器,攔截器能夠對請求進行預處理,例如驗證、檔案上傳等,并能夠對響應内容進行再處理。通常攔截器是由系統提供的,如果需要,程式設計人員隻需要進行配置即可。在調用action的方法之前,會調用攔截器的預處理方法;
u actioninvocation對象調用攔截器的預處理方法之後會調用action的execute方法,action中的代碼主要由程式設計人員根據功能進行編寫的,通常從資料庫檢索資訊或者向資料庫存儲資訊。action的方法傳回一個字元串。下面是一個簡單的action例子。
package simple;
import java.util.map;
import javax.servlet.http.httpsession;
import com.opensymphony.webwork.servletactioncontext;
import com.opensymphony.xwork.actionsupport;
public class logoutaction extends actionsupport {
public string execute() throws exception {
map session = actioncontext.getcontext().getsession();
session.remove("logined");
session.remove("context");
return success;
}
}
u actioninvocation對象根據action方法的傳回結果以及struts配置檔案生成result對象。result對象選擇一個模闆檔案來響應使用者,模闆檔案可以是jsp、freemarker和velocity。
u 容器加載并執行模闆檔案,使用在action中擷取的資訊對模版中的變量進行指派,也可能從資源檔案或者其他内部對象中擷取資訊。最終向浏覽器呈現的是html、pdf或者其他内容。
u 模闆檔案執行的結果會經過攔截器進行再處理,最後通過過濾器傳回給用戶端。
在該結構圖中,既包含了struts架構提供的基礎接口,也包括了使用者要編寫的檔案。其中,actionmapper、actionproxy、configurationmanager、actioninvocation和result是架構提供的核心類。過濾器和攔截器是架構提供的,使用者可以根據需要進行配置,當然也可以編寫自己的過濾器和攔截器。使用者需要編寫的檔案是struts.xml、action和模闆檔案,這些也是使用者在使用struts 2架構時需要做的工作。
架構為開發人員提供了大量的輔助類,使用者在使用架構開發的時候隻需要編寫很少檔案。在使用struts 2開發的時候,首先應該把環境搭建起來,然後使用struts 2提供的标簽開發界面,然後編寫action類,最後進行配置。
在進行具體的開發之前,需要先搭建環境。包括如下過程:
u 建立web工程;
u 加載struts 2的核心類庫,核心類庫包括commons-logging-1.0.4.jar、freemarker-2.3.8.jar、ognl-2.6.11.jar、struts2-core-2.0.11.2.jar和xwork-2.0.5.jar,把這些類庫放到web工程的web-inf/lib下面;
u 配置web.xml,主要配置struts中心控制器filterdispatcher,下面是1個例子。
<?xml version="1.0" encoding="utf-8"?>
<web-app id="webapp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>struts blank</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
u 建立struts.xml配置檔案,與類檔案放在一起,空白的struts檔案如下所示。在使用struts 2進行開發所有的配置基本上都在這個檔案中完成。也可以根據需要建立多個配置檔案,然後在這個配置檔案中使用<include file="example.xml"/>進行包含。
<?xml version="1.0" encoding="utf-8" ?>
<!doctype struts public
"-//apache software foundation//dtd struts configuration 2.0//en"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<constant name="struts.enable.dynamicmethodinvocation" value="false" />
<constant name="struts.devmode" value="false" />
<include file="example.xml"/>
<!-- add packages here -->
環境搭建完之後,在具體開發過程中主要完成3個方面的工作:
u 制作模闆檔案,可以使用jsp、freemarker或者velocity等;
u 編寫action,基本上每個動作對應1個action;
u 配置,主要在struts.xml中進行配置。
下面分别介紹。
模版檔案的主要作用是接收使用者輸入的資訊,并向使用者展示資訊。struts提供了多個标簽庫來簡化頁面的代碼量,使用标簽之後頁面也更容易維護。下面是一段标簽:
<s:actionerror/>
<s:form action="profile_update" validate="true">
<s:textfield label="username" name="username"/>
<s:password label="password" name="password"/>
<s:password label="(repeat) password" name="password2"/>
<s:textfield label="full name" name="fullname"/>
<s:textfield label="from address" name="fromaddress"/>
<s:textfield label="reply to address" name="replytoaddress"/>
<s:submit value="save" name="save"/>
<s:submit action="register_cancel" value="cancel" name="cancel"
onclick="form.onsubmit=null"/>
</s:form>
struts 2中提供了兩類通用标簽和3類界面标簽:
u 控制标簽
u 資料标簽
u form标簽
u non-form使用者接口标簽
u ajax标簽
下面對這些類型的标簽進行介紹。
控制标簽及其用法如表2.2所示。
表2.2 控制标簽
标簽名
描述
例子
if
與java中的if基本相同
<s:if test="%{false}">
<div>will not be executed</div>
</s:if>
<s:elseif test="%{true}">
<div>will be executed</div>
</s:elseif>
<s:else>
</s:else>
else if
與java中的else if基本相同
else
與java中的else基本相同
append
按照順序把多個疊代器的元素組合到一個疊代器中,保持原來的順序不變。
<s:append var="myappenditerator">
<s:param value="%{mylist1}" />
<s:param value="%{mylist2}" />
<s:param value="%{mylist3}" />
</s:append>
<s:iterator value="%{#myappenditerator}">
<s:property />
</s:iterator>
generator
根據val屬性的給定的值生成疊代器對象。
<s:generator val="%{'aaa,bbb,ccc,ddd,eee'}">
<s:iterator>
<s:property /><br/>
</s:iterator>
</s:generator>
iterator
對疊代器或者集合進行周遊,類似于java中的for-each循環。
<s:iterator value="#it.days" status="rowstatus">
<tr>
<s:if test="#rowstatus.odd == true">
<td style="background: grey"><s:property/></td>
</s:if>
<s:else>
<td><s:property/></td>
</s:else>
</tr>
merge
把多個疊代器的元素合并到一個疊代器中,合并後的順序為1.1,2.1,3.1,1.2,1.3…,1.1表示第1個疊代器的第1個元素。
<s:merge var="mymergediterator1">
</s:merge>
<s:iterator value="%{#mymergediterator1}">
sort
對list進行排序。
<s:sort var="mysortedlist" comparator="mycomparator" source="mylist" />
subset
擷取集合的子集。
<s:subset var="mysubset" source="mylist" count="13" start="3" />
資料标簽及其用法如表2.3所示。
表2.3 資料标簽
a
生成html的<a>
<s:a href="%{testurlid}"><img src="<s:url
value="/images/delete.gif"/>" border="none"/></s:a>
action
在jsp頁面中直接調用action
<s:action name="actiontagaction" executeresult="true" />
bean
執行個體化javabean對象
<s:bean name="org.apache.struts2.example.counter.simplecounter" var="counter">
<s:param name="foo" value="bar" />
the value of foot is : <s:property value="foo"/> <br />
</s:bean>
date
建立date對象
<s:date name="person.birthday" format="dd/mm/yyyy" />
debug
i18n
得到resourcebundle對象。
<s:i18n name="mycustombundle">
</s:i18n>
include
包含1個jsp或者servlet的輸出。
<s:include value="myjsp.jsp">
<s:param name="param1" value="value2" />
</s:include>
param
為其他标簽提供參數
參考上面的例子
property
擷取屬性值
參考bean标簽的例子
push
把值儲存起來使用
<s:push value="user">
<s:propery value="firstname" />
<s:propery value="lastname" />
</s:push>
set
把某個值儲存到某個作用範圍的變量中。
<s:set name="personname" value="person.name"/>
hello, <s:property value="#personname"/>. how are you?
text
呈現i18n的文本消息
<s:i18n name="struts.action.test.i18n.shop">
<s:text name="main.title"/>
url
用于生成url
<s:url value="editgadget.action">
<s:param name="id" value="%{selected}" />
</s:url>
form标簽及其用法如表2.4所示。
表2.4 form标簽
checkbox
生成複選框
<s:checkbox label="checkbox test" name="checkboxfield1"
value="aboolean" fieldvalue="true"/>
checkboxlist
生成多個複選框
<s:checkboxlist name="foo" list="bar"/>
combobox
輸入框與下拉框的組合。
<s:combobox
label="my favourite fruit"
name="myfavouritefruit"
list="{'apple','banana','grape','pear'}"
headerkey="-1"
headervalue="--- please select ---"
emptyoption="true"
value="banana" />
doubleselect
生成關聯菜單
<s:doubleselect label="doubleselect test1" name="menu"
list="{'fruit','other'}" doublename="dishes"
doublelist="top == 'fruit' ? {'apple', 'orange'} : {'monkey', 'chicken'}" />
head
生成html的head部分。
<head>
<title>my page</title>
<s:head/>
</head>
file
生成檔案輸入框
<s:file name="anuploadfile" accept="text/*" />
form
生成form表單
<p/>
<s:form ... />
hidden
生成隐藏域
<s:hidden name="foo" value="bar" />
label
生成标簽
<s:label key="username" />
optiontrans
-ferselect
生成兩個清單框,可以通過中間的按鈕把左邊的選項移動到右邊,也可以把右邊的選項移動到左邊。
<s:optiontransferselect
label="favourite cartoons characters"
name="leftsidecartooncharacters"
list="{'popeye', 'he-man', 'spiderman'}"
doublename="rightsidecartooncharacters"
doublelist="{'superman', 'mickey mouse', 'donald duck'}"
/>
optgroup
在select中提供選項
<s:select label="my selection"
name="myselection"
value="%{'popeye'}"
list="%{#{'superman':'superman', 'spiderman':'spiderman'}}">
<s:optgroup label="adult"
list="%{#{'south_park':'south park'}}" />
<s:optgroup label="japanese"
list="%{#{'pokemon':'pokemon','digimon':'digimon',
'sailormoon':'sailormoon'}}" />
</s:select>
select
生成下拉框
password
密碼輸入框
<s:password label="%{text('password')}" name="password"
size="10" maxlength="15" />
radio
單選按鈕
<s:radio label="gender" name="male" list="#genders.genders"/>
reset
重值按鈕
<s:reset value="reset" />
submit
送出按鈕
<s:submit value="ok" />
textarea
生成文本域
<s:textarea label="comments" name="comments" cols="30" rows="8"/>
textfield
生成輸入框
<s:textfield key="user" />
token
阻止表單重複送出
updownselect
建立元素能夠上下移動的清單框
<s:updownselect
list="#{'england':'england', 'america':'america', 'germany':'germany'}"
name="prioritisedfavouritecountries"
headerkey="-1"
headervalue="--- please order them accordingly ---"
emptyoption="true" />
non-form ui标簽及其用法如表2.5所示。
表2.5 non-form标簽
actionerror
呈現錯誤資訊
<s:actionerror />
actionmessage
呈現提示資訊
<s:actionmessage />
component
建立自定義元件
<s:component template="/my/custom/component.vm"/>
div
生成html <div>
fielderror
輸出關于輸入元素的錯誤資訊
<s:fielderror>
<s:param>field1</s:param>
<s:param>field2</s:param>
</s:fielderror>
<s:form .... >
....
</s:form>
ajax标簽包括a、autocompleter、bind、datetimepicker、div、head、submit、tabbedpanel、textarea、tree、treenode等。具體用法參考struts 2幫助文檔。
針對每個功能可以編寫1個action,也可以多個功能共享1個action。action完成的主要功能包括:
u 擷取使用者的輸入資訊,這個擷取的過程是由架構完成的,但是使用者需要在action中定義與使用者輸入表單元素名字相同的成員變量,關鍵是要提供對成員變量指派的set方法,這樣架構在擷取使用者輸入資訊之後會調用set方法把值賦給action的成員變量。
u 根據使用者的請求資訊,調用完成業務邏輯的javabean。如果希望要把某些執行結果傳遞給模闆檔案(jsp、freemarker和velocity等),需要在action中定義成員變量來表示這些結果,最關鍵的是要定義get方法,這樣在執行模版檔案的時候會通過get方法來擷取這些資訊。
u 根據執行的結果,傳回1個字元串,這個字元串決定了使用什麼模闆對使用者進行響應。
下面是1個簡單的例子。
public class loginaction extends actionsupport {
private string userid;
private string passwd;
// 對userid和passwd操作的setter和getter方法
if ("admin".equals(userid) && "password".equals(passwd)) {
map session = actioncontext.getcontext().getsession();
session.put("logined","true");
session.put("context", new date());
return success;
}
return error;
注意:并不是必須繼承actionsupport,主要提供execute方法即可。
通過配置檔案struts.xml對web應用的流程進行管理,包括action映射和result處理,前者把請求與action關聯起來,後者把action執行的結果與響應界面關聯起來。下面是一段配置。下面是一個簡單的例子。
struts 2中完成的主要配置如表2.6所示。
表2.6 struts 2的主要配置資訊
配置元素
javabean
<bean type="com.opensymphony.xwork2.objectfactory"
name="myfactory" class="com.company.myapp.myobjectfactory" />
常量
<constant name="struts.devmode" value="true" />
包
<package name="employee" extends="struts-default" namespace="/employee">
...
</package>
命名空間
包含
<include file="home.xml"/>
攔截器
<interceptors>
<interceptor name="security"
class="com.company.security.securityinterceptor"/>
<interceptor-stack name="securestack">
<interceptor-ref name="security"/>
<interceptor-ref name="defaultstack"/>
</interceptor-stack>
</interceptors>
引用攔截器
<action name="velocitycounter"
class="org.apache.struts2.example.counter.simplecounter">
<result name="success">...</result>
<interceptor-ref name="defaultcomponentstack"/>
</action>
全局result:
<global-results>
<result name="error">/error.jsp</result>
<result name="invalid.token">/error.jsp</result>
</global-results>
result
異常配置
在action中使用:
<exception-mapping exception="com.company.securityexception" result="login"/>
全局:
<global-exception-mappings>
<exception-mapping exception="java.sql.sqlexception" result="sqlexception"/>
<exception-mapping exception="java.lang.exception" result="exception"/>
</global-exception-mappings>
struts 2提供了大量的攔截器,使用者可以根據需要調用。
struts 2的配置檔案struts.xml的dtd定義如下。
<!--
struts configuration dtd.
use the following doctype
<!doctype struts public
"-//apache software foundation//dtd struts configuration 2.0//en"
"http://struts.apache.org/dtds/struts-2.0.dtd">
-->
<!element struts (package|include|bean|constant)*>
<!element package (result-types?, interceptors?, default-interceptor-ref?, default-action-ref?, default-class-ref?, global-results?, global-exception-mappings?, action*)>
<!attlist package
name cdata #required
extends cdata #implied
namespace cdata #implied
abstract cdata #implied
externalreferenceresolver nmtoken #implied
>
<!element result-types (result-type+)>
<!element result-type (param*)>
<!attlist result-type
class cdata #required
default (true|false) "false"
<!element interceptors (interceptor|interceptor-stack)+>
<!element interceptor (param*)>
<!attlist interceptor
<!element interceptor-stack (interceptor-ref*)>
<!attlist interceptor-stack
<!element interceptor-ref (param*)>
<!attlist interceptor-ref
<!element default-interceptor-ref (param*)>
<!attlist default-interceptor-ref
<!element default-action-ref (param*)>
<!attlist default-action-ref
<!element default-class-ref (param*)>
<!attlist default-class-ref
<!element global-results (result+)>
<!element global-exception-mappings (exception-mapping+)>
<!element action (param|result|interceptor-ref|exception-mapping)*>
<!attlist action
class cdata #implied
method cdata #implied
converter cdata #implied
<!element param (#pcdata)>
<!attlist param
<!element result (#pcdata|param)*>
<!attlist result
name cdata #implied
type cdata #implied
<!element exception-mapping (#pcdata|param)*>
<!attlist exception-mapping
exception cdata #required
result cdata #required
<!element include (#pcdata)>
<!attlist include
file cdata #required
<!element bean (#pcdata)>
<!attlist bean
scope cdata #implied
static cdata #implied
optional cdata #implied
<!element constant (#pcdata)>
<!attlist constant
value cdata #required
功能:登入。
涉及的檔案有:
l login.jsp,用于輸入登入資訊;
l welcome.jsp,登入之後的歡迎界面;
l logincheck.jsp,判斷使用者是否登入;
l loginaction.java,完成登入業務處理,正常情況下會調用其他業務邏輯javabean來完成;
l logoutaction.java,完成退出業務處理;
l struts.xml,應用的配置檔案。
源檔案:login.jsp
<%@ page language="java" contenttype="text/html; charset=iso-8859-1"
pageencoding="iso-8859-1"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<title>insert title here</title>
</head><body>
<form action="login.action" method="post">
user id<input type="text" name="userid" /> <br/>
password <input type="password" name="passwd" /> <br />
<input type="submit" value="login"/>
</form>
</body>
</html>
pageencoding="iso-8859-1"%>
<%@ taglib prefix="ww" uri="/webwork" %>
<jsp:include page="web-inf/inc/logincheck.jsp" />
<title>welcome</title>
<body>welcome, you have logined. <br />
the attribute of 'context' in session is
<ww:property value="#session.context" />
<br /><br /><br />
<a xhref="<%= request.getcontextpath() %>/logout.action">logout</a>
<br />
<a xhref="<%= request.getcontextpath() %>/logout2.action">logout2</a>
<%@ taglib="/webwork" prefix="ww" %>
<ww:if test="#session.login != 'true'">
<jsp:forward page="<%= request.getcontextpath() %>/login.jsp" />
</ww:if>
import java.util.date;import java.util.map;
// httpsession session = servletactioncontext.getrequest().getsession();
// session.setattribute("logined","true");
// session.setattribute("context", new date());
// better is using actioncontext
map session = actioncontext.getcontext().getsession();
session.put("logined","true");
public string logout() throws exception {
// httpsession session = servletactioncontext.getrequest().getsession();
// session.removeattribute("logined");
// session.removeattribute("context");
map session = actioncontext.getcontext().getsession();
session.remove("logined");
session.remove("context");
public string getpasswd() {
return passwd;
public void setpasswd(string passwd) {
this.passwd = passwd;
public string getuserid() {
return userid;
public void setuserid(string userid) {
this.userid = userid;
session.remove("logined");
session.remove("context");
}
<!doctype xwork public "-//opensymphony group//xwork 1.1.1//en" "http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">
<xwork>
<include file="webwork-default.xml"/>
<package name="default" extends="webwork-default">
<!-- add your actions here -->
<action name="login" class="simple.loginaction" >
<result name="success" type="dispatcher">/pages/welcome.jsp</result>
<result name="error" type="redirect">/login.jsp</result>
<action name="logout2" class="simple.loginaction" method="logout" >
<result name="success" type="redirect">/login.jsp</result>
<action name="logout" class="simple.logoutaction" >
</xwork>