Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

Instead of populating controls in an Action method, implement the Preparable interface, and use a prepare method instead. The prepare method is called before validation, so it validation fails, we still have a chance to populate controls (or whatever).

Code Block
xml
xml
titleInput.jsp
<s:checkboxlist
 name="selectedOptions"
 list="options"
 listKey="id"
 listValue="name" />
Code Block
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:form>
<s:select
   tooltip="Choose Your Favorite Language"
   label="Favorite Language"
   list="languages"
   name="language"
   listKey="key"
   listValue="description"
   emptyOption="true"
   headerKey="None"
   headerValue="None"/>
<s:submit>
</form>
Code Block
java
java
titleInput.java (prepare)

    public String prepare() {
        languages.add(new Language("EnglishKey", "English Language"));
        languages.add(new Language("FrenchKey", "French Language"));
        languages.add(new Language("SpanishKey", "Spanish Language"));
        return SUCCESS;
    }

    List languages = new ArrayList();
    public List getLanguages() {
        return languages;
    }

    String language;
    public void setLanguage(String value) {
        language = value;
    }
    public String getLanguage() {
        return language;
    }

    public static class Language {

        public Language(String key, String description) {
            this.key = key;
javajava

public void prepare() throws Exception {
      // populate the options property list with options
      // that are supposed to be checked.
this.description = description;
        }

        String key;
        public String getKey() {
            return key;
        }

        String description;
        public String getDescription() {
            return description;
        }
    }
 }

(tick) If a custom stack is being used, be sure to put the Prepare Interceptor before the Validation Interceptor.

...

One way to use this tag is to put a control on a "snippet" JSP that is rendered as a result of an Action that does nothing but create the object that populates the control. The action tag sets "executeresult=true", then control markup will be "included" into the page (like a tile), after the action executes.

In effect, exectuteResult actions can be used like a tag that can run its own action before emitting the markup.

Code Block
xml
xml
1titletitle:Input.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:form>
   <s:action name="languages" namespace="/ActionTagLanguages" executeResult="true"/>
   <s:action name="colors" namespace="/ActionTag" executeResult="true"/>
   <ssubmitsubmit/>
</s::form>
Code Block
xml
xml
1titletitle:Languages.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:select
       tooltip="Choose Your Favorite Language"
       label="Favorite Language"
       list="favoriteLanguageslanguages"
       name="favoriteLanguagelanguage"
       listKey="key"
       listValue="description"
       emptyOption="true"
       headerKey="None"
       headerValue="None"/>
Code Block
java
java
1titletitle:Languages.java (execute)

public class Languages extends ActionSupport {
 
   public String execute() {
        favoriteLanguageslanguages.add(new Select.Language("EnglishKey",
 "English Language"));
        favoriteLanguageslanguages.add(new Select.Language("FrenchKey", "French
 Language"));
        favoriteLanguageslanguages.add(new Select.Language("SpanishKey",
 "Spanish Language"));
        return SUCCESS;
    }

    List languages = new ArrayList();
    public List getLanguages() {
        return languages;
    }
Code Block
xmlxml
1title:struts.xml (Input, Languages)



    public static class Language {
        String description;
        String key;

        public Language(String key, String description) {
            <actionthis.key name="Input">
= key;
            <resultthis.description type="plaintext">Input.jsp</result>
= description;
        }

        public String getKey() {
            return key;
        }

        public String getDescription() {
            return description;
        </action>}

    }


}
Code Block
xml
xml
titlestruts.xml (Input, Languages)

<action name="Input">
    <result>/app/Input.jsp</result>
</action>
<action name="Languages" class="app.Languages">
           <result>Languages.jsp</result>
       </action>

The advantage being that the "Languages" action could be dropped in whereever wherever the "Languages" control is needed, and that the Action for the form doesn't need to know how to populate the Languages control.

...