开发者

Calling ActionClass in tiles.xml using Struts 2.0

开发者 https://www.devze.com 2023-03-30 04:10 出处:网络
I reviewed the example of tiles with struts2.0 and found that in tiles.xml jsp pages are called like:

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-a开发者_Go百科pp_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>


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


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.

0

精彩评论

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