Apache Struts 2 's namespace combat

Source: Internet
Author: User

Apache Struts 2 namespace Combat Chszs, copyright, without consent, may not be reproduced. Blogger Home: Http://blog.csdn.net/chszs

The Apache Struts 2 namespace is a new concept that enables multi-module processing by assigning a single namespace to each module. In addition, it solves the problem of name conflict of the same acion name under different modules.
From the figure below we can understand how the URL matches the Struts 2 namespace.

One, the configuration of the namespace

Below we illustrate how it matches URLs and directories by using the Struts2 namespace configuration instance. To illustrate a point, the package name (that is, the "default") does not affect the URL match result, it is just a meaningful name.
Struts.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"><struts>    <package name="default" namespace="/" extends="struts-default">        <action name="SayWelcome">            <result>pages/welcome.jsp</result>        </action>    </package>    <package name="common" namespace="/common" extends="struts-default">        <action name="SayWelcome">            <result>pages/welcome.jsp</result>        </action>    </package>    <package name="user" namespace="/user" extends="struts-default">        <action name="SayWelcome">            <result>pages/welcome.jsp</result>        </action>    </package></struts>

The diagram for the action namespace of Struts 2 maps to the directory is as follows:

Second, JSP page

I put three JSP files of the same file name, but they are in different modules.

1) Root-webapp/pages/welcome.jsp
2) Common module--webapp/common/pages/welcome.jsp
3) User Module--webapp/user/pages/welcome.jsp

Iii. mechanism of work

To run the project, we can access different namespaces by accessing different URLs separately.

Example 1
Url:http://localhost:8080/struts2example/saywelcome.action
This will match the root namespace.

<package name="default" namespace="/" extends="struts-default">    <action name="SayWelcome">        <result>pages/welcome.jsp</result>    </action></package>


The contents of the webapp/pages/welcome.jsp are displayed.

Example 2
Url:http://localhost:8080/struts2example/common/saywelcome.action
This will match the common namespace.

<package name="common" namespace="/common" extends="struts-default">    <action name="SayWelcome">        <result>pages/welcome.jsp</result>    </action></package>


The contents of the webapp/common/pages/welcome.jsp are displayed.

Example 3
Url:http://localhost:8080/struts2example/user/saywelcome.action
This will match the user namespace.

<package name="user" namespace="/user" extends="struts-default">    <action name="SayWelcome">        <result>pages/welcome.jsp</result>    </action></package>


The contents of the webapp/user/pages/welcome.jsp are displayed.

Copyright NOTICE: This article for Bo Master Chszs original article, without Bo Master permission not reproduced.

Apache Struts 2 's namespace combat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.