使用Struts 2.0调用tiles.xml中的ActionClass

发布于 2024-12-01 01:51:15 字数 2402 浏览 1 评论 0原文

我回顾了struts2.0的tiles示例,发现在tiles.xml中jsp页面的调用方式如下:

<definition name="welcome" extends="baseLayout">
  <put-attribute name="title"  value="Welcome"/>
  <put-attribute name="body"   value="/welcome.jsp"/>      

如何调用它,

<definition name="friends" extends="baseLayout">
  <put-attribute name="title"  value="Friends"/>
  <put-attribute name="body"   value="/checkActionLink.action"/>      

但我的问题是,如果我想调用操作类而不是 .jsp 页面,而不是像当我尝试编写执行上述代码时 而不是显示未找到 checkActionLink.action 的错误......预先感谢您的帮助......

以下是 web.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>Struts2Example15</display-name>
    <servlet>
        <servlet-name>tiles</servlet-name>
        <servlet-class>org.apache.tiles.web.startup.TilesServlet</servlet-class>
        <init-param>
            <param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>
            <param-value>/WEB-INF/tiles.xml</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <listener>
        <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
    </listener>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
    </filter-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

I reviewed the example of tiles with struts2.0 and found that in tiles.xml jsp pages are called like:

<definition name="welcome" extends="baseLayout">
  <put-attribute name="title"  value="Welcome"/>
  <put-attribute name="body"   value="/welcome.jsp"/>      

BUT my question is if I want to call the action class instead of .jsp pages than how to call it like

<definition name="friends" extends="baseLayout">
  <put-attribute name="title"  value="Friends"/>
  <put-attribute name="body"   value="/checkActionLink.action"/>      

when I am trying to write to execute the above code than its showing the error that checkActionLink.action is not found....thanks in advance for the help.....

Following is the web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>Struts2Example15</display-name>
    <servlet>
        <servlet-name>tiles</servlet-name>
        <servlet-class>org.apache.tiles.web.startup.TilesServlet</servlet-class>
        <init-param>
            <param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>
            <param-value>/WEB-INF/tiles.xml</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <listener>
        <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
    </listener>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
    </filter-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫瑾少女 2024-12-08 01:51:15

经过大量研究,我发现添加以下标签:
web.xml 中的 FORWARD 问题得到解决。

现在问题来了,为什么呢?
根据我的理解,当我们添加任何操作作为 标记的 value 属性的值时,请求将转发到提到的操作,因此操作执行成功。

以前缺少 FORWARD 标签,因此导致了此问题。

如果我的理解有任何修正,我将不胜感激。

谢谢。快乐编码:)。

After so much research I found out that adding the following tag:
<dispatcher>FORWARD</dispatcher> in the web.xml the issue gets resolved.

Now the question arises why?
As per my understanding when we add any action as the value of value attribute of the <put-attribute/> tag the request is forwarded to the mentioned action so the action is executed successfully.

Previously <dispatcher>FORWARD</dispatcher> tag was missing so this issue was caused.

I would really appreciate if any correction is there in my understanding.

Thanks. Happy Coding :).

孤云独去闲 2024-12-08 01:51:15

我认为你不能。您需要创建一个 jsp 并在其中使用 struts2 操作标记。它可以调用一个操作并渲染其页面的一部分。如果你只使用action标签来制作jsp,你可能会得到你想要的效果。从未尝试过此操作,但您可能可以在调用 jsp 之前将tiles中的操作名称和命名空间插入到操作标记中。

我很想知道这对您来说效果如何。

I don't think you can. You'll need to create a jsp and use the struts2 action tag in it. That can call an action and render part of it's page. If you make a jsp only using the action tag, you'll probably get the effect you want. Have never tried this, but you can probably insert the name of the action and namespace from tiles into the action tag before the jsp is invoked.

I would be very interested to hear how this works out for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文