Return-Path: X-Original-To: apmail-struts-commits-archive@minotaur.apache.org Delivered-To: apmail-struts-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 14FCD18933 for ; Sat, 27 Feb 2016 10:00:09 +0000 (UTC) Received: (qmail 76438 invoked by uid 500); 27 Feb 2016 10:00:08 -0000 Delivered-To: apmail-struts-commits-archive@struts.apache.org Received: (qmail 76393 invoked by uid 500); 27 Feb 2016 10:00:08 -0000 Mailing-List: contact commits-help@struts.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@struts.apache.org Delivered-To: mailing list commits@struts.apache.org Received: (qmail 76383 invoked by uid 99); 27 Feb 2016 10:00:08 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 27 Feb 2016 10:00:08 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 3850BDFE14; Sat, 27 Feb 2016 10:00:08 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: lukaszlenart@apache.org To: commits@struts.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: struts-examples git commit: Adds simple example how to define custom type conversion Date: Sat, 27 Feb 2016 10:00:08 +0000 (UTC) Repository: struts-examples Updated Branches: refs/heads/master 2697ee741 -> de96f2c16 Adds simple example how to define custom type conversion Project: http://git-wip-us.apache.org/repos/asf/struts-examples/repo Commit: http://git-wip-us.apache.org/repos/asf/struts-examples/commit/de96f2c1 Tree: http://git-wip-us.apache.org/repos/asf/struts-examples/tree/de96f2c1 Diff: http://git-wip-us.apache.org/repos/asf/struts-examples/diff/de96f2c1 Branch: refs/heads/master Commit: de96f2c169a4035da30132b65f103f6668b487cc Parents: 2697ee7 Author: Lukasz Lenart Authored: Sat Feb 27 10:59:46 2016 +0100 Committer: Lukasz Lenart Committed: Sat Feb 27 10:59:46 2016 +0100 ---------------------------------------------------------------------- pom.xml | 1 + type-conversion/pom.xml | 62 ++++++++++++++++++++ .../java/org/apache/struts/example/Theme.java | 56 ++++++++++++++++++ .../example/ThemeDescriptorConverter.java | 42 +++++++++++++ .../apache/struts/model/ThemeDescriptor.java | 35 +++++++++++ .../java/org/apache/struts/model/Themes.java | 28 +++++++++ .../main/resources/Theme-conversion.properties | 1 + type-conversion/src/main/resources/example.xml | 19 ++++++ type-conversion/src/main/resources/log4j2.xml | 16 +++++ type-conversion/src/main/resources/struts.xml | 25 ++++++++ .../main/resources/xwork-conversion.properties | 1 + .../src/main/webapp/WEB-INF/example/Theme.jsp | 22 +++++++ type-conversion/src/main/webapp/WEB-INF/web.xml | 23 ++++++++ type-conversion/src/main/webapp/index.html | 10 ++++ 14 files changed, 341 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/struts-examples/blob/de96f2c1/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index 75ada42..61eb7a5 100644 --- a/pom.xml +++ b/pom.xml @@ -75,6 +75,7 @@ unit-testing wildcard-method-selection message-store + type-conversion http://git-wip-us.apache.org/repos/asf/struts-examples/blob/de96f2c1/type-conversion/pom.xml ---------------------------------------------------------------------- diff --git a/type-conversion/pom.xml b/type-conversion/pom.xml new file mode 100644 index 0000000..45f9c93 --- /dev/null +++ b/type-conversion/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + + + struts-examples + org.apache.struts + 1.0.0 + + + type-conversion + war + Custom Type Convertion + + + UTF-8 + + + + + + org.apache.struts + struts2-core + ${struts2.version} + + + + javax.servlet + servlet-api + 2.4 + provided + + + + javax.servlet + jsp-api + 2.0 + provided + + + + + + + + org.mortbay.jetty + jetty-maven-plugin + 8.1.16.v20140903 + + CTRL+C + 8999 + 10 + ${basedir}/src/main/webapp/ + + ${basedir}/src/main/webapp/WEB-INF/web.xml + + + + + + http://git-wip-us.apache.org/repos/asf/struts-examples/blob/de96f2c1/type-conversion/src/main/java/org/apache/struts/example/Theme.java ---------------------------------------------------------------------- diff --git a/type-conversion/src/main/java/org/apache/struts/example/Theme.java b/type-conversion/src/main/java/org/apache/struts/example/Theme.java new file mode 100644 index 0000000..198830f --- /dev/null +++ b/type-conversion/src/main/java/org/apache/struts/example/Theme.java @@ -0,0 +1,56 @@ +/* + * $Id$ + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.struts.example; + +import com.opensymphony.xwork2.ActionSupport; +import com.opensymphony.xwork2.util.logging.Logger; +import com.opensymphony.xwork2.util.logging.LoggerFactory; +import org.apache.struts.model.ThemeDescriptor; +import org.apache.struts.model.Themes; + +import java.util.Map; + +public class Theme extends ActionSupport { + + private static final Logger LOG = LoggerFactory.getLogger(Theme.class); + + private Map themes = Themes.list(); + private ThemeDescriptor selectedTheme = Themes.get("simple"); + + public String execute() throws Exception { + return SUCCESS; + } + + public Map getThemes() { + return themes; + } + + public ThemeDescriptor getSelectedTheme() { + LOG.info("Existing theme: #0", String.valueOf(selectedTheme)); + return selectedTheme; + } + + public void setSelectedTheme(ThemeDescriptor selectedTheme) { + LOG.info("Selected theme: #0", String.valueOf(selectedTheme)); + this.selectedTheme = selectedTheme; + } +} http://git-wip-us.apache.org/repos/asf/struts-examples/blob/de96f2c1/type-conversion/src/main/java/org/apache/struts/example/ThemeDescriptorConverter.java ---------------------------------------------------------------------- diff --git a/type-conversion/src/main/java/org/apache/struts/example/ThemeDescriptorConverter.java b/type-conversion/src/main/java/org/apache/struts/example/ThemeDescriptorConverter.java new file mode 100644 index 0000000..78fcbb4 --- /dev/null +++ b/type-conversion/src/main/java/org/apache/struts/example/ThemeDescriptorConverter.java @@ -0,0 +1,42 @@ +package org.apache.struts.example; + +import com.opensymphony.xwork2.conversion.TypeConversionException; +import com.opensymphony.xwork2.util.logging.Logger; +import com.opensymphony.xwork2.util.logging.LoggerFactory; +import org.apache.struts.model.ThemeDescriptor; +import org.apache.struts.model.Themes; +import org.apache.struts2.util.StrutsTypeConverter; + +import java.util.Map; + +public class ThemeDescriptorConverter extends StrutsTypeConverter { + + private static final Logger LOG = LoggerFactory.getLogger(ThemeDescriptorConverter.class); + + @SuppressWarnings("rawtypes") + @Override + public Object convertFromString(final Map context, final String[] values, final Class toClass) { + if ((values != null) && (values.length > 0)) { + try { + if (Themes.contains(values[0])) { + return Themes.get(values[0]); + } + throw new RuntimeException("No theme with id: " + values[0]); + } catch (Exception e) { + throw new TypeConversionException("Unable to convert into a ThemeDescriptor: " + values[0], e); + } + } + return null; + } + + @SuppressWarnings("rawtypes") + @Override + public String convertToString(final Map context, final Object value) { + try { + return ((ThemeDescriptor) value).getId(); + } catch (Exception e) { + LOG.error("Unable to convert {0} into a string", e, value); + } + return null; + } +} http://git-wip-us.apache.org/repos/asf/struts-examples/blob/de96f2c1/type-conversion/src/main/java/org/apache/struts/model/ThemeDescriptor.java ---------------------------------------------------------------------- diff --git a/type-conversion/src/main/java/org/apache/struts/model/ThemeDescriptor.java b/type-conversion/src/main/java/org/apache/struts/model/ThemeDescriptor.java new file mode 100644 index 0000000..1e93bad --- /dev/null +++ b/type-conversion/src/main/java/org/apache/struts/model/ThemeDescriptor.java @@ -0,0 +1,35 @@ +package org.apache.struts.model; + +public class ThemeDescriptor { + + private final String id; + private final String displayName; + private final String contextPath; + + public ThemeDescriptor(final String id, final String displayName, final String contextPath) { + this.id = id; + this.displayName = displayName; + this.contextPath = contextPath; + } + + public String getId() { + return id; + } + + public String getDisplayName() { + return displayName; + } + + public String getContextPath() { + return contextPath; + } + + @Override + public String toString() { + return "ThemeDescriptor{" + + "id='" + id + '\'' + + ", displayName='" + displayName + '\'' + + ", contextPath='" + contextPath + '\'' + + '}'; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/struts-examples/blob/de96f2c1/type-conversion/src/main/java/org/apache/struts/model/Themes.java ---------------------------------------------------------------------- diff --git a/type-conversion/src/main/java/org/apache/struts/model/Themes.java b/type-conversion/src/main/java/org/apache/struts/model/Themes.java new file mode 100644 index 0000000..84cbefa --- /dev/null +++ b/type-conversion/src/main/java/org/apache/struts/model/Themes.java @@ -0,0 +1,28 @@ +package org.apache.struts.model; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +public class Themes { + + private static Map themes; + + static { + themes = new HashMap(); + themes.put("simple", new ThemeDescriptor("simple", "Simple Theme", "themes/simple")); + themes.put("extended", new ThemeDescriptor("extended", "Extended Theme", "themes/extended")); + } + + public static ThemeDescriptor get(String id) { + return themes.get(id); + } + + public static boolean contains(String id) { + return themes.containsKey(id); + } + + public static Map list() { + return Collections.unmodifiableMap(themes); + } +} http://git-wip-us.apache.org/repos/asf/struts-examples/blob/de96f2c1/type-conversion/src/main/resources/Theme-conversion.properties ---------------------------------------------------------------------- diff --git a/type-conversion/src/main/resources/Theme-conversion.properties b/type-conversion/src/main/resources/Theme-conversion.properties new file mode 100644 index 0000000..38f01d1 --- /dev/null +++ b/type-conversion/src/main/resources/Theme-conversion.properties @@ -0,0 +1 @@ +Element_themes=org.apache.struts.model.ThemeDescriptor http://git-wip-us.apache.org/repos/asf/struts-examples/blob/de96f2c1/type-conversion/src/main/resources/example.xml ---------------------------------------------------------------------- diff --git a/type-conversion/src/main/resources/example.xml b/type-conversion/src/main/resources/example.xml new file mode 100644 index 0000000..f255867 --- /dev/null +++ b/type-conversion/src/main/resources/example.xml @@ -0,0 +1,19 @@ + + + + + + + + + + /WEB-INF/example/Theme.jsp + + + + http://git-wip-us.apache.org/repos/asf/struts-examples/blob/de96f2c1/type-conversion/src/main/resources/log4j2.xml ---------------------------------------------------------------------- diff --git a/type-conversion/src/main/resources/log4j2.xml b/type-conversion/src/main/resources/log4j2.xml new file mode 100644 index 0000000..9160e24 --- /dev/null +++ b/type-conversion/src/main/resources/log4j2.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file http://git-wip-us.apache.org/repos/asf/struts-examples/blob/de96f2c1/type-conversion/src/main/resources/struts.xml ---------------------------------------------------------------------- diff --git a/type-conversion/src/main/resources/struts.xml b/type-conversion/src/main/resources/struts.xml new file mode 100644 index 0000000..c2aff5b --- /dev/null +++ b/type-conversion/src/main/resources/struts.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + Theme + /example + + + + + + + http://git-wip-us.apache.org/repos/asf/struts-examples/blob/de96f2c1/type-conversion/src/main/resources/xwork-conversion.properties ---------------------------------------------------------------------- diff --git a/type-conversion/src/main/resources/xwork-conversion.properties b/type-conversion/src/main/resources/xwork-conversion.properties new file mode 100644 index 0000000..7c7f843 --- /dev/null +++ b/type-conversion/src/main/resources/xwork-conversion.properties @@ -0,0 +1 @@ +org.apache.struts.model.ThemeDescriptor=org.apache.struts.example.ThemeDescriptorConverter http://git-wip-us.apache.org/repos/asf/struts-examples/blob/de96f2c1/type-conversion/src/main/webapp/WEB-INF/example/Theme.jsp ---------------------------------------------------------------------- diff --git a/type-conversion/src/main/webapp/WEB-INF/example/Theme.jsp b/type-conversion/src/main/webapp/WEB-INF/example/Theme.jsp new file mode 100644 index 0000000..b6b59ef --- /dev/null +++ b/type-conversion/src/main/webapp/WEB-INF/example/Theme.jsp @@ -0,0 +1,22 @@ +<%@ page contentType="text/html; charset=UTF-8" %> +<%@ taglib prefix="s" uri="/struts-tags" %> + + + Theme selector + + + +

+ + + + + + + + +
Choose
+
+ + + http://git-wip-us.apache.org/repos/asf/struts-examples/blob/de96f2c1/type-conversion/src/main/webapp/WEB-INF/web.xml ---------------------------------------------------------------------- diff --git a/type-conversion/src/main/webapp/WEB-INF/web.xml b/type-conversion/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..8858656 --- /dev/null +++ b/type-conversion/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,23 @@ + + + Struts Blank + + + struts2 + + org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter + + + + + struts2 + /* + + + + index.html + + http://git-wip-us.apache.org/repos/asf/struts-examples/blob/de96f2c1/type-conversion/src/main/webapp/index.html ---------------------------------------------------------------------- diff --git a/type-conversion/src/main/webapp/index.html b/type-conversion/src/main/webapp/index.html new file mode 100644 index 0000000..b749921 --- /dev/null +++ b/type-conversion/src/main/webapp/index.html @@ -0,0 +1,10 @@ + + + + + + + +

Loading ...

+ +