鍍金池/ 教程/ Java/ Struts2注解示例
設(shè)置復(fù)選框的默認(rèn)值
<s:if>, <s:elseIf>, <s:else> 標(biāo)簽示例
下拉框示例
Struts2 &lt;s:bean&gt;標(biāo)簽示例
Struts2 &lt;s:date&gt;標(biāo)簽示例
<s:text>標(biāo)簽示例
Struts2的ActionError&ActionMessage示例
Struts2 MySQL數(shù)據(jù)庫(kù)訪問
Struts2上傳多個(gè)文件例子
Struts2+Log4j集成
Struts 2 hello world (XML版本)
Struts2和Spring集成
Struts2 控件標(biāo)簽
Struts2 和 JSON 實(shí)例
Struts2 環(huán)境配置
Struts2 &lt;s:checkboxlist&gt;多個(gè)復(fù)選框例子
Struts2 注釋類型
在Struts2中配置靜態(tài)參數(shù)
Struts2 驗(yàn)證框架
Struts2 param 標(biāo)簽
Struts2 &lt;s:hidden&gt;隱藏值例子
Struts2下載文件實(shí)例
Struts2 &lt;sx:autocompleter&gt;自動(dòng)提示例子
Struts2 action標(biāo)簽
autocompleter自動(dòng)完成示例
Struts2 merge標(biāo)簽
Struts2 &lt;s:action&gt;標(biāo)簽示例
<s:property>標(biāo)簽示例
Struts2 &lt;s:sort&gt;標(biāo)簽示例
Struts2和Hibernate集成
自動(dòng)選擇下拉框示例
Struts2 結(jié)果和結(jié)果類型
Struts2 是什么?
如何刪除Struts2動(dòng)作的后綴擴(kuò)展名
Struts2 Hello World 實(shí)例
<s:action>標(biāo)簽示例
<s:bean>標(biāo)簽示例
Struts2 + Quartz調(diào)度集成實(shí)例
如何獲取ServletContext對(duì)象
Struts2 &lt;s:i18n&gt;標(biāo)簽示例
Struts2 append標(biāo)簽
i18n 或本地化示例
autocompleter + JSON 示例
Struts2獲取HttpServletRequest實(shí)例
updownselect 示例
Struts2 &lt;s:debug&gt;標(biāo)簽示例
Struts2重寫攔截器參數(shù)
多個(gè)Struts配置文件示例
<s:i18n>標(biāo)簽示例
Struts2中文本地化問題
文件上傳示例
Struts2 &lt;s:property&gt;標(biāo)簽實(shí)例
Struts2攔截器棧的例子
<s:merge>標(biāo)簽示例
Struts2 數(shù)據(jù)標(biāo)簽
Struts2 &lt;s:merge&gt;標(biāo)簽示例
復(fù)選框checkboxes 示例
Struts2 generator標(biāo)簽
如何配置Struts2全局資源包
key 屬性示例
Struts2 注釋
FilterDispatcher 和 StrutsPrepareAndExecuteFilter區(qū)別?
Struts 2 Hello World (注解版本)
Struts 2 + Log4j 集成實(shí)例
Struts2 值棧/OGNL
Struts2配置Action類的靜態(tài)參數(shù)
Struts2 本地化/國(guó)際化(i18n)
Chinese 本地化問題
Struts2單選預(yù)選按鈕值
Struts2 include標(biāo)簽
Struts2 + Hibernate 集成實(shí)例
配置全局資源包
Struts2 異常處理
Struts2 &lt;s:set&gt;標(biāo)簽示例
Struts2 &lt;s:doubleselect&gt;示例
Struts2 iterator標(biāo)簽
optiontransferselect 示例
Struts2映射攔截動(dòng)作
head 示例
Struts2+Hibernate使用Full Hibernate Plugin集成
Struts2命名空間配置和解釋
<s:param>標(biāo)簽示例
Struts2+Hibernate集成實(shí)例
Struts2 &lt;s:head&gt;示例
Struts2 &lt;s:push&gt;標(biāo)簽示例
Struts2文件上傳例子
Struts2 &lt;s:param&gt;標(biāo)簽示例
@ResultPath 注釋示例
Struts2 體系結(jié)構(gòu)
Struts2 execAndWait攔截器例子
Struts2 攔截器
Textarea - 文本域
Struts2教程
<s:push>標(biāo)簽示例
Struts2 文件上傳
Struts2 date 標(biāo)簽
Struts2 &lt;s:texttag&gt;標(biāo)簽示例
Struts2+Quartz2調(diào)度集成實(shí)例
Struts2開發(fā)模式
Struts2 autocompleter+JSON例子
Struts2 類型轉(zhuǎn)換
<s:url>標(biāo)簽示例
Struts2 Ajax 標(biāo)簽
Struts2 bean 標(biāo)簽
<s:iterator>標(biāo)簽迭代示例
Hidden隱藏值示例
在Struts2獲取HttpServletResponse對(duì)象
Struts2 主題和模板
Struts2自動(dòng)選擇下拉框的值
Struts2下載文件實(shí)例
在Struts2中獲取 HttpServletRequest
級(jí)聯(lián)選擇示例
Struts2 &lt;sx:datetimepicker&gt;示例
Struts2 include(包含)多個(gè)配置文件
Struts2 &lt;s:select&gt;下拉框例子
Struts2 Action/動(dòng)作
Struts2 &lt;s:textarea&gt;文本域?qū)嵗?/span>
Struts2+Spring集成實(shí)例
Struts2 &lt;s:updownselect&gt;示例
Struts2 property 標(biāo)簽
Struts2 &lt;s:url&gt;標(biāo)簽示例
Struts2 表單標(biāo)簽
Struts2資源包使用示例
subset tag example
Struts 2 ActionError &amp; ActionMessage Example
Struts2 MVC架構(gòu)
Struts2 if/else標(biāo)簽
Struts2 &lt;s:generator&gt;標(biāo)簽示例
<s:include>標(biāo)簽示例
Struts2開發(fā)者模式
<s:generator> 標(biāo)簽示例
復(fù)選框heckbox 示例
Struts2 text 標(biāo)簽
Radio 單選按鍵示例
Struts2 &lt;s:a&gt;標(biāo)簽示例
Struts2 key鍵屬性示例
Struts2設(shè)置多個(gè)復(fù)選框缺省值
Struts2 基于XML校驗(yàn)
Struts2 配置文件
Struts2 &lt;s:radio&gt;單選按鈕例子
Struts2 發(fā)送郵件Email
Struts2獲取HttpServletResponse實(shí)例
<s:sort>標(biāo)簽示例
Struts2 &lt;s:optiontransferselect&gt;示例
Struts2和Tiles集成
Struts2模型驅(qū)動(dòng)實(shí)例
Struts2的FilterDispatcher和StrutsPrepareAndExecuteFilter有什么不同?
<s:append>標(biāo)簽示例
組合框示例
Struts2 &lt;s:password&gt;密碼示例
datetimepicker 日期選擇
Struts2 &lt;s:iterator&gt;迭代器標(biāo)簽示例
TextBox文檔示例
Struts2 url標(biāo)簽
資源包使用示例
Struts2本地化和國(guó)際化
Struts2 &lt;s:subset&gt;標(biāo)簽示例
Struts2 + Hibernate使用“Full Hibernate Plugin"插件集成
Struts2和JSON實(shí)例
Struts2注解示例
Struts2 &lt;s:checkbox&gt;復(fù)選框例子
Struts2快速入門
<s:date>標(biāo)簽示例
Struts2 set 標(biāo)簽
<s:set>標(biāo)簽示例
Struts2 命名空間配置和解釋
Struts 2 hello world (XML版本)
&nbsp;預(yù)選單選按鈕
<s:a>標(biāo)簽示例
Struts2+Spring+Hibernate集成實(shí)例
Struts2 &lt;s:combobox&gt;組合框的例子
Struts2 if,elseif,else標(biāo)簽示例
Struts2 @ResultPath注釋示例
struts2 &lt;s:append&gt;標(biāo)簽示例
Struts2 &lt;s:textfield&gt;文本示例
多文件上傳示例
使用Struts2動(dòng)作
<s:debug>標(biāo)簽示例
在Struts2中如何獲取ServletContext對(duì)象
Struts2 &lt;s:include&gt;標(biāo)簽示例
Password 示例

