`
neilone.cn
  • 浏览: 21796 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

GAE中增加Struts2支持

    博客分类:
  • GAE
阅读更多

1.增加Struts2的jar包


                      

2.修改web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
		</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<listener>
		<listener-class>cn.neilone.OgnlListener</listener-class>
	</listener>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 
 这里需要增加一个监听器,代码如下

package cn.neilone;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import ognl.OgnlRuntime;

public class OgnlListener implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {

	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		OgnlRuntime.setSecurityManager(null);
	}
}

 

3.增加struts.xml文件

该文件与普通的javaee工程无异,仅贴一份我自己的拷贝

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />

    <package name="default" namespace="/" extends="struts-default">
    	<default-action-ref name="index"></default-action-ref>
    	<action name="index">
    		<result>/index.jsp</result>
    	</action>
        <action name="hello" class="cn.neilone.struts2.HelloAction" method="sayHello">
			<result>/sayHello.jsp</result>
        </action>
    </package>

</struts>

 

4.一个问题的解决

新建包freemarker.core,添加类TextBlock.java,源码如下

package freemarker.core;

import java.io.IOException;

public class TextBlock extends TemplateElement {
	private static final char[] EMPTY_CHAR_ARRAY = new char[0];
	static final TextBlock EMPTY_BLOCK = new TextBlock(EMPTY_CHAR_ARRAY, false);
	// We're using char[] instead of String for storing the text block because
	// Writer.write(String) involves copying the String contents to a char[]
	// using String.getChars(), and then calling Writer.write(char[]).By
	// using Writer.write(char[]) directly, we avoid array copying on each
	// write.
	private char[] text;
	private final boolean unparsed;

	public TextBlock(String text) {
		this(text, false);
	}

	public TextBlock(String text, boolean unparsed) {
		this(text.toCharArray(), unparsed);
	}

	private TextBlock(char[] text, boolean unparsed) {
		this.text = text;
		this.unparsed = unparsed;
	}

	/**
	 * Simply outputs the text.
	 */
	public void accept(Environment env) throws IOException {
		env.getOut().write(text);
	}

	public String getCanonicalForm() {
		String text = new String(this.text);
		if (unparsed) {
			return "<#noparse>" + text + "</#noparse>";
		}
		return text;
	}

	public String getDescription() {
		String s = new String(text).trim();
		if (s.length() == 0) {
			return "whitespace";
		}
		if (s.length() > 20) {
			s = s.substring(0, 20) + "...";
			s = s.replace('\n', ' ');
			s = s.replace('\r', ' ');
		}
		return "text block (" + s + ")";
	}

	TemplateElement postParseCleanup(boolean stripWhitespace) {
		if (text.length == 0)
			return this;
		int openingCharsToStrip = 0, trailingCharsToStrip = 0;
		boolean deliberateLeftTrim = deliberateLeftTrim();
		boolean deliberateRightTrim = deliberateRightTrim();
		if (!stripWhitespace || text.length == 0) {
			return this;
		}
		if (parent.parent == null && previousSibling() == null)
			return this;
		if (!deliberateLeftTrim) {
			trailingCharsToStrip = trailingCharsToStrip();
		}
		if (!deliberateRightTrim) {
			openingCharsToStrip = openingCharsToStrip();
		}
		if (openingCharsToStrip == 0 && trailingCharsToStrip == 0) {
			return this;
		}
		this.text = substring(text, openingCharsToStrip, text.length
				- trailingCharsToStrip);
		if (openingCharsToStrip > 0) {
			this.beginLine++;
			this.beginColumn = 1;
		}
		if (trailingCharsToStrip > 0) {
			this.endColumn = 0;
		}
		return this;
	}

	/**
	 * Scans forward the nodes on the same line to see whether there is a
	 * deliberate left trim in effect. Returns true if the left trim was
	 * present.
	 */
	private boolean deliberateLeftTrim() {
		boolean result = false;
		for (TemplateElement elem = this.nextTerminalNode(); elem != null
				&& elem.beginLine == this.endLine; elem = elem
				.nextTerminalNode()) {
			if (elem instanceof TrimInstruction) {
				TrimInstruction ti = (TrimInstruction) elem;
				if (!ti.left && !ti.right) {
					result = true;
				}
				if (ti.left) {
					result = true;
					int lastNewLineIndex = lastNewLineIndex();
					if (lastNewLineIndex >= 0 || beginColumn == 1) {
						char[] firstPart = substring(text, 0,
								lastNewLineIndex + 1);
						char[] lastLine = substring(text, 1 + lastNewLineIndex);
						if (trim(lastLine).length == 0) {
							this.text = firstPart;
							this.endColumn = 0;
						} else {
							int i = 0;
							while (Character.isWhitespace(lastLine[i])) {
								i++;
							}
							char[] printablePart = substring(lastLine, i);
							this.text = concat(firstPart, printablePart);
						}
					}
				}
			}
		}
		if (result) {
		}
		return result;
	}

	/**
	 * Checks for the presence of a t or rt directive on the same line. Returns
	 * true if the right trim directive was present.
	 */
	private boolean deliberateRightTrim() {
		boolean result = false;
		for (TemplateElement elem = this.prevTerminalNode(); elem != null
				&& elem.endLine == this.beginLine; elem = elem
				.prevTerminalNode()) {
			if (elem instanceof TrimInstruction) {
				TrimInstruction ti = (TrimInstruction) elem;
				if (!ti.left && !ti.right) {
					result = true;
				}
				if (ti.right) {
					result = true;
					int firstLineIndex = firstNewLineIndex() + 1;
					if (firstLineIndex == 0) {
						return false;
					}
					if (text.length > firstLineIndex
							&& text[firstLineIndex - 1] == '\r'
							&& text[firstLineIndex] == '\n') {
						firstLineIndex++;
					}
					char[] trailingPart = substring(text, firstLineIndex);
					char[] openingPart = substring(text, 0, firstLineIndex);
					if (trim(openingPart).length == 0) {
						this.text = trailingPart;
						this.beginLine++;
						this.beginColumn = 1;
					} else {
						int lastNonWS = openingPart.length - 1;
						while (Character.isWhitespace(text[lastNonWS])) {
							lastNonWS--;
						}
						char[] printablePart = substring(text, 0, lastNonWS + 1);
						if (trim(trailingPart).length == 0) {
							// THIS BLOCK IS HEINOUS! THERE MUST BE A BETTER
							// WAY! REVISIT (JR)
							boolean trimTrailingPart = true;
							for (TemplateElement te = this.nextTerminalNode(); te != null
									&& te.beginLine == this.endLine; te = te
									.nextTerminalNode()) {
								if (te.heedsOpeningWhitespace()) {
									trimTrailingPart = false;
								}
								if (te instanceof TrimInstruction
										&& ((TrimInstruction) te).left) {
									trimTrailingPart = true;
									break;
								}
							}
							if (trimTrailingPart)
								trailingPart = EMPTY_CHAR_ARRAY;
						}
						this.text = concat(printablePart, trailingPart);
					}
				}
			}
		}
		return result;
	}

	/*
	 * private String leftTrim(String s) { int i =0; while (i<s.length()) { if
	 * (!Character.isWhitespace(s.charAt(i))) break; ++i; } return
	 * s.substring(i); }
	 */
	private int firstNewLineIndex() {
		String content = new String(text);
		int newlineIndex1 = content.indexOf('\n');
		int newlineIndex2 = content.indexOf('\r');
		int result = newlineIndex1 >= 0 ? newlineIndex1 : newlineIndex2;
		if (newlineIndex1 >= 0 && newlineIndex2 >= 0) {
			result = Math.min(newlineIndex1, newlineIndex2);
		}
		return result;
	}

	private int lastNewLineIndex() {
		String content = new String(text);
		return Math.max(content.lastIndexOf('\r'), content.lastIndexOf('\n'));
	}

	/**
	 * figures out how many opening whitespace characters to strip in the
	 * post-parse cleanup phase.
	 */
	private int openingCharsToStrip() {
		int newlineIndex = firstNewLineIndex();
		if (newlineIndex == -1 && beginColumn != 1) {
			return 0;
		}
		++newlineIndex;
		if (text.length > newlineIndex) {
			if (newlineIndex > 0 && text[newlineIndex - 1] == '\r'
					&& text[newlineIndex] == '\n') {
				++newlineIndex;
			}
		}
		if (new String(text).substring(0, newlineIndex).trim().length() > 0) {
			return 0;
		}
		// We look at the preceding elements on the line to see if we should
		// strip the opening newline and any whitespace preceding it.
		for (TemplateElement elem = this.prevTerminalNode(); elem != null
				&& elem.endLine == this.beginLine; elem = elem
				.prevTerminalNode()) {
			if (elem.heedsOpeningWhitespace()) {
				return 0;
			}
		}
		return newlineIndex;
	}

	/**
	 * figures out how many trailing whitespace characters to strip in the
	 * post-parse cleanup phase.
	 */
	private int trailingCharsToStrip() {
		String content = new String(text);
		int lastNewlineIndex = lastNewLineIndex();
		if (lastNewlineIndex == -1 && beginColumn != 1) {
			return 0;
		}
		String substring = content.substring(lastNewlineIndex + 1);
		if (substring.trim().length() > 0) {
			return 0;
		}
		// We look at the elements afterward on the same line to see if we
		// should strip any whitespace after the last newline
		for (TemplateElement elem = this.nextTerminalNode(); elem != null
				&& elem.beginLine == this.endLine; elem = elem
				.nextTerminalNode()) {
			if (elem.heedsTrailingWhitespace()) {
				return 0;
			}
		}
		return substring.length();
	}

	boolean heedsTrailingWhitespace() {
		if (isIgnorable()) {
			return false;
		}
		for (int i = 0; i < text.length; i++) {
			char c = text[i];
			if (c == '\n' || c == '\r') {
				return false;
			}
			if (!Character.isWhitespace(c)) {
				return true;
			}
		}
		return true;
	}

	boolean heedsOpeningWhitespace() {
		if (isIgnorable()) {
			return false;
		}
		for (int i = text.length - 1; i >= 0; i--) {
			char c = text[i];
			if (c == '\n' || c == '\r') {
				return false;
			}
			if (!Character.isWhitespace(c)) {
				return true;
			}
		}
		return true;
	}

	boolean isIgnorable() {
		if (text == null || text.length == 0) {
			return true;
		}
		if (!isWhitespace()) {
			return false;
		}
		// trick here
		boolean atTopLevel = true;
		TemplateElement prevSibling = previousSibling();
		TemplateElement nextSibling = nextSibling();
		return ((prevSibling == null && atTopLevel) || nonOutputtingType(prevSibling))
				&& ((nextSibling == null && atTopLevel) || nonOutputtingType(nextSibling));
	}

	private boolean nonOutputtingType(TemplateElement element) {
		return (element instanceof Macro || element instanceof Assignment
				|| element instanceof AssignmentInstruction
				|| element instanceof PropertySetting
				|| element instanceof LibraryLoad || element instanceof Comment);
	}

	private static char[] substring(char[] c, int from, int to) {
		char[] c2 = new char[to - from];
		System.arraycopy(c, from, c2, 0, c2.length);
		return c2;
	}

	private static char[] substring(char[] c, int from) {
		return substring(c, from, c.length);
	}

	private static char[] trim(char[] c) {
		if (c.length == 0) {
			return c;
		}
		return new String(c).trim().toCharArray();
	}

	private static char[] concat(char[] c1, char[] c2) {
		char[] c = new char[c1.length + c2.length];
		System.arraycopy(c1, 0, c, 0, c1.length);
		System.arraycopy(c2, 0, c, c1.length, c2.length);
		return c;
	}

	boolean isWhitespace() {
		return text == null || trim(text).length == 0;
	}

}

 

5.新建一二Action即可测试

包结构如下


                    
 

  • 大小: 1.9 KB
  • 大小: 9.8 KB
分享到:
评论

相关推荐

    GAE Struts2配置

    标题中的“GAE Struts2配置”指的是在Google App Engine (GAE) 上配置Struts2框架的过程。GAE是一个基于Java的云平台,允许开发者部署Web应用,而Struts2是一个流行的MVC(模型-视图-控制器)框架,用于构建Java Web...

    Struts2,Spring,JDO,AJAX on GAE

    Struts2、Spring、JDO(Java Data Objects)和AJAX(Asynchronous JavaScript and XML)是四个在Web应用开发中非常关键的技术。这篇博客“Struts2,Spring,JDO,AJAX on GAE”可能探讨了如何在Google App Engine (GAE)...

    基于GAE的Demo

    在【gae-struts2-spring-tiles-starter】这个压缩包中,我们可以预见到包含以下几个部分: 1. 项目配置:如pom.xml(Maven配置文件),包含了项目的依赖信息,如GAE SDK、Struts2、Spring和Tiles的版本。 2. 源代码...

    GAE中整合SSH2的空项目备份

    标题中的“GAE中整合SSH2的空项目备份”指的是在Google App Engine (GAE) 平台上集成Secure Shell (SSH) v2协议的一个空白项目,可能是为了实现远程管理和调试应用的功能。GAE是一个托管服务,它允许开发者部署Java...

    gae strus2 spring 整合

    压缩包中的"gae-struts2-spring-tiles-starter"可能是一个启动项目,包含了整合Struts2、Spring和Tiles的示例代码。Tiles是一个视图分辨率框架,它可以组合多个JSP片段来创建复杂页面。通过Struts2和Tiles的结合,...

    让gae支持php的方法

    标题"让gae支持php的方法"所指的就是如何在GAE环境中运行PHP应用程序。描述中提到的"基于java的Quercus"是一个关键的解决方案,它是一个兼容PHP的Java实现,可以让我们在不直接支持PHP的GAE上运行PHP代码。 Quercus...

    gea 整合struts2+jpa+spring实例

    在本实例中,我们将探讨如何将Google App Engine (GAE) 与三个强大的Java框架——Struts2、JPA(Java Persistence API)以及Spring进行整合,以构建一个高效的Web应用程序。GAE是一个由Google提供的云平台,允许...

    GAE使用规则

    GAE使用规则GAE使用规则GAE使用规则GAE使用规则GAE使用规则GAE使用规则GAE使用规则GAE使用规则GAE使用规则

    gae-pytorch-master_pytorch_pytorchgae_GAE_自编码器_gaepytorchmaster_

    在【标签】中,"pytorch"、"pytorchgae"、"GAE"、"自编码器" 和 "gaepytorchmaster" 是关键点。"pytorch" 是一个广泛使用的深度学习框架,提供了灵活的计算图机制,适合构建复杂的神经网络模型,如自编码器。...

    gae_in_pytorch-master_GAE_

    **图形自动编码器(GAE)在PyTorch中的实现** **一、GAE概述** 图形自动编码器(Graph Autoencoder, GAE)是一种应用于图数据的深度学习模型,它结合了自动编码器(Autoencoder)的思想与图神经网络(Graph Neural...

    spring+gae

    2. **数据存储**:Spring与GAE的数据存储API(如JDO或JPA)结合,可以提供一个统一的数据访问层,简化对Google Datastore的操作。 3. **调度任务**:Spring的TaskExecution和TaskScheduler模块可以与GAE的后台任务...

    关于GAE的教程、工具与文档(建站)

    GAE支持多种编程语言,包括Python、Java、Go、Node.js等,提供了完整的基础设施,如数据库服务、存储系统、身份验证以及负载均衡。 在学习和使用GAE进行建站的过程中,以下几个关键知识点是必须掌握的: 1. **开发...

    GAE包(以配置好,解压可用)

    Python是GAE支持的主要编程语言之一,用于编写应用程序。SDU(可能是某种特定的工具或服务)里的exe文件可能是用于启动或管理GAE环境的可执行程序。而“客户端加服务端中localproxy中的proxy.py”可能是一个本地代理...

    GAE入门教程

    pass之GAE入门教程, 学习GAE

    如何在GAE中轻松使用Django框架

    4. 由于GAE不支持传统的SQLite数据库,你需要在开发环境中使用如MySQL或PostgreSQL等其他数据库,然后在生产环境中切换到Datastore。 5. 设置`urls.py`文件,确保URL路由正常工作。 6. 在`app.yaml`文件中定义你的...

Global site tag (gtag.js) - Google Analytics