struts2操作未正确调用 [英] struts2 action not calling properly

查看:98
本文介绍了struts2操作未正确调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,我希望我的struts2应用转发到一个动作:

On default I want my struts2 app to forward to an action:

<?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.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />

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


        <action name="Login_*" method="{1}" class="myApp.SessionManager">
            <result name="input">/myApp/Login.jsp</result>
            <result type="redirectAction">Menu</result>     
        </action>

    </package>

    <package name="default" namespace="/" extends="struts-default">
        <default-action-ref name="index" />
        <action name="index">
            <result type="redirectAction">
                <param name="actionName">Login_input.action</param>
                <param name="namespace">/myApp</param>
            </result>
        </action>
    </package>


</struts>

我正在寻找要调用SessionManager.input()的应用程序,而是调用SessionManager.execute().

I'm looking for the application to call SessionManager.input(), but instead it calls SessionManager.execute().

推荐答案

您通常不希望使用公共的 input()方法.动作最基本,最典型的场景是:

You don't want typically a public input() method. The most basic and typical scenario for an Action is:

  • 该操作旨在做某事"(一件事情),并且此操作需要一些用户输入.
  • 操作比方法 execute()
  • 中的操作要好
  • 如果该操作成功完成某件事,则将返回成功.这将触发结果丰富的JSP页面.
  • 如果操作没有得到用户输入(因为没有输入,或者因为操作不正确或错误),它将返回 INPUT .这将触发输入表单" JSP,以便用户可以填写数据并(重新)尝试操作.
  • The Action is intended to "do something" (one thing), and this action needs some user input.
  • The Action does than something in the method execute()
  • If the Action did succesfully that something, it returs SUCCESS. This triggers a result informative JSP page.
  • If the Action didn't get the user input (because there wasn't any, or because it was insufficient or wrong) it returns INPUT. This triggers a "input form" JSP so that the user can fill the data and (re)try the action.

现在,可以用几种方式对这种基本情况进行编码,其中包括:

Now, this basic scenario can be coded in several ways, among them:

1)两种不同的映射,一种用于输入表单,另一种用于执行

1) Two different mappings, one for the input form, other for the execution

<!-- default action class: execute does nothing, returns SUCCES -->
<action name="ActionXXXShowForm"> 
                <result>/myApp/XXXForm.jsp</result>
</action>

<action name="ActionXXX" class="myApp.XXXAction"> 
                <result name="input">/myApp/XXXForm.jsp</result>
                <result>/myApp/XXXDone.jsp</result>
</action>

2)仅一个映射

<action name="ActionXXX"  class="myApp.XXXAction"> 
                <result name="input">/myApp/XXXForm.jsp</result>
                <result>/myApp/XXXDone.jsp</result>
</action>

两者非常相似(特别是在进行程序验证时). 在这两种情况下,动作中都只有一个"struts"方法( execute ),这是一个好习惯,因为我们的动作仅做一件事". 但是在第二种情况下,我们需要处理没有数据发布的情况,并且在这种情况下不要发出错误消息.

Both are very similar (specially if your are doing programmatic validation). In both cases, we have only a "struts" method in the action (execute), which is good practice as our action only "does one thing". But in the second case, we need to deal with the case in which no data is post, and dont emit an error message for that case.

示例:在情况1中:

public class XXXAction extends ActionSupport {
    ...    
    public String execute() throws Exception {
        if(! inputOk()) return INPUT;
         // .. do your stuff
        return SUCCESS;
    }

    private boolean inputOk() {
       // ... check your inputs - sort of programatic validation
       if( surname == null ) addActionError("no surname");
       // .... more checks
       return ! hasActionErrors());
    }

}

在第2种情况下,您需要对其进行一些修改:

In case 2 you modify it slightly:

public class XXXAction extends ActionSupport {
    ....    
    public String execute() throws Exception {
        if( emptyInput() ) return INPUT;  // no error messages in this case
        if(! inputOk()) return INPUT;
         // .. do your stuff
        return SUCCESS;
    }

    private boolean inputOk() {
       // same as before
    }

    private boolean emptyInput() {
       // detect no input was sent. do not emit errors herer
    }
}

这篇关于struts2操作未正确调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