2

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?

Roman C
  • 49,761
  • 33
  • 66
  • 176
user3769040
  • 235
  • 4
  • 14
  • Nothing is wrong. You should access pages through actions, not directly. – Aleksandr M Aug 19 '14 at 08:16
  • You're not "accessing JSP pages", you're accessing actions, that happen to use JSPs as their results. It's the same way you can access a servlet that forwards to a JSP, or streams a file back from the file system--from the client's perspective it's just a URL. – Dave Newton Aug 19 '14 at 12:03

1 Answers1

1

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 example of usage namespaces and explanation you can find in the example Struts 2 Namespace configuration example and explanation.

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • 1
    The only thing that helped me to figure out my problem was by me going through the link provided by Roman C.: http://www.mkyong.com/struts2/struts-2-namespace-configuration-example-and-explanation/ Extremely helpful and now I understand it. Thank you. – user3769040 Aug 19 '14 at 22:51