tags:

views:

76

answers:

1

Hello,

First off, please forgive me for my lack of understanding... I'm still learning :)

I have 2 packages in my struts.xml that extend a base package and I want to be able to access them by typing in my browser something similar to http://site.com/application/1/ThisAction.action and /application/2/ThisAction.action (examples).

I created two directories in my webapp folder, named '1' and '2' and I am able to navigate to both packages using the urls above. What I want to do is actually put my jsps within the jsp directory, instead of webapps. so instead of my two folders residing inside /webapp, they should reside in /webapp/jsp/.

I tried changing the namespace of the two packages to something like /1/jsp/ instead of simply '/1', but i get nothing. It just keeps telling me there is no action mapped to that action name.

Does anyone have any insight on how I can accomplish this? Google's not giving me a lot of help, but it may just be that I'm not searching for the right thing.

Here's a quick sample of what I'm referring to:

    <struts>
    <!-- Base-->
    <package name="base" extends="struts-default" abstract = "true" namespace="/base">



<global-results>
   <result name="cancel" type="redirectAction">CancelAction</result>
   <result name="close">closewindow.jsp</result>
   <result name="error">/jsp/wizard/GeneralError.jsp</result>
  </global-results>



    </package>

    <package name="1" extends="base" namespace="/1"> 

+1  A: 

In Struts 2 you do not need to put your JSP's in various folders according to the URL that you use to access them. Rather the package and the action comes together to create the URL and the result will determine the next view. So you start by writing your action:

public class MyActionClass ...{

  ...
  public String actionMethod() {
    //Your action code here
    return SUCCESS;
 } 
}

Next you will create an entry in struts.xml that points to this action.

<package name="default" extends="struts-default">
   <!--Interceptors, Global Results etc.-->
   <action name="myaction" class="my.package.MyActionClass" method="actionMethod">
        <result>/WEB-INF/path/to/yourpage.jsp</result>
    </action>
   ...
</package>

Now, to access this action in the default package you simply use the URL: http://yourserver/myaction.action.

If you create a second package with a different name like this:

    <package  name="2" extends="default" namespace="/2" >

      <action name="myaction" class="my.package.MyActionClass" method="actionMethod">
        <result>/WEB-INF/path/to/yourpage.jsp</result>
       </action>
   ...
   </package>

Then you can access that action with the URL: http://yourserver/2/myaction.action.

So you can go ahead and put your JSP in the directory called jsp if you wish and you only have to modify the result to point to the correct place.

Vincent Ramdhanie
I'm sorry, I must have not explained myself correctly. We want to have two separate copies of these jsps. In other words, we want to have /jsp/1/yourpage.jsp as well as /jsp/2/yourpage.jsp. I think what you're describing involves having one jsp that is used for both packages, correct?
jacosta
No, I am simply saying that you need to set up the packages and actions the way that I suggested in order to construct the URL the way you want. So if they are two separate JSP pages then go ahead and create folders for them, simply let the result for the different actions point to the respective JSP pages in the respective folders.
Vincent Ramdhanie
What you mentioned will definitely work, but what if we have a sizable amount of actions and we don't want to have the same actions in each package? After looking at the struts2 documentation, I was able to somewhat accomplish this by using wildcards in the actions after setting struts.enable.SlashesInActionNames =true, but my results seem to vary, depending on how the actions themselves are invoked (the specific method 'results').I think i may be able to figure this out with a little bit more tweaking..Thanks for your help! Does it sound like i'm on the right track?
jacosta
@jacosta Yes, you are on the right track. Using wildcards is a good diea when you have multiple actions per action class and it does simplify your struts.xml file. Remember that each action can have many results with each pointing to a different view. Each action method will simply return the name of the result that points to the view it intends to display.
Vincent Ramdhanie