Search code examples
javastruts2actionresultxml-configurationstruts2-namespace

Struts2 URL unreachable


I'm really racking my head here with Struts2 - I'm able to access the JSP pages by omitting part of the path. Note the path suppose to include pages/welcome_user.jsp. The key is to look at the word pages in the path.

here's the struts.xml file:

<?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="/User" extends="struts-default">
        <action name="Login">
            <result>pages/login.jsp</result>
        </action>
        <action name="Welcome" class="com.mkyong.user.action.WelcomeUserAction">
            <result name="SUCCESS">pages/welcome_user.jsp</result>
        </action>
    </package>
</struts>

I'm able to access login.jsp via: http://localhost/Struts2Example/User/Login
and welcome_user.jsp via: http://localhost/Struts2Example/User/Welcome
Note that in both URL, I'm able to drop pages, why?

source: http://www.mkyong.com/misc/how-to-use-mkyong-tutorial/

Can someone go through the above tutorial and tell me what's wrong?


Solution

  • First, you have used URLs that are mapped to the actions in the struts.xml.

    The action method is executed and returns a result code SUCCESS. This result you can find in the action config. Then result is executed, if the type of result isn't set the default is dispatcher, and request is forwarded to the location specified in the result config.

    If location is relative the final absolute location will be determined by the namespace of the package used for this action.

    More detailed information of usage namespaces and explanation you can find in the Struts 2 Namespace configuration.

    You can't drop pages if you are using dispatcher result that forwards to JSP. In this case the URL has been rewritten and you can't see the final URL.