Java 在Struts 2中获取请求

Java 在Struts 2中获取请求,java,jsp,struts2,Java,Jsp,Struts2,我不熟悉struts2。在应用程序中有一个名为userLogin的操作。当我输入urlhttp://servername:9090/appName/userLogin,它应该将请求直接转发到/jsp/account/login.jsp。它不应该调用action方法。我如何确保当请求通过get then forword登录页面时,如果请求是Post,那么它应该调用操作 Struts.xml如下所示 <?xml version="1.0" encoding="UTF-8"?> <!

我不熟悉struts2。在应用程序中有一个名为userLogin的操作。当我输入url
http://servername:9090/appName/userLogin
,它应该将请求直接转发到/jsp/account/login.jsp。它不应该调用action方法。我如何确保当请求通过get then forword登录页面时,如果请求是Post,那么它应该调用操作

Struts.xml如下所示

<?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>

jsp/index.jsp
jsp/account/login.jsp
jsp/index.jsp
jsp/account/login.jsp
jsp/account/ministate.jsp
jsp/account/ministatementdetails.jsp
jsp/account/ministate.jsp

1。在执行业务方法之前,您只需检查request方法in action方法。

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

2。或者,您可以使用拦截器严格限制操作方法的执行。

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

方法1

a) Java代码

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

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

        return  "getrequest";
     }

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

}
b) xml配置

<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>
b) Xml配置

<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>

......
jsp/account/login.jsp
jsp/index.jsp
jsp/account/login.jsp


如果你想过滤很多方法,我建议你使用拦截器。如果您只想筛选一种方法,那么第一种方法更好。

为什么?只使用两个操作如何。根据
@AleksandrM
,您可以为这两种情况创建两个单独的操作。您也可以调用只返回结果名称的特定方法,但我几乎看不到要点。@ankit337,@Dave Newton你能给我举个例子吗?例如创建
userLogin
,它只会将你重定向到
login.jsp
(一直忽略成功结果)。和第二个行动的东西,无论你想要什么,并称之为任何点击或提交。