This is an automated email from the ASF dual-hosted git repository. lukaszlenart pushed a commit to branch fix/conversion-example in repository https://gitbox.apache.org/repos/asf/struts-examples.git
commit fcf6d06c9e6de59c7cd0dd941f6b86dc24904d48 Author: Lukasz Lenart <lukaszlen...@apache.org> AuthorDate: Tue Mar 4 16:37:11 2025 +0100 Updates the example to be compatible with Struts 7 It also extends the example to show how to use different approach to error messages --- .../main/java/org/apache/struts/example/NumberAction.java | 4 ++++ .../src/main/java/org/apache/struts/example/ThemeAction.java | 12 ++++++++---- type-conversion/src/main/resources/example.xml | 10 ++++------ type-conversion/src/main/resources/log4j2.xml | 2 +- .../org/apache/struts/example/NumberAction.properties | 1 + ...rk-conversion.properties => struts-conversion.properties} | 0 type-conversion/src/main/resources/struts.xml | 8 ++++++-- type-conversion/src/main/webapp/WEB-INF/example/Number.jsp | 3 +++ type-conversion/src/main/webapp/WEB-INF/example/Theme.jsp | 4 +++- 9 files changed, 30 insertions(+), 14 deletions(-) diff --git a/type-conversion/src/main/java/org/apache/struts/example/NumberAction.java b/type-conversion/src/main/java/org/apache/struts/example/NumberAction.java index 32a78b4..16653cb 100644 --- a/type-conversion/src/main/java/org/apache/struts/example/NumberAction.java +++ b/type-conversion/src/main/java/org/apache/struts/example/NumberAction.java @@ -3,6 +3,7 @@ package org.apache.struts.example; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.struts2.ActionSupport; +import org.apache.struts2.interceptor.parameter.StrutsParameter; import java.math.BigDecimal; @@ -27,6 +28,7 @@ public class NumberAction extends ActionSupport { return bigDecimal; } + @StrutsParameter public void setBigDecimal(BigDecimal bigDecimal) { this.bigDecimal = bigDecimal; } @@ -35,6 +37,7 @@ public class NumberAction extends ActionSupport { return bigDouble; } + @StrutsParameter public void setBigDouble(Double bigDouble) { this.bigDouble = bigDouble; } @@ -43,6 +46,7 @@ public class NumberAction extends ActionSupport { return primitiveDouble; } + @StrutsParameter public void setPrimitiveDouble(double primitiveDouble) { this.primitiveDouble = primitiveDouble; } diff --git a/type-conversion/src/main/java/org/apache/struts/example/ThemeAction.java b/type-conversion/src/main/java/org/apache/struts/example/ThemeAction.java index 95fb639..64606dc 100644 --- a/type-conversion/src/main/java/org/apache/struts/example/ThemeAction.java +++ b/type-conversion/src/main/java/org/apache/struts/example/ThemeAction.java @@ -24,6 +24,8 @@ import org.apache.logging.log4j.Logger; import org.apache.struts.model.ThemeDescriptor; import org.apache.struts.model.Themes; import org.apache.struts2.ActionSupport; +import org.apache.struts2.interceptor.parameter.StrutsParameter; +import org.apache.struts2.validator.annotations.ConversionErrorFieldValidator; import java.util.Map; @@ -32,10 +34,10 @@ public class ThemeAction extends ActionSupport { private static final Logger LOG = LogManager.getLogger(ThemeAction.class); private Map<String, ThemeDescriptor> themes = Themes.list(); - private ThemeDescriptor selectedTheme = Themes.get("simple"); + private ThemeDescriptor selectedTheme;// = Themes.get("simple"); public String execute() throws Exception { - return SUCCESS; + return INPUT; } public Map<String, ThemeDescriptor> getThemes() { @@ -43,12 +45,14 @@ public class ThemeAction extends ActionSupport { } public ThemeDescriptor getSelectedTheme() { - LOG.info("Existing theme: #0", String.valueOf(selectedTheme)); + LOG.info("Existing theme: {}", String.valueOf(selectedTheme)); return selectedTheme; } + @StrutsParameter + @ConversionErrorFieldValidator(fieldName = "selectedTheme", key = "error.theme.not.valid", message = "Invalid theme") public void setSelectedTheme(ThemeDescriptor selectedTheme) { - LOG.info("Selected theme: #0", String.valueOf(selectedTheme)); + LOG.info("Selected theme: {}", String.valueOf(selectedTheme)); this.selectedTheme = selectedTheme; } } diff --git a/type-conversion/src/main/resources/example.xml b/type-conversion/src/main/resources/example.xml index 5535fe6..a83dbdc 100644 --- a/type-conversion/src/main/resources/example.xml +++ b/type-conversion/src/main/resources/example.xml @@ -1,23 +1,21 @@ <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC - "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" - "https://struts.apache.org/dtds/struts-2.5.dtd"> + "-//Apache Software Foundation//DTD Struts Configuration 6.0//EN" + "https://struts.apache.org/dtds/struts-6.0.dtd"> <!-- - This file is included by the struts.xml file as an example - of how to break up the configuration file into multiple files. --> <struts> <package name="example" namespace="/example" extends="struts-default"> - <default-action-ref name="Theme" /> <action name="Theme" class="org.apache.struts.example.ThemeAction"> - <result>/WEB-INF/example/Theme.jsp</result> + <result name="input">/WEB-INF/example/Theme.jsp</result> </action> - <action name="Number" class="org.apache.struts.example.IndexAction"> + <action name="Number" class="org.apache.struts.example.NumberAction"> <result name="input">/WEB-INF/example/Number.jsp</result> </action> - </package> </struts> diff --git a/type-conversion/src/main/resources/log4j2.xml b/type-conversion/src/main/resources/log4j2.xml index 06f1766..4cf31c0 100644 --- a/type-conversion/src/main/resources/log4j2.xml +++ b/type-conversion/src/main/resources/log4j2.xml @@ -2,7 +2,7 @@ <Configuration> <Appenders> <Console name="STDOUT" target="SYSTEM_OUT"> - <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/> + <PatternLayout pattern="[%-5p] %C{2} (%F:%L) - %m%n"/> </Console> </Appenders> <Loggers> diff --git a/type-conversion/src/main/resources/org/apache/struts/example/NumberAction.properties b/type-conversion/src/main/resources/org/apache/struts/example/NumberAction.properties new file mode 100644 index 0000000..0434ee2 --- /dev/null +++ b/type-conversion/src/main/resources/org/apache/struts/example/NumberAction.properties @@ -0,0 +1 @@ +invalid.fieldvalue.bigDecimal=Wrong value for BigDecimal! diff --git a/type-conversion/src/main/resources/xwork-conversion.properties b/type-conversion/src/main/resources/struts-conversion.properties similarity index 100% rename from type-conversion/src/main/resources/xwork-conversion.properties rename to type-conversion/src/main/resources/struts-conversion.properties diff --git a/type-conversion/src/main/resources/struts.xml b/type-conversion/src/main/resources/struts.xml index e1351b4..da89264 100644 --- a/type-conversion/src/main/resources/struts.xml +++ b/type-conversion/src/main/resources/struts.xml @@ -1,12 +1,16 @@ <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC - "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" - "https://struts.apache.org/dtds/struts-2.5.dtd"> + "-//Apache Software Foundation//DTD Struts Configuration 6.0//EN" + "https://struts.apache.org/dtds/struts-6.0.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false"/> <constant name="struts.devMode" value="true"/> <constant name="struts.i18n.reload" value="true"/> + <constant name="struts.ui.theme" value="simple"/> + + <constant name="struts.allowlist.packageNames" value="org.apache.struts.model"/> + <include file="example.xml"/> <package name="default" namespace="/" extends="struts-default"> diff --git a/type-conversion/src/main/webapp/WEB-INF/example/Number.jsp b/type-conversion/src/main/webapp/WEB-INF/example/Number.jsp index bc556c7..066a57c 100644 --- a/type-conversion/src/main/webapp/WEB-INF/example/Number.jsp +++ b/type-conversion/src/main/webapp/WEB-INF/example/Number.jsp @@ -29,14 +29,17 @@ <tr> <td>BigDecimal</td> <td><s:textfield name="bigDecimal"/></td> + <td><s:fielderror fieldName="bigDecimal"/></td> </tr> <tr> <td>Double</td> <td><s:textfield name="bigDouble"/></td> + <td><s:fielderror fieldName="bigDouble"/></td> </tr> <tr> <td>double</td> <td><s:textfield name="primitiveDouble"/></td> + <td><s:fielderror fieldName="primitiveDouble"/></td> </tr> <tr> <td colspan="2"><s:submit/></td> diff --git a/type-conversion/src/main/webapp/WEB-INF/example/Theme.jsp b/type-conversion/src/main/webapp/WEB-INF/example/Theme.jsp index 4fef193..b024c7f 100644 --- a/type-conversion/src/main/webapp/WEB-INF/example/Theme.jsp +++ b/type-conversion/src/main/webapp/WEB-INF/example/Theme.jsp @@ -15,8 +15,10 @@ <tr> <td>Choose</td> <td><s:select name="selectedTheme" emptyOption="true" list="themes" listKey="value" listValue="value.displayName"/></td> - <td><s:submit/></td> + <td><s:fielderror fieldName="selectedTheme"/></td> </tr> + <tr> + <td colspan="3"><s:submit/></td> </table> </s:form>