Руководство Struts2 Namespace
View more Tutorials:
@Namespace это annotation (аннотация) может использоваться чтобы аннотировать на уровне (level) пакета (package) или класса (class). И действует на класс Action который находится в пакете аннотации или на класс Action аннотированный @Namespace.
Обычно, если ваш класс Action не аннотирован черезi @Namespace, и не находится в package который аннотируется через @Namespace, по умолчанию будет считаться аннотированным в @Namespace(value = "/")
// Configure an Action: @Action(value = "hello", // results = { // ... ) public class HelloAction extends ActionSupport // ================================== // Same as: @Namespace(value ="/") @Action(value = "hello", // results = { // ... ) public class HelloAction extends ActionSupport
Иллюстрация ниже показывает способ войти в Action который находится в namespace.

@Namespace аннотированный на package:
@Namespace("/path1/path2") package org.o7planning.struts2namespace.action;
В Eclipse создайте пустой Maven Web App Project с названием Struts2Namespace.

Конфигурировать Struts2 в web.xml:
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Struts2Namespace</display-name> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> </web-app>
Конфигурировать maven:
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.o7planning</groupId> <artifactId>Struts2Namespace</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>Struts2Namespace Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- Servlet Library --> <!-- http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- http://mvnrepository.com/artifact/org.apache.struts/struts2-core --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.20</version> </dependency> <!-- http://mvnrepository.com/artifact/org.apache.struts/struts2-convention-plugin --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-convention-plugin</artifactId> <version>2.3.20</version> </dependency> </dependencies> <build> <finalName>Struts2Namespace</finalName> <plugins> <!-- Config: Maven Tomcat Plugin --> <!-- http://mvnrepository.com/artifact/org.apache.tomcat.maven/tomcat7-maven-plugin --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <!-- Config: contextPath and Port (Default: /Struts2Namespace : 8080) --> <!-- <configuration> <path>/</path> <port>8899</port> </configuration> --> </plugin> </plugins> </build> </project>

AboutUsAction.java
package org.o7planning.struts2namespace.action; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.Result; import com.opensymphony.xwork2.ActionSupport; @Namespace(value = "/web/info") @Action(value = "aboutUs", // results = { // @Result(name = "success", location = "/WEB-INF/pages/aboutUs.jsp") // } // ) public class AboutUsAction extends ActionSupport { private static final long serialVersionUID = 1L; @Override public String execute() { return "success"; } }
/WEB-INF/pages/aboutUs.jsp
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>About Us</title> </head> <body> <h3>About o7planning</h3> <p>Address: Vietnam</p> <p>Contact: ... </p> </body> </html>


Ввод:
- Name: Run Struts2Namespace
- Base directory: ${workspace_loc:/Struts2Namespace}
- Goals: tomcat7:run

