Struts 2 Annotations: @Namespace, @Results
May 16, 2014
In this page, we will learn about @Namespace and @Results struts annotation. @Namespace hides the package structure from URL and @Results defines the response of action class. The usability of these annotation has been defined below in detail.
@Namespace Annotation in Struts 2
@Namespace is used at class level or package level. This helps to change the namespace for action class. While accessing or calling action class, it hides package structure. When namespace is applied at package level, all the action of that package gets that namespace as default.@Namespace("/user") @Action("/form") @Result(name="success",location="/user/user.jsp") public class UserAction extends ActionSupport{ public String execute() { return SUCCESS; } }
http://localhost:8080/Struts2Demo-1/user/form
will access UserAction class.
Package level @Namespace can be defined as below.
@org.apache.struts2.convention.annotation.Namespace("/user") package com.concretepage.action;
@Result and @Results Annotation in Struts 2
@Result and @Results define the response of action class. These annotations can be applied at class level as well method level. @Result defines one response and @Results defines more than one response.@Namespace("/user") @Results({ @Result(name="success", location="/user/user.jsp"), @Result(name="failure", location="/user/error.jsp"), }) public class UserAction extends ActionSupport{ @Action("/one") public String one() { return "success"; } @Action("/two") public String two() { return "failure"; } }
will respond to user.jsp and
http://localhost:8080/Struts2Demo-1/user/two
will respond to error.jsp