Struts 2 Hello World Annotation Example

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.
    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.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
Struts2 hello world annotation

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.

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 Example

Hello 

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

Struts 2 annotation login screen
http://localhost:8080/Struts2Example/User/Welcome.action

Struts 2 annotation welcome screen

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值