struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject struts-examples git commit: Adds simple example how to define custom type conversion
Date Sat, 27 Feb 2016 10:00:08 GMT
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 <lukasz.lenart@gmail.com>
Authored: Sat Feb 27 10:59:46 2016 +0100
Committer: Lukasz Lenart <lukasz.lenart@gmail.com>
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 @@
         <module>unit-testing</module>
         <module>wildcard-method-selection</module>
         <module>message-store</module>
+        <module>type-conversion</module>
   </modules>
 
     <dependencies>

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 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <artifactId>struts-examples</artifactId>
+    <groupId>org.apache.struts</groupId>
+    <version>1.0.0</version>
+  </parent>
+
+  <artifactId>type-conversion</artifactId>
+  <packaging>war</packaging>
+  <name>Custom Type Convertion</name>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <dependencies>
+
+    <dependency>
+      <groupId>org.apache.struts</groupId>
+      <artifactId>struts2-core</artifactId>
+      <version>${struts2.version}</version>
+    </dependency>
+
+    <dependency>
+      <groupId>javax.servlet</groupId>
+      <artifactId>servlet-api</artifactId>
+      <version>2.4</version>
+      <scope>provided</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>javax.servlet</groupId>
+      <artifactId>jsp-api</artifactId>
+      <version>2.0</version>
+      <scope>provided</scope>
+    </dependency>
+
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.mortbay.jetty</groupId>
+        <artifactId>jetty-maven-plugin</artifactId>
+        <version>8.1.16.v20140903</version>
+        <configuration>
+          <stopKey>CTRL+C</stopKey>
+          <stopPort>8999</stopPort>
+          <scanIntervalSeconds>10</scanIntervalSeconds>
+          <webAppSourceDirectory>${basedir}/src/main/webapp/</webAppSourceDirectory>
+          <webAppConfig>
+            <descriptor>${basedir}/src/main/webapp/WEB-INF/web.xml</descriptor>
+          </webAppConfig>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>

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<String, ThemeDescriptor> themes = Themes.list();
+    private ThemeDescriptor selectedTheme = Themes.get("simple");
+
+    public String execute() throws Exception {
+        return SUCCESS;
+    }
+
+    public Map<String, ThemeDescriptor> 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<String, ThemeDescriptor> themes;
+
+    static {
+        themes = new HashMap<String, ThemeDescriptor>();
+        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<String, ThemeDescriptor> 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 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE struts PUBLIC
+        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
+        "http://struts.apache.org/dtds/struts-2.3.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.Theme">
+            <result>/WEB-INF/example/Theme.jsp</result>
+        </action>
+
+    </package>
+</struts>

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 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Configuration>
+    <Appenders>
+        <Console name="STDOUT" target="SYSTEM_OUT">
+            <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
+        </Console>
+    </Appenders>
+    <Loggers>
+        <Logger name="com.opensymphony.xwork2" level="info"/>
+        <Logger name="org.apache.struts2" level="info"/>
+        <Logger name="org.apache.struts" level="debug"/>
+        <Root level="warn">
+            <AppenderRef ref="STDOUT"/>
+        </Root>
+    </Loggers>
+</Configuration>
\ 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 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE struts PUBLIC
+        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
+        "http://struts.apache.org/dtds/struts-2.3.dtd">
+<struts>
+    <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
+    <constant name="struts.devMode" value="true"/>
+
+    <include file="example.xml"/>
+
+    <package name="default" namespace="/" extends="struts-default">
+
+        <default-action-ref name="index"/>
+
+        <action name="index">
+            <result type="redirectAction">
+                <param name="actionName">Theme</param>
+                <param name="namespace">/example</param>
+            </result>
+        </action>
+
+    </package>
+
+    <!-- Add addition packages and configuration here. -->
+</struts>

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" %>
+<html>
+<head>
+  <title>Theme selector</title>
+</head>
+
+<body>
+<h2><s:property value="message"/></h2>
+
+<s:form method="POST" action="design" theme="simple">
+  <table>
+    <tr>
+      <td>Choose</td>
+      <td><s:select name="selectedTheme" emptyOption="true" list="themes" listKey="value"
listValue="value.displayName"/></td>
+      <td><s:submit/></td>
+    </tr>
+  </table>
+</s:form>
+
+</body>
+</html>

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 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<web-app id="struts_blank" version="2.4"
+         xmlns="http://java.sun.com/xml/ns/j2ee" 
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
+  <display-name>Struts Blank</display-name>
+
+  <filter>
+    <filter-name>struts2</filter-name>
+    <filter-class>
+      org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
+    </filter-class>
+  </filter>
+
+  <filter-mapping>
+    <filter-name>struts2</filter-name>
+    <url-pattern>/*</url-pattern>
+  </filter-mapping>
+
+  <welcome-file-list>
+    <welcome-file>index.html</welcome-file>
+  </welcome-file-list>
+</web-app>

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 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
+<html>
+<head>
+    <META HTTP-EQUIV="Refresh" CONTENT="0;URL=example/Theme.action">
+</head>
+
+<body>
+<p>Loading ...</p>
+</body>
+</html>


Mime
View raw message