You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by lu...@apache.org on 2017/03/24 09:17:26 UTC

[2/2] struts-examples git commit: Adds example with custom TextProvider

Adds example with custom TextProvider


Project: http://git-wip-us.apache.org/repos/asf/struts-examples/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts-examples/commit/1b98fdcd
Tree: http://git-wip-us.apache.org/repos/asf/struts-examples/tree/1b98fdcd
Diff: http://git-wip-us.apache.org/repos/asf/struts-examples/diff/1b98fdcd

Branch: refs/heads/master
Commit: 1b98fdcdd8212a9fe9dbcae94dec27af46611062
Parents: 07cf50b
Author: Lukasz Lenart <lu...@gmail.com>
Authored: Fri Mar 24 10:17:09 2017 +0100
Committer: Lukasz Lenart <lu...@gmail.com>
Committed: Fri Mar 24 10:17:09 2017 +0100

----------------------------------------------------------------------
 text-provider/pom.xml                           | 52 +++++++++++++
 .../apache/struts_example/FactoryAction.java    | 31 ++++++++
 .../org/apache/struts_example/IndexAction.java  | 44 +++++++++++
 .../struts_example/MyTextProviderFactory.java   | 19 +++++
 .../struts_example/MyTextTextProvider.java      | 80 ++++++++++++++++++++
 .../org/apache/struts_example/SystemAction.java | 45 +++++++++++
 .../main/resources/DefaultMessages.properties   |  1 +
 .../apache/struts_example/package.properties    |  1 +
 text-provider/src/main/resources/struts.xml     | 27 +++++++
 .../src/main/webapp/WEB-INF/content/success.jsp | 31 ++++++++
 text-provider/src/main/webapp/WEB-INF/web.xml   | 23 ++++++
 11 files changed, 354 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts-examples/blob/1b98fdcd/text-provider/pom.xml
----------------------------------------------------------------------
diff --git a/text-provider/pom.xml b/text-provider/pom.xml
new file mode 100644
index 0000000..ef10c4f
--- /dev/null
+++ b/text-provider/pom.xml
@@ -0,0 +1,52 @@
+<?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>
+    <groupId>org.apache.struts</groupId>
+    <artifactId>struts-examples</artifactId>
+    <version>1.0.0</version>
+  </parent>
+
+  <artifactId>text-provider</artifactId>
+  <packaging>war</packaging>
+  <name>Custom TextProvider</name>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.struts</groupId>
+      <artifactId>struts2-convention-plugin</artifactId>
+      <version>${struts2.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.logging.log4j</groupId>
+      <artifactId>log4j-core</artifactId>
+      <version>2.7</version>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.mortbay.jetty</groupId>
+        <artifactId>jetty-maven-plugin</artifactId>
+        <version>8.1.16.v20140903</version>
+        <configuration>
+          <webApp>
+            <contextPath>/${artifactId}</contextPath>
+          </webApp>
+          <stopKey>CTRL+C</stopKey>
+          <stopPort>8999</stopPort>
+          <scanIntervalSeconds>10</scanIntervalSeconds>
+          <scanTargets>
+            <scanTarget>src/main/webapp/WEB-INF/web.xml</scanTarget>
+          </scanTargets>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>

http://git-wip-us.apache.org/repos/asf/struts-examples/blob/1b98fdcd/text-provider/src/main/java/org/apache/struts_example/FactoryAction.java
----------------------------------------------------------------------
diff --git a/text-provider/src/main/java/org/apache/struts_example/FactoryAction.java b/text-provider/src/main/java/org/apache/struts_example/FactoryAction.java
new file mode 100644
index 0000000..b3b3d16
--- /dev/null
+++ b/text-provider/src/main/java/org/apache/struts_example/FactoryAction.java
@@ -0,0 +1,31 @@
+/*
+ * $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;
+
+/**
+ * This class is using thr factory defined in "struts.xml"
+ */
+public class FactoryAction extends ActionSupport {
+
+}

http://git-wip-us.apache.org/repos/asf/struts-examples/blob/1b98fdcd/text-provider/src/main/java/org/apache/struts_example/IndexAction.java
----------------------------------------------------------------------
diff --git a/text-provider/src/main/java/org/apache/struts_example/IndexAction.java b/text-provider/src/main/java/org/apache/struts_example/IndexAction.java
new file mode 100644
index 0000000..a3e5d3c
--- /dev/null
+++ b/text-provider/src/main/java/org/apache/struts_example/IndexAction.java
@@ -0,0 +1,44 @@
+/*
+ * $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.TextProvider;
+import com.opensymphony.xwork2.TextProviderFactory;
+import com.opensymphony.xwork2.config.Configuration;
+
+public class IndexAction extends ActionSupport {
+
+    @Override
+    public String execute() throws Exception {
+        return SUCCESS;
+    }
+
+    /**
+     * This a bit of hack as factory defined in "struts.xml" will be used by default,
+     * so we must exactly tell which implementation we want to use here
+     */
+    @Override
+    protected TextProvider getTextProvider() {
+        return container.getInstance(TextProviderFactory.class, "struts").createInstance(getClass());
+    }
+}

http://git-wip-us.apache.org/repos/asf/struts-examples/blob/1b98fdcd/text-provider/src/main/java/org/apache/struts_example/MyTextProviderFactory.java
----------------------------------------------------------------------
diff --git a/text-provider/src/main/java/org/apache/struts_example/MyTextProviderFactory.java b/text-provider/src/main/java/org/apache/struts_example/MyTextProviderFactory.java
new file mode 100644
index 0000000..e010ad3
--- /dev/null
+++ b/text-provider/src/main/java/org/apache/struts_example/MyTextProviderFactory.java
@@ -0,0 +1,19 @@
+package org.apache.struts_example;
+
+import com.opensymphony.xwork2.StrutsTextProviderFactory;
+import com.opensymphony.xwork2.TextProvider;
+
+import java.util.ResourceBundle;
+
+public class MyTextProviderFactory extends StrutsTextProviderFactory {
+
+    @Override
+    protected TextProvider getTextProvider(Class clazz) {
+        return new MyTextTextProvider(clazz, localeProviderFactory.createLocaleProvider(), localizedTextProvider);
+    }
+
+    @Override
+    protected TextProvider getTextProvider(ResourceBundle bundle) {
+        return new MyTextTextProvider(bundle, localeProviderFactory.createLocaleProvider(), localizedTextProvider);
+    }
+}

http://git-wip-us.apache.org/repos/asf/struts-examples/blob/1b98fdcd/text-provider/src/main/java/org/apache/struts_example/MyTextTextProvider.java
----------------------------------------------------------------------
diff --git a/text-provider/src/main/java/org/apache/struts_example/MyTextTextProvider.java b/text-provider/src/main/java/org/apache/struts_example/MyTextTextProvider.java
new file mode 100644
index 0000000..41ddfed
--- /dev/null
+++ b/text-provider/src/main/java/org/apache/struts_example/MyTextTextProvider.java
@@ -0,0 +1,80 @@
+package org.apache.struts_example;
+
+import com.opensymphony.xwork2.LocaleProvider;
+import com.opensymphony.xwork2.LocalizedTextProvider;
+import com.opensymphony.xwork2.TextProviderSupport;
+import com.opensymphony.xwork2.util.ValueStack;
+
+import java.util.List;
+import java.util.ResourceBundle;
+
+public class MyTextTextProvider extends TextProviderSupport {
+
+    public MyTextTextProvider(Class clazz, LocaleProvider provider, LocalizedTextProvider localizedTextProvider) {
+        super(clazz, provider, localizedTextProvider);
+    }
+
+    public MyTextTextProvider(ResourceBundle bundle, LocaleProvider provider, LocalizedTextProvider localizedTextProvider) {
+        super(bundle, provider, localizedTextProvider);
+    }
+
+    @Override
+    public String getText(String key) {
+        return localizedTextProvider.findDefaultText(key, localeProvider.getLocale());
+    }
+
+    @Override
+    public String getText(String key, String defaultValue) {
+        return localizedTextProvider.findDefaultText(key, localeProvider.getLocale());
+    }
+
+    @Override
+    public boolean hasKey(String key) {
+        return localizedTextProvider.findDefaultText(key, localeProvider.getLocale()) != null;
+    }
+
+    @Override
+    public String getText(String key, String defaultValue, String arg) {
+        return localizedTextProvider.findDefaultText(key, localeProvider.getLocale(), new Object[] { arg });
+    }
+
+    @Override
+    public String getText(String key, List<?> args) {
+        return localizedTextProvider.findDefaultText(key, localeProvider.getLocale(), args.toArray());
+    }
+
+    @Override
+    public String getText(String key, String[] args) {
+        return localizedTextProvider.findDefaultText(key, localeProvider.getLocale(), args);
+    }
+
+    @Override
+    public String getText(String key, String defaultValue, List<?> args) {
+        return localizedTextProvider.findDefaultText(key, localeProvider.getLocale(), args.toArray());
+    }
+
+    @Override
+    public String getText(String key, String defaultValue, String[] args) {
+        return localizedTextProvider.findDefaultText(key, localeProvider.getLocale(), args);
+    }
+
+    @Override
+    public String getText(String key, String defaultValue, List<?> args, ValueStack stack) {
+        return localizedTextProvider.findDefaultText(key, localeProvider.getLocale(), args.toArray());
+    }
+
+    @Override
+    public String getText(String key, String defaultValue, String[] args, ValueStack stack) {
+        return localizedTextProvider.findDefaultText(key, localeProvider.getLocale(), args);
+    }
+
+    @Override
+    public ResourceBundle getTexts(String aBundleName) {
+        return localizedTextProvider.findResourceBundle(aBundleName, localeProvider.getLocale());
+    }
+
+    @Override
+    public ResourceBundle getTexts() {
+        return super.getTexts();
+    }
+}

http://git-wip-us.apache.org/repos/asf/struts-examples/blob/1b98fdcd/text-provider/src/main/java/org/apache/struts_example/SystemAction.java
----------------------------------------------------------------------
diff --git a/text-provider/src/main/java/org/apache/struts_example/SystemAction.java b/text-provider/src/main/java/org/apache/struts_example/SystemAction.java
new file mode 100644
index 0000000..b92fdb7
--- /dev/null
+++ b/text-provider/src/main/java/org/apache/struts_example/SystemAction.java
@@ -0,0 +1,45 @@
+/*
+ * $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.TextProvider;
+import com.opensymphony.xwork2.inject.Inject;
+
+public class SystemAction extends ActionSupport {
+
+    private TextProvider textProvider;
+
+    /**
+     * This injects existing an "internal" TextProvider which shouldn't be used by users
+     * as this can change without a notice
+     */
+    @Inject("system")
+    public void setTextProvider(TextProvider textProvider) {
+        this.textProvider = textProvider;
+    }
+
+    @Override
+    protected TextProvider getTextProvider() {
+        return textProvider;
+    }
+}

http://git-wip-us.apache.org/repos/asf/struts-examples/blob/1b98fdcd/text-provider/src/main/resources/DefaultMessages.properties
----------------------------------------------------------------------
diff --git a/text-provider/src/main/resources/DefaultMessages.properties b/text-provider/src/main/resources/DefaultMessages.properties
new file mode 100644
index 0000000..f10dea3
--- /dev/null
+++ b/text-provider/src/main/resources/DefaultMessages.properties
@@ -0,0 +1 @@
+default.index.title=Default: Custom Text Provider example
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/struts-examples/blob/1b98fdcd/text-provider/src/main/resources/org/apache/struts_example/package.properties
----------------------------------------------------------------------
diff --git a/text-provider/src/main/resources/org/apache/struts_example/package.properties b/text-provider/src/main/resources/org/apache/struts_example/package.properties
new file mode 100644
index 0000000..57afdb7
--- /dev/null
+++ b/text-provider/src/main/resources/org/apache/struts_example/package.properties
@@ -0,0 +1 @@
+default.index.title=Package: Custom Text Provider example
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/struts-examples/blob/1b98fdcd/text-provider/src/main/resources/struts.xml
----------------------------------------------------------------------
diff --git a/text-provider/src/main/resources/struts.xml b/text-provider/src/main/resources/struts.xml
new file mode 100644
index 0000000..867aee8
--- /dev/null
+++ b/text-provider/src/main/resources/struts.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE struts PUBLIC
+    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
+    "http://struts.apache.org/dtds/struts-2.5.dtd">
+<struts>
+  <constant name="struts.devMode" value="true"/>
+  <constant name="struts.convention.action.packages" value="org.apache.struts_example"/>
+  <constant name="struts.action.extension" value=","/>
+
+  <constant name="struts.custom.i18n.resources" value="DefaultMessages"/>
+
+  <!--
+  This factory overrides the one defined in Struts and will be used a default one across the framework
+  -->
+  <bean type="com.opensymphony.xwork2.TextProviderFactory"
+        class="org.apache.struts_example.MyTextProviderFactory"
+        name="myTextProviderFactory"
+        scope="singleton"/>
+
+  <constant name="struts.textProviderFactory" value="myTextProviderFactory"/>
+
+  <package name="default" extends="struts-default">
+
+    <default-action-ref name="index"/>
+
+  </package>
+</struts>

http://git-wip-us.apache.org/repos/asf/struts-examples/blob/1b98fdcd/text-provider/src/main/webapp/WEB-INF/content/success.jsp
----------------------------------------------------------------------
diff --git a/text-provider/src/main/webapp/WEB-INF/content/success.jsp b/text-provider/src/main/webapp/WEB-INF/content/success.jsp
new file mode 100644
index 0000000..b90803c
--- /dev/null
+++ b/text-provider/src/main/webapp/WEB-INF/content/success.jsp
@@ -0,0 +1,31 @@
+<%@ page contentType="text/html; charset=UTF-8" %>
+<%@ taglib prefix="s" uri="/struts-tags" %>
+<html>
+<head>
+  <title><s:text name="default.index.title"/></title>
+</head>
+
+<body>
+
+<h1>
+  <s:text name="default.index.title"/>
+</h1>
+
+<h3>Select your provider</h3>
+<ul>
+  <li>
+    <s:url var="url" action="index"/>
+    <s:a href="%{url}">Default aka scan everything</s:a>
+  </li>
+  <li>
+    <s:url var="url" action="system"/>
+    <s:a href="%{url}">System aka use only default bundles</s:a>
+  </li>
+  <li>
+    <s:url var="url" action="factory"/>
+    <s:a href="%{url}">Factory aka use custom TextProviderFactory</s:a>
+  </li>
+</ul>
+
+</body>
+</html>

http://git-wip-us.apache.org/repos/asf/struts-examples/blob/1b98fdcd/text-provider/src/main/webapp/WEB-INF/web.xml
----------------------------------------------------------------------
diff --git a/text-provider/src/main/webapp/WEB-INF/web.xml b/text-provider/src/main/webapp/WEB-INF/web.xml
new file mode 100644
index 0000000..8858656
--- /dev/null
+++ b/text-provider/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>