2011年1月21日 星期五

轉貼 struts2 整合 Google Appengine

簡單的說 struts2 是第二代基於Model-View-Controller (MVC)模型的web應用框架。 詳細說明就請查閱 WIKI


步驟有點繁複,我這裡只整理簡單的步驟,詳細可以看參考文件

1. 下載 struts2 ,這裡我是下載 struts-2.2.1.1-all

2. 載入以下的 jar 檔, javassist-3.7.ga.jar 這個檔要到apps\struts2-blank-2.2.1\WEB-INF\lib 這裡找
2-1. commons-fileupload-1.2.1.jar
2-2. commons-io-1.3.2.jar
2-3. commons-logging-1.0.4.jar
2-4. freemarker-2.3.16.jar
2-5. ognl-3.0.jar
2-6. struts2-core-2.2.1.jar
2-7. xwork-2.2.1.jar
2-8. javassist-3.7.ga.jar


2. 建立 java



package com

import com.opensymphony.xwork2.ActionSupport;



@SuppressWarnings("serial")

public class LoginAction extends ActionSupport{

private String userName;

private String password;

public void setUserName(String name){

userName = name;

}

public void setPassword(String pwd){

password = pwd;

}



public String execute(){

if(userName.equals("gae") && password.equals("struts2")){

addActionMessage("Welcome," + userName);

return SUCCESS;

}

addActionError("Username and Password Combination doesnot match.");

return INPUT;

}

}






3. 配置 web.xml


<?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>



4. 建立要使用的jsp檔案

如 login.jsp 和 home.jsp


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>



home.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>Home Page</title>
</head>
<body>
<s:actionmessage />

</body>
</html>



5. 在SRC 目錄下建立 structs.xml 大小寫不能變, 設定檔案的對應




<?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>




6. 建立 ServletContextListener 避免錯誤

如下 命名為 OgnlListener , implements ServletContextListener


package com;

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

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;



import ognl.OgnlRuntime;

public class OgnlListener implements ServletContextListener,
HttpSessionListener, HttpSessionAttributeListener {

public OgnlListener() {
}

public void contextInitialized(ServletContextEvent sce) {
OgnlRuntime.setSecurityManager(null);
}

public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}

public void sessionCreated(HttpSessionEvent arg0) {
// TODO Auto-generated method stub
}

public void sessionDestroyed(HttpSessionEvent arg0) {
// TODO Auto-generated method stub
}

public void attributeAdded(HttpSessionBindingEvent arg0) {
// TODO Auto-generated method stub
}

public void attributeRemoved(HttpSessionBindingEvent arg0) {
// TODO Auto-generated method stub
}

public void attributeReplaced(HttpSessionBindingEvent arg0) {
// TODO Auto-generated method stub
}

}



7. 將對應的 檔案加入 web.xml宣告中



<?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">

<listener>
<listener-class>com.OgnlListener</listener-class>
</listener>

<result>/login.jsp</result>
</action>
</package>
</struts>



8. 建立 Package: freemarker.core, 並在下面建立 一個 TextBlock 的檔案 如下
https://docs.google.com/leaf?id=0B_JOf_Tyv_f6MzcwNDY5NTAtZTkwYS00OWUzLWJjN2ItNDhmNDNmYTg0NzQ4&hl=en


9. 完成執行GAE

參考文章:

Creating Struts2 application on Google App Engine (GAE)

在GAE中使用struts2框架

沒有留言: