Daniel Morgan
Daniel Morgan

Reputation: 772

How to set a default page for wrong action requests in struts2?

I have followed apache tutorial but the following code runs into error.

Error>>>>

Caused by: org.xml.sax.SAXParseException; systemId:     
file:/C:/Users/target/Project-1.0/WEB-
INF/classes/struts.xml; lineNumber: 53; columnNumber: 15; The content of element type "package"   
must match "(result-types?,interceptors?,default-interceptor-ref?,default-action-ref?,default-  
class-ref?,global-results?,global-exception-mappings?,action*)".

Please note the actions and packages are correctly defined as once I copy the redirect code it runs into error. Code

     <package name="default" namespace="/" extends="struts-default">

        <default-action-ref name="UnderConstruction"></default-action-ref>

            <action name="UnderConstruction">
                <result>notFound.jsp</result>
            </action>

            <result-types>
                <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>
            </result-types>

            <action ....>
            </action>
              ,,,
    </package>

When I change the follwing line

 <result>notFound.jsp</result> 

to

 <result type="tiles">notFound.jsp</result>  

the application will be ran but when I enter a wrong address it does not show notFound.jsp page, just throw a not found action exception.

When I try the following code the application runs but it does not redirect the wrong requests to the underconstruction page

    <package name="default" namespace="/" extends="struts-default">

            <result-types>
                <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>
            </result-types>

            <action ....>
            </action>
              ,,,
    </package>

      <package name="Hello" extends="struts-default">
           <default-action-ref name="UnderConstruction"></default-action-ref>

            <action name="UnderConstruction">
                <result>notFound.jsp</result>
            </action>
       </package>

Upvotes: 0

Views: 2525

Answers (1)

arvin_codeHunk
arvin_codeHunk

Reputation: 2390

The above configuration you have shown is for tiles-integration. You can not simply copy-paste and make it working like rocket. However There are various ways to achieve your goals, Some of the approch are as follows:

1st approch

create a global result-declaration for error or exception

<global-results>
    <result name="error">/Error.jsp</result>
    <result name="specific_exception">/Unique.jsp</result>
    <result name="login" type="redirectAction">Login.jsp</result>
</global-results>

and validate and return respective string ex error or success from your action based invaidation.

2nd approch

you can redirect itself in your action mapping

<action name="your_action_name">
     <result type="success">Success.jsp</result>
     <result type="error">notFound.jsp</result>
 </action>

Upvotes: 3

Related Questions