Struts 2 Annotations: @Namespace, @Results

By Arvind Rai, 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;
	}
}
The above class can be accessed by @Namespace + @Action that is /user/form
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";
	}
}
http://localhost:8080/Struts2Demo-1/user/one
will respond to user.jsp and
http://localhost:8080/Struts2Demo-1/user/two
will respond to error.jsp
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us