1

One of my project is running on Struts 1.x and We are trying to integrate few of URL's pattern on struts 2.x. Few URL also end with Abc.do .

Currently web.xml looks like:

<servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
          <param-name>config</param-name>
          <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
</servlet>

<servlet-mapping>
      <servlet-name>action</servlet-name>
      <url-pattern>*.do</url-pattern>
</servlet-mapping>

And above pattern working fine as defined in web.xml.

Now we have added in web.xml to support Struts 2.x parallel.

<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>*.action</url-pattern>
</filter-mapping>

After added above info in web.xml , those URL pattern ends with .action they are also working fine using struts 2.x.

But when we want to add few specific URL pattern which also ends with .do mapped to Struts 2.x as below.

<filter-mapping>
         <filter-name>struts2</filter-name>
         <url-pattern>/Hello.do</url-pattern>
</filter-mapping>

We want all request which starts with Hello*.do should mapped on struts 2.x , but right now this is not happening.

So is am something missing here in web.xml or Altogether it is not possible.

Is there any way apart we can solve this issue?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Anil Singh
  • 13
  • 1
  • 8
  • First of all, your `Hello` mapping doesn't reflect what you're actually trying to do; I see no wildcard there. Second, the *order* of mappings is significant, you need to have them from most-to-least significant. Third, mapping S2 to anything other than * is fraught with issues if you're not absolutely sure what you're doing. – Dave Newton Sep 15 '14 at 14:24
  • Struts1 + Struts2 together has been tried before. [It didn't work out so well](http://en.wikipedia.org/wiki/The_Island_of_Doctor_Moreau)... – Andrea Ligios Sep 15 '14 at 14:39
  • I want all URL pattern start with Hello should go through struts 2.x. but these patterns end with .do e.g. Hello.do – Anil Singh Sep 15 '14 at 23:53

2 Answers2

0

First you should define Struts2 filter and let it map all URLs including /Hello*.do pattern along with *.action pattern. Then follows Struts 1 filter that maps *.do. The struts.xml should be configured to exclude patterns that ends with .do except /Hello*.do.

struts.xml:

<constant name="struts.action.excludePattern" value="(?!/Hello.*).*\.do"/>
<constant name="struts.action.extension" value="action,do,,"/>

web.xml:

<!-- Struts2 configurations -->
<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>
</filter-mapping>

<!-- Struts configurations -->
<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • URL pattern /Hello*.do not accepting in above web.xml – Anil Singh Sep 15 '14 at 23:36
  • Yes, I changed the pattern. – Roman C Sep 16 '14 at 09:13
  • thanks Roman, now behavior is if request is coming with Hello*.do then it is still going into struts1 xml and if request is without .do like Hello* is working fine.But we need with Hello*.do. Is there any needs to change in this pattern (?!/Hello.*).*\.do ???? – Anil Singh Sep 16 '14 at 12:22
  • No need to change, struts2 filter should handle the request. If servlet is still invoked then you need to override the struts1 request processor to prevent processing already handled by struts2 request. – Roman C Sep 16 '14 at 19:16
  • thanks Roman, yes servlet is still invoked by Hello*.do (it is not going into struts.xml).Can you elaborate how to override the struts1 request processor to prevent processing already handled by struts2 request?????? – Anil Singh Sep 17 '14 at 06:55
  • Add ``. It will handle `Hello*.do` but not other struts2 actions with `.do` because they are excluded. Use `.action` for struts2 actions. – Roman C Sep 17 '14 at 13:05
  • thanks again Roman, as per user given solution Now it is working with Hello*.do .but as you have already told Now we can't do same with another like Welcom*.do so that's why it is not useful for us.because here we have 6 7 url those are with .do and want to redirect to struts2.Is there any another solution for this problem???need your help – Anil Singh Sep 19 '14 at 07:29
  • You can modify exclude pattern to match other URLs like `(?!/(Hello|Welcom).*).*\.do`, but it seems different question. You have asked how to *map* actions. Now you asking how redirect them. I don't understand it because it's different thing. – Roman C Sep 19 '14 at 09:14
  • Glad to hear that. Now you can mark it as accepted solution, if you don't know how to do that see http://stackoverflow.com/tour – Roman C Sep 22 '14 at 15:20
0

Now its working---

In Web.xml--

 <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
 <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        </filter>

In Struts.xml---

<constant name="struts.action.excludePattern" value="(?!/(Hello|AbcOn|Person|School).*).*\.do"/>
             <constant name="struts.action.extension" value="action,do,,"/>
Anil Singh
  • 13
  • 1
  • 8