Struts 2 Hello World Annotation Example
来源:互联网 发布:葛航 创业软件 知乎 编辑:程序博客网 时间:2024/06/07 00:16
In this tutorials, it will reuse the previous Strust 2 Hello World (XML version) example, and convert it into annotation version.
Struts 2 Annotation Concept
The Struts 2 annotation is supported by Struts 2 convention plugin, So, you have to understand the magic behind its “Scanning Methodology” and “Naming Converter” mechanism.
1. Scanning Methodology
Many Struts 2 articles or books stated that you can configure the filter’s “init-param
” or “struts.convention.action.packages
” to tell Struts 2 where to scan the annotated classes. For example,
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.mkyong.common</param-value> </init-param></filter>
From my testing (Struts2 version 2.1.6 and 2.1.8), this is not true, no matter what you put in the “param-value
” or “struts.convention.action.packages
“, Struts 2 will just ignore it and scan the specified folders named struts, struts2, action or actions only.
Here’s how the scanning work
- Scan the annotated classes which located at the packaged named “struts, struts2, action or actions“.
- Next, scan the file which match either of the following criteria :
- Implemented the
com.opensymphony.xwork2.Action
interface. - Extends the
com.opensymphony.xwork2.ActionSupport
class.
- Implemented the
- File name ends with
Action
(e.gUserAction
,LoginAction
).
2. Naming Converter
Struts 2 convention plugin will convert all the annotated action file name to a specified format.
For example : LoginAction.java
First, remove the “Action
” word at the end of the file name, if present.
Second, convert the first letter of the file name to lowercase.
So, After removing the ending and converting the case of the first letter, the LoginAction.action
will change to login.action
.
The Struts 2 convention plugin’s “scanning methodology” and “naming converter” features are really bring a lot of conveniences and benefits, only if your Struts 2 project is following the naming convention properly; otherwise it will be a total disaster.
Struts 2 Annotation example
It’s time to start the conversion process.
Final project structure
1. Update pom.xml
To use the Struts 2 annotation feature, you need to download the struts2-convention-plugin.jar
.
pom.xml
... <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.1.8</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-convention-plugin</artifactId> <version>2.1.8</version> </dependency>...
2. LoginAction
Create a LoginAction
extends ActionSupport
and do nothing, the ActionSupport
default return a “success
” string, which will match the @Result
and redirect to “pages/login.jsp
“.
Annotation version
package com.mkyong.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="pages/login.jsp")public class LoginAction extends ActionSupport{}
XML equivalent
<package name="user" namespace="/User" extends="struts-default"> <action name="Login"> <result>pages/login.jsp</result> </action></package>
3. WelcomeUserAction
Override the execute()
method and specified the @Action
and @Result
annotation.
Annotation version
package com.mkyong.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="pages/welcome_user.jsp") }) public String execute() { return SUCCESS; }}
XML equivalent
<package name="user" namespace="/User" extends="struts-default"> <action name="Welcome" class="com.mkyong.user.action.WelcomeUserAction"> <result name="SUCCESS">pages/welcome_user.jsp</result> </action></package>
The Struts 2 annotations – @Action
, @Result
and @Namespace
are self-explanatory, you can always compare it with the XML equivalent. @Action
是用在方法上面的,而不是类上的
4. JSP view pages
Normal JSP view pages to accept a username and password, and redirect to a welcome page once the submit button is clicked.
login.jsp
<%@ page contentType="text/html; charset=UTF-8" %><%@ taglib prefix="s" uri="/struts-tags" %>Struts 2 Hello World Annotation Example
welcome_user.jsp
<%@ page contentType="text/html; charset=UTF-8" %><%@ taglib prefix="s" uri="/struts-tags" %>Struts 2 Hello World Annotation ExampleHello
5. struts.xml
No need to create struts.xml
file, all classes are annotated.
6. web.xml
Just create a classic web.xml
file and declared the FilterDispatcher
filter as normal.
<!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. Run it
The LoginAction.action
is changed to login.action
, see “Naming Converter” above.
http://localhost:8080/Struts2Example/User/login.action
http://localhost:8080/Struts2Example/User/Welcome.action
- Struts 2 Hello World Annotation Example
- Struts 2 Hello World Annotation Example
- Struts 2 Hello World Annotation Example
- Struts 2 Hello World Example
- Struts 2 Hello World Example
- Struts 2 Hello World Example
- Struts 2 Hello World Example
- Struts 2 Hello World Example
- Struts Hello World Example
- Spring Security Hello World Annotation Example
- Spring MVC hello world annotation example
- Spring MVC hello world annotation example
- Spring MVC hello world annotation example
- Spring MVC hello world annotation example
- Struts 2 @ResultPath annotation example
- Struts 2 @ResultPath annotation example
- Struts 2 @ResultPath annotation example
- Gradle – Spring 4 MVC Hello World Example – Annotation
- lib文件配置的3种方法
- POJ 1039:Pipe 计算几何
- 基于jquery的fileupload上传控件 带进度条、支持多上传、拖拽上传 下载即可用 十分强大 有图
- 排序二叉树,平衡二叉树和红黑树的概念以及相关的操作讲解
- 使用Jmai发送邮件
- Struts 2 Hello World Annotation Example
- LINK:fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- Java EE文件上传
- 关于JSP九大内置对象、四大作用域和session的生命周期
- 如何用两个队列实现一个栈,并分析有关栈操作的运行时间。
- LeetCode|Majority Element-java
- $ git push origin test:master // 提交本地test分支 作为 远程的master分支
- css里面常用一些通用样式写法
- 对于IT人非常有价值的博客,常来看看。