AUGUST 30, 2009 59 COMMENTS
Why i am writing this blog?
I faced lot of problems while creating my first application using Struts 2 on Google App Engine . So i decided to write a blog detailing how developers can create their application on Struts2 by avoiding the problems that i faced while developing my application.
Note
If you want to do FileUpload using struts 2 on google app engine please refer to this post.
Prerequisites for starting Struts2 Application on Google App Engine
Before you start building your sample application on google app engine using struts 2 you will need the following:-
Step by Step procedure to create Struts2 application on Google App Engine.
Step 1: Create a new project by clicking the New Web Application Project button in the toolbar.
Step 2 : Give the project name say login as we are going to create a simple login application. Enter package name as com.login and uncheck “Use Google Web Toolkit,” and ensure “Use Google App Engine” is checked and click the finish button.
Step3 : When you click the finish button you will get a sample HelloWorld application, which you can run going in the Run menu, select Run As > Web Application.By default application will run at port 8080, you can view the sample application at http://locahost:8080. For more information on the sample google web application created by the plugin you can refer to Google java app engine documentation .Please keep in mind that intent of this document is not to provide developers the overview of Google App engine for Java.
Step4 : By now you are ready with the google app engine infrastructure and we can move to the next step of creating a login application in Struts 2.
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</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>
index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Struts2 on Google App Engine</title> </head> <body> <h1>Struts2 on Google App Engine!</h1> <table> <tr> <td colspan="2" style="font-weight: bold;">Available Application:</td> </tr> <tr> <td><a href="/member/login" />Login</td> </tr> </table> </body> </html>
login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Please login</title> </head> <body> <s:actionerror/> <s:form action="home" method="post"> <s:textfield name="username" label="UserName"></s:textfield> <s:textfield name="password" label="Password"></s:textfield> <s:submit name="login" value="login"></s:submit> </s:form> </body> </html>
<?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> <include file="struts-default.xml"></include> <package name="member" namespace="/member" extends="struts-default"> <action name="login"> <result>/login.jsp</result> </action> </package> </struts>
SEVERE: Unable to set parameter [location] in result of type [org.apache.struts2.dispatcher.ServletDispatcherResult]
Caught OgnlException while setting property ‘location’ on type ‘org.apache.struts2.dispatcher.ServletDispatcherResult’. – Class: ognl.OgnlRuntime
File: OgnlRuntime.java
Method: invokeMethod
Line: 508 – ognl/OgnlRuntime.java:508:-1
at com.opensymphony.xwork2.ognl.OgnlUtil.internalSetProperty(OgnlUtil.java:392)
Caused by: java.lang.IllegalAccessException: Method [public void org.apache.struts2.dispatcher.StrutsResultSupport.setLocation(java.lang.String)] cannot be accessed.
at ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:508)
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:812)
at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:964)
at ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:75)
at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:131)
at com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.setProperty(ObjectAccessor.java:28)
at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:1656)
at ognl.ASTProperty.setValueBody(ASTProperty.java:101)
at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:177)
at ognl.SimpleNode.setValue(SimpleNode.java:246)
at ognl.Ognl.setValue(Ognl.java:476)
at com.opensymphony.xwork2.ognl.OgnlUtil.setValue(OgnlUtil.java:192)
at com.opensymphony.xwork2.ognl.OgnlUtil.internalSetProperty(OgnlUtil.java:385)
… 73 more
package com.login; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import ognl.OgnlRuntime; public class OgnlListener implements ServletContextListener { @Override public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub } @Override public void contextInitialized(ServletContextEvent arg0) { OgnlRuntime.setSecurityManager(null); } }
<listener> <listener-class>com.login.OgnlListener</listener-class> </listener>
javax.servlet.ServletException: java.lang.NoClassDefFoundError: javax.swing.tree.TreeNode is a restricted class. Please see the Google App Engine developer’s guide for more details.
at org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)
at org.apache.jasper.runtime.PageContextImpl.access$1100(PageContextImpl.java:64)
at org.apache.jasper.runtime.PageContextImpl$12.run(PageContextImpl.java:745)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:743)
at org.apache.jsp.login_jsp._jspService(login_jsp.java:86)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:806)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
at com.google.appengine.tools.development.PrivilegedJspServlet.access$101(PrivilegedJspServlet.java:23)
at com.google.appengine.tools.development.PrivilegedJspServlet$2.run(PrivilegedJspServlet.java:59)
at java.security.AccessController.doPrivileged(Native Method)
at com.google.appengine.tools.development.PrivilegedJspServlet.service(PrivilegedJspServlet.java)
To avoid this error you need to create a new package “freemarker.core” in your source folder and add the following class
</span></span> /* * Copyright (c) 2003 The Visigoth Software Society. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowledgement: * "This product includes software developed by the * Visigoth Software Society (http://www.visigoths.org/)." * Alternately, this acknowledgement may appear in the software itself, * if and wherever such third-party acknowledgements normally appear. * * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the * project contributors may be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [email protected]. * * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth" * nor may "FreeMarker" or "Visigoth" appear in their names * without prior written permission of the Visigoth Software Society. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Visigoth Software Society. For more * information on the Visigoth Software Society, please see * http://www.visigoths.org/ */ package freemarker.core; import java.io.IOException; /** * A TemplateElement representing a block of plain text. * * @version $Id: TextBlock.java,v 1.17 2004/01/06 17:06:42 szegedia Exp $ */ public final 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; } }
package com.login; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { private static final long serialVersionUID = 1L; private String username; private String password; public String login(){ if(username.equals("whyjava") && password.equals("password")){ addActionMessage("You are successfully logged in."); return SUCCESS; } addActionError("Username and Password Combination doesnot match."); return INPUT; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
<?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> <include file="struts-default.xml"></include> <package name="member" namespace="/member" extends="struts-default"> <action name="login"> <result>/login.jsp</result> </action> <action name="home" method="login" class="com.login.LoginAction"> <result>/home.jsp</result> <result name="input">/login.jsp</result> </action> </package> </struts>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Home Page</title> </head> <body> <s:actionmessage /> </body> </html>
I have tried to cover all the steps you will need to start of your first struts 2 project. Hope you all find this post useful.
If you need to start learning struts2 you can read Struts 2 in Action. It is a very good book to get a very good understanding of all the struts 2 concepts.