Abhijit
Abhijit

Reputation: 683

Get Request in Struts 2

I am new to struts2. In application there is Action called userLogin. when i enter to url http://servername:9090/appName/userLogin, It should forward the request directly to to /jsp/account/login.jsp. it should not call action method. how can i ensure that when request is through get then forword to loginPage else if Request is Post then it should call the action

Struts.xml is as follows

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.devMode" value="true" />
    <constant name="struts.custom.i18n.resources" value="ApplicationResources" />
    <package name="default" extends="struts-default">
        <action name="index">
            <result>jsp/index.jsp</result>
        </action>    

        <action name="userLogin" class="com.ril.tc.action.LoginAction">
            <result>jsp/account/login.jsp</result>
            <result name="success">jsp/index.jsp</result>
            <result name="error">jsp/account/login.jsp</result>
        </action>

        <action name="ministatement" class="com.ril.tc.action.MiniStatementAction">
            <result>jsp/account/ministatement.jsp</result>
            <result name="success">jsp/account/ministatementdetails.jsp</result>
            <result name="error">jsp/account/ministatement.jsp</result>
        </action>
    </package>
</struts>

Upvotes: 0

Views: 3948

Answers (1)

zumit
zumit

Reputation: 189

1. You can simply check the request method in action method before the business method executes.

      It will add  2-3 lines of java code in action method and  a line modification in xml configuration.

2. Or you can strictly restrict the execution of the Action method by using an interceptor.

    Interceptor requires a seperate java class and a few lines of xml configuration.

Method 1

a) Java code

public String execute (){  // or your method name

     if(ServletActionContext.getRequest().getMethod().equals("GET")){

        return  "getrequest";
     }

........
//your business logic

}

b) xml configuration

<action name="userLogin" class="com.ril.tc.action.LoginAction">
            <result name="getrequest">jsp/account/login.jsp</result>
            <result name="success">jsp/index.jsp</result>
            <result name="error">jsp/account/login.jsp</result>
        </action>

Method 2.

a) Java Code

public GetRequestFilterInterceptor extends AbstractInterceptor{
  @Override
    public void intercept (ActionInvocation action){
            if(ServletActionContext.getRequest().getMethod(). equals("GET")){
                    return "getrequest";
            }else{
                    action.invoke();
            }

    }
}

b) Xml configuration

<struts>
  <package name="default" extends="struts-default">
        <interceptors>

        <interceptor name="getUrlFilter" class="packagename.GetRequestFilterInterceptor"/>

                    <interceptor-stack  name="requestFilter">

                        <interceptor-ref  name="getUrlFilter"/>
                        <interceptor-ref name="defaultStack"/>  

                     </interceptor-stack>

        </interceptors>
     ......

        <action name="userLogin" class="com.ril.tc.action.LoginAction">

            <interceptor-ref  name="requestFilter"/>

            <result name="getrequest">jsp/account/login.jsp</result>
            <result name="success">jsp/index.jsp</result>
            <result name="error">jsp/account/login.jsp</result>
        </action>    

     </package>

</struts>

I recommend you to use interceptors, if you want to filter a lot a methods. If you have want to filter only one method, the first one is better.

Upvotes: 1

Related Questions