开发者

Struts2 - have two packages in struts.xml point to dynamic web root

开发者 https://www.devze.com 2023-01-15 09:00 出处:网络
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 b

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 j开发者_如何学Cust 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"> 


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消