2010年7月25日 星期日

AppEngine整合Struts2+Spring+JPA

STEP 1. 事前準備
1. 請先參考AppEngine整合Spring+JPA
2. 本範例是用Struts2.1.8, 建議是用一樣的版本, 比較不會有問題,下載之後把下面這些JAR檔, 放到Lib裡
  • commons-fileupload-1.2.1.jar
  • commons-io-1.3.2.jar
  • freemarker-2.3.15.jar
  • ognl-2.7.3.jar
  • struts2-core-2.1.8.1.jar
  • struts2-spring-plugin-2.1.8.1.jar
  • xwork-core-2.1.6.jar

STEP 2.因為GAE安全的設定比較嚴謹, 所以必須修改安全設定, 這裡是依照官網的討論用Listener的方式去修改
gae.InitListener

package gae;

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 InitListener implements ServletContextListener, HttpSessionListener, HttpSessionAttributeListener {
    public void contextInitialized(ServletContextEvent arg0) {
        OgnlRuntime.setSecurityManager(null); 
    }
    public void contextDestroyed(ServletContextEvent arg0) {}
    public void sessionCreated(HttpSessionEvent arg0) {}
    public void sessionDestroyed(HttpSessionEvent arg0) {}
    public void attributeAdded(HttpSessionBindingEvent arg0) {}
    public void attributeRemoved(HttpSessionBindingEvent arg0) {}
    public void attributeReplaced(HttpSessionBindingEvent arg0) {}
}


STEP 3. 修改 Action 及相關的設定
1. 修改 Action: fattom.riddle.TestAction

package fattom.riddle;

import java.util.List;
import fattom.riddle.dao.UserDao;
import fattom.riddle.entity.User;

public class TestAction{
    private UserDao userDao;
    private String name;
    private String action;
private List users;

    public String execute() throws Exception {
        if (name != null) {
            User tmpUser = new User();
            tmpUser.setName(name);
            userDao.persist(tmpUser);
        }

        users = userDao.findAll();

        if (action != null && "delete".equals(action)) {
            for (Object user : users) {
                userDao.remove((User)user);
            }
        }
       
        return "success";
    }

    public void setUserDao(UserDao userDao) {this.userDao = userDao;}
    public void setName(String name) {this.name = name;}
    public void setAction(String action) {this.action = action;}
    public List getUsers() {return users;}
}



2. 新增struts設定檔: src/struts.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>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />

    <package name="default" namespace="/" extends="struts-default">
        <action name="test" class="fattom.riddle.TestAction" method="execute">
            <result name="success">/WEB-INF/success.jsp</result>
        </action>
    </package>
</struts>


3. 新增Action回應的VIEW: /WEB-INF/success.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
    <title>Hello App Engine</title>
</head>
<body>
    <s:iterator value="users">
        <s:property value="id"/>/<s:property value="name"/><br>
     </s:iterator>
</body>
</html>



4. 修改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">
    <listener>
        <listener-class>gae.InitListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <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>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

沒有留言:

張貼留言