Struts2注解示例

在這個(gè)教程,我們重復(fù)使用以前 STRUST2  Hello World(XML版本)的例子,并將其轉(zhuǎn)換成注解版本。

Struts2 注解概念

Struts2注解是由Struts 2的約定插件的支持,所以,必須要了解其背后的“掃描方法”和“命名轉(zhuǎn)換”機(jī)制的魔力。

1. 掃描方法

許多Struts 2的文章或書籍說,可以配置過濾器的“init-param”或“struts.convention.action.packages”告訴Struts2,其中掃描注解的類。 例如,

web.xml

<filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  <init-param>
	<param-name>actionPackages</param-name>
	<param-value>com.yiibai.common</param-value>
  </init-param>
</filter>

從測(cè)試(Struts22.1.6和2.1.8版本),這是不正確的,不管你把在“param-value”還是 “struts.convention.action.packages“, 在Struts 2會(huì)忽略它,并只掃描指定的文件夾命名:struts, struts2, action 或 actions 。

下面是掃描工作
  1. 掃描其位于包的命名注解的類 “struts, struts2, action 或 actions“.
  2. 接著,掃描相匹配下列任一條件的文件:
    • 實(shí)例了 com.opensymphony.xwork2.Action 接口。
    • 擴(kuò)展了 com.opensymphony.xwork2.ActionSupport 類
    • 文件名用動(dòng)作(例如:UserAction,LoginAction)結(jié)束

