English 中文(简体)
struts2 action not calling properly
原标题:

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>

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

最佳回答

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

  • 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) 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) Just one mapping

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

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.

Example: In 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());
    }

}

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
    }
}
问题回答

When you call an actions from jsp, the default method is execute(). If you want to call another method, you can spec by the attribute method="".

<s:url action="SessionManager" method="input"/>

The attribute method is common on other tags.

If you are using a <s:a... tag, then you will not be able to call the intended method apart from the default execute() method. Instead of <s:a... tag, you need to define the action using an <s:url... tag with an id and call the same using normal html anchor tag ie., <a href=${<<id>>} but you may need to specify the method in your struts.xml or any other xml configuration file, which you have included in your struts.xml file with the method attribute in your <action... method="..." clause.





相关问题
Getting sitemesh to work with struts2

I m trying to integrate sitemesh into my struts2 app, but i m finding that it s not making any difference and isn t showing any errors (or anything sitemesh related at all) in the logs. I ve started ...

Struts Action Validation

Hey People. I have been recently validating Struts 2 actions. In One of my action class, the save method to be precise must have two id s. That is an assessmenttype id and a course id. this is what is ...

struts2 action not calling properly

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

How to validate a select tag in struts 2 framework

I am a newbie into programming and i am currently employed as a junior programmer. I am currently having some problems validating the select tags in one of my forms. What i actually trying to do is to ...

Setting the default value in Struts2

I am setting the value(kind of default value) for a drop down select value from action class in a page(given below). When the page loads the value is beig displayed but the other elements of the ...

Localized exceptions (within a Struts2 app)

I am developing a Struts 2 application with support for multiple languages. If one of the domain objects needs to throw an exception, how can it do so in such a way that the error message is no ...

热门标签