Struts2 - 在 struts.xml 中有两个包指向动态 web 根 [英] Struts2 - have two packages in struts.xml point to dynamic web root

查看:28
本文介绍了Struts2 - 在 struts.xml 中有两个包指向动态 web 根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,请原谅我缺乏理解...我还在学习:)

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

我的 struts.xml 中有 2 个扩展基本包的包,我希望能够通过在浏览器中输入类似于 http://site.com/application/1/ThisAction.action 和/application/2/ThisAction.action(示例).

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).

我在 webapp 文件夹中创建了两个目录,分别命名为1"和2",我可以使用上面的 url 导航到这两个包.我想要做的实际上是将我的jsps放在jsp目录中,而不是webapps.所以不是我的两个文件夹驻留在/webapp 中,而是它们应该驻留在/webapp/jsp/中.

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/.

我尝试将两个包的命名空间更改为/1/jsp/之类的名称,而不是简单的/1",但我什么也没得到.它只是一直告诉我没有映射到该操作名称的操作.

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.

有没有人对我如何做到这一点有任何见解?Google 没有给我很多帮助,但可能只是我没有在寻找正确的东西.

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"> 

推荐答案

在 Struts 2 中,您不需要根据用于访问它们的 URL 将 JSP 放在各种文件夹中.相反,包和操作一起创建 URL,结果将决定下一个视图.所以你从写你的动作开始:

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;
 } 
}

接下来,您将在 struts.xml 中创建一个指向此操作的条目.

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>

现在,要在默认包中访问此操作,您只需使用以下 URL:http://yourserver/myaction.action.

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>

然后您可以使用以下 URL 访问该操作:http://yourserver/2/myaction.action.

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

因此,如果您愿意,您可以继续将 JSP 放在名为 jsp 的目录中,您只需修改结果以指向正确的位置.

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.

这篇关于Struts2 - 在 struts.xml 中有两个包指向动态 web 根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