詳細(xì)請(qǐng)查看這里Struts 2 約定插件文件

2. 命名轉(zhuǎn)換器

Struts 2的約定插件將所有的注解操作文件名轉(zhuǎn)換為指定的格式。

例如 : LoginAction.java

  1. 首先,去掉“Action”字符在文件名的末尾,如果存在的話。
  2. 其次,轉(zhuǎn)換文件名的第一個(gè)字母為小寫。
因此,去除結(jié)束并轉(zhuǎn)換第一個(gè)字母為小寫后,LoginAction.action 將變?yōu)?login.action。
Struts2約定插件的“掃描方法”和“命名轉(zhuǎn)換”特性真正帶來了很多的便利和好處,只有當(dāng)你的Struts2項(xiàng)目正確下面的命名約定才會(huì)帶來好處; 否則,這將是一場(chǎng)災(zāi)難。

Struts 2 注解例子

現(xiàn)在是時(shí)候開始轉(zhuǎn)換過程了,我們使用MyEclipse 10 創(chuàng)建一個(gè)工程為:struts2example。

最終的項(xiàng)目結(jié)構(gòu)

2. LoginAction

擴(kuò)展ActionSupport并創(chuàng)建了LoginAction,什么也不做,ActionSupport 默認(rèn)返回 “success” 字符串,這將匹配 @Result 并重定位到 “pages/login.jsp“.

注解版本

package com.yiibai.user.action;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;

import com.opensymphony.xwork2.ActionSupport;

@Namespace("/User")
@ResultPath(value="/")
@Result(name="success",location="/login.jsp")
public class LoginAction extends ActionSupport{

}

XML 實(shí)現(xiàn)版本

<package name="user" namespace="/User" extends="struts-default">
	<action name="Login">
		<result>/login.jsp</result>
	</action>
</package>

3. WelcomeUserAction

重寫execute()方法并指定 @Action 和 @Result 注解。

注解版本

package com.yiibai.user.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;

import com.opensymphony.xwork2.ActionSupport;

@Namespace("/User")
@ResultPath(value="/")
public class WelcomeUserAction extends ActionSupport{

	private String username;
	 
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Action(value="Welcome", results={
		@Result(name="success",location="welcome_user.jsp")
	})
	public String execute() {

		return SUCCESS;

	}
}

XML 實(shí)現(xiàn)版本

<package name="user" namespace="/User" extends="struts-default">
   <action name="Welcome" class="com.yiibai.user.action.WelcomeUserAction">
	<result name="SUCCESS">/welcome_user.jsp</result>
   </action>
</package>
Struts 2 注解 – @Action, @Result 和 @Namespace 不言自明,可以將它與XML比較。@ResultPath 可能需要一點(diǎn)點(diǎn)的解釋,請(qǐng)參閱本 @ResultPath示例

4. JSP視圖頁(yè)面

普通JSP視圖頁(yè)面來接受用戶名和密碼后點(diǎn)擊提交按鈕,并重定向到一個(gè)歡迎頁(yè)面。

login.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head></head>
<body>
	<h1>Struts 2 注解示例</h1>

	<s:form action="Welcome">
		<s:textfield name="username" label="用戶名" />
		<s:password name="password" label="密碼" />
		<s:submit value="提交"/>
	</s:form>

</body>
</html>

welcome_user.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head></head>
<body>
<h1>Struts 2 注解示例</h1>

<h4>您好, <s:property value="username"/></h4>

</body>
</html>

5. struts.xml

所有類注解無需創(chuàng)建 struts.xml 文件。

6. web.xml

只要?jiǎng)?chuàng)建一個(gè)典型的web.xml文件,并聲明FilterDispatcher過濾器標(biāo)準(zhǔn)。

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Struts 2 Web Application</display-name>
  
  <filter>
	<filter-name>struts2</filter-name>
	<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  
  <filter-mapping>
	<filter-name>struts2</filter-name>
	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
</web-app>

7. 運(yùn)行測(cè)試

LoginAction.action 改為 login.action,請(qǐng)參閱上面的“命名轉(zhuǎn)換器”。

http://localhost:8080/struts2example/User/login.action

提交到 http://localhost:8080/Struts2Example/User/Welcome.action 后顯示:

參考

  1. Struts 2 約定插件文檔
  2. Strust 2 Hello World (XML 版本)