Struts 2 Hello World Annotation Example
2015-09-15 14:35
525 查看
In this tutorials, it will reuse the previous Strust 2 Hello World (XML version) example, and convert it into annotation version.
From my testing (Struts2 version 2.1.6 and 2.1.8), this is not true, no matter what you put in the “
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
Extends the
File name ends with Action (e.g
For example :
First, remove the “
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
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.
Final project structure
1. Update
To use the Struts 2 annotation feature, you need to download the
2.
Create a
pages/login.jsp
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{
}
““
XML equivalent
The Struts 2 annotations –
5.
No need to create
6.
Just create a classic
http://localhost:8080/Struts2Example/User/login.action
http://localhost:8080/Struts2Example/User/Welcome.action
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.Actioninterface.
Extends the
com.opensymphony.xwork2.ActionSupportclass.
File name ends with Action (e.g
UserAction,
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.actionwill 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 LoginActionextends
ActionSupportand do nothing, the
ActionSupportdefault return a “
success” string, which will match the
@Resultand 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
pages/login.jsp
##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,
@Resultand
@Namespaceare self-explanatory, you can always compare it with the XML equivalent. The
@ResultPathmay need a bit explanation, see this
@ResultPathexample.
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.xmlfile, all classes are annotated.
6. web.xml
Just create a classic web.xmlfile and declared the
FilterDispatcherfilter 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
TheLoginAction.actionis changed to login.action, see “Naming Converter” above.
http://localhost:8080/Struts2Example/User/login.action
http://localhost:8080/Struts2Example/User/Welcome.action
相关文章推荐
- JQuery+Strusts1.x无刷新登录
- XML 与 JSON 优劣对比
- As3.0 xml + Loader应用代码
- 网马生成器 MS Internet Explorer XML Parsing Buffer Overflow Exploit (vista) 0day
- ext读取两种结构的xml的代码
- C#针对xml基本操作及保存配置文件应用实例
- asp下查询xml的实现代码
- sqlserver FOR XML PATH 语句的应用
- 使用sp_xml_preparedocument处理XML文档的方法
- C#中的Linq to Xml详解
- C#操作XML文件实例汇总
- SQL Server中的XML数据进行insert、update、delete
- SQL Server中的XML数据进行insert、update、delete操作实现代码
- 关于SQLServer2005的学习笔记 XML的处理
- C#通过DataSet读写xml文件的方法
- C#实现基于XML配置MenuStrip菜单的方法
- php xml 入门学习资料
- Zend 输出产生XML解析错误
- PHP遍历XML文档所有节点的方法