...
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 |
---|
|
<%@ 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 |
---|
title | Input.java (prepare)java |
---|
|
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;
this.description = description;
}
String key;
public String getKey() {
return key;
}
String description;
public String getDescription() {
return description;
}
}
}
|
...
In effect, exectuteResult
actions can be used like a tag that can run its own action before emitting the markup.
Code Block |
---|
|
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:form>
<s:action name="Languages" executeResult="true"/>
<s:submit/>
</s::form>
|
Code Block |
---|
| xml |
---|
| xml |
---|
title | Languages.jspxml |
---|
|
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:select
tooltip="Choose Your Favorite Language"
label="Favorite Language"
list="languages"
name=language"
listKey="key"
listValue="description"
emptyOption="true"
headerKey="None"
headerValue="None"/>
|
Code Block |
---|
| java |
---|
| java |
---|
title | Languages.javajava |
---|
|
public class Languages extends ActionSupport {
public String execute() {
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;
}
public static class Language {
String description;
String key;
public Language(String key, String description) {
this.key = key;
this.description = description;
}
public String getKey() {
return key;
}
public String getDescription() {
return description;
}
}
}
|
Code Block |
---|
| xml |
---|
| xml |
---|
title | struts.xml (Input, Languages)xml |
---|
|
<action name="Input">
<result>/app/Input.jsp</result>
</action>
<action name="Languages" class="app.Languages">
<result>Languages.jsp</result>
</action>
|
...