mirror of
https://github.com/apache/struts.git
synced 2026-08-05 22:56:59 +00:00
- added a simple example into showcase demonstrating the usage of a
custom freemarker manager instantiated using Spring. - the custom freemarker manager have a custom util class injected into it through Spring's constructor injection - a simple ftl page just uses freemarker interpolation to display some date and time put into freemarker's model by the custom freemarker manager git-svn-id: https://svn.apache.org/repos/asf/struts/action2/trunk@412774 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
+54
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* $Id: FileUploadAction.java 394498 2006-04-16 15:28:06Z tmjee $
|
||||
*
|
||||
* Copyright 2006 The Apache Software Foundation.
|
||||
*
|
||||
* Licensed 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.action2.showcase.freemarker;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.struts.action2.views.freemarker.FreemarkerManager;
|
||||
import org.apache.struts.action2.views.freemarker.ScopesHashModel;
|
||||
|
||||
import com.opensymphony.xwork.util.OgnlValueStack;
|
||||
|
||||
/**
|
||||
* This is an example of a custom FreemarkerManager, mean to be
|
||||
* instantiated through Spring.
|
||||
* <p/>
|
||||
*
|
||||
* It will add into Freemarker's model
|
||||
* an utility class called {@link CustomFreemarkerManagerUtil} as a simple
|
||||
* example demonstrating how to extends FreemarkerManager.
|
||||
* <p/>
|
||||
*
|
||||
* The {@link CustomFreemarkerManagerUtil} will be created by Spring and
|
||||
* injected through constructor injection.
|
||||
* <p/>
|
||||
*/
|
||||
public class CustomFreemarkerManager extends FreemarkerManager {
|
||||
|
||||
private CustomFreemarkerManagerUtil util;
|
||||
|
||||
public CustomFreemarkerManager(CustomFreemarkerManagerUtil util) {
|
||||
this.util = util;
|
||||
}
|
||||
|
||||
public void populateContext(ScopesHashModel model, OgnlValueStack stack, Object action, HttpServletRequest request, HttpServletResponse response) {
|
||||
super.populateContext(model, stack, action, request, response);
|
||||
model.put("customFreemarkerManagerUtil", util);
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* $Id: FileUploadAction.java 394498 2006-04-16 15:28:06Z tmjee $
|
||||
*
|
||||
* Copyright 2006 The Apache Software Foundation.
|
||||
*
|
||||
* Licensed 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.action2.showcase.freemarker;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* This class is just a simple util that gets injected into
|
||||
* {@link CustomFreemarkerManager} through Spring's constructor
|
||||
* injection, serving as a simple example in SAF2's Showcase.
|
||||
*/
|
||||
public class CustomFreemarkerManagerUtil {
|
||||
|
||||
public String getTodayDate() {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
|
||||
return sdf.format(new Date());
|
||||
}
|
||||
|
||||
public String getTimeNow() {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
|
||||
return sdf.format(new Date());
|
||||
}
|
||||
}
|
||||
@@ -11,4 +11,14 @@
|
||||
|
||||
<bean id="personManager" class="org.apache.struts.action2.showcase.person.PersonManager" singleton="true"/>
|
||||
|
||||
<!-- Showcase's CustomFreemarkerManager example -->
|
||||
<bean id="customFreemarkerManager" class="org.apache.struts.action2.showcase.freemarker.CustomFreemarkerManager">
|
||||
<constructor-arg index="0">
|
||||
<ref bean="customFreemarkerManagerUtil" />
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
<bean id="customFreemarkerManagerUtil" class="org.apache.struts.action2.showcase.freemarker.CustomFreemarkerManagerUtil" />
|
||||
|
||||
|
||||
</beans>
|
||||
|
||||
|
||||
@@ -6,3 +6,4 @@ struts.objectFactory = spring
|
||||
struts.custom.i18n.resources=globalMessages
|
||||
#struts.action.extension=jspa
|
||||
struts.url.http.port = 8080
|
||||
struts.freemarker.manager.classname=customFreemarkerManager
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<!DOCTYPE xwork PUBLIC
|
||||
"-//OpenSymphony Group//XWork 1.1.1//EN"
|
||||
"http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">
|
||||
|
||||
|
||||
<xwork>
|
||||
<include file="struts-default.xml" />
|
||||
|
||||
<package name="freemarker" namespace="/freemarker" extends="struts-default">
|
||||
<action name="customFreemarkerManagerDemo">
|
||||
<result type="freemarker">/freemarker/customFreemarkerManagerUsage.ftl</result>
|
||||
</action>
|
||||
</package>
|
||||
</xwork>
|
||||
@@ -37,6 +37,8 @@
|
||||
<include file="xwork-filedownload.xml" />
|
||||
|
||||
<include file="xwork-conversion.xml" />
|
||||
|
||||
<include file="xwork-freemarker.xml" />
|
||||
|
||||
<package name="default" extends="struts-default">
|
||||
<interceptors>
|
||||
|
||||
@@ -77,6 +77,7 @@
|
||||
<li><a href="<saf:url value="/filedownload/index.jsp"/>">File Download</a></li>
|
||||
<li><a href="<saf:url value="/conversion/index.jsp"/>"/>Conversion</a></li>
|
||||
<li><a href="<saf:url action="index" namespace="/jsf"/>">JSF</a></li>
|
||||
<li><a href="<saf:url value="/freemarker/index.jsp"/>">Freemarker</a>
|
||||
<li class="last"><a href="<saf:url value="/help.jsp"/>">Help</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@@ -56,6 +56,9 @@
|
||||
|
||||
<!-- model-driven -->
|
||||
<li><saf:url id="url" action="modelDriven" namespace="/modelDriven" method="input"/><saf:a href="%{url}">Model Driven Example</saf:a>
|
||||
|
||||
<!-- freemarker -->
|
||||
<li><saf:url id="url" value="/freemarker" /><saf:a href="%{#url}">Freemarker Example</saf:a></li>
|
||||
</ul>
|
||||
</p>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user