JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Application Server » GAE  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Google App Engine 整合 Struts2, Spring 及 JPA
J2





發文: 42
積分: 6
於 2010-07-27 13:52 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
STEP 1. 環境準備
1. 請先參考, 上一篇文章Google App Engine 整合 Spring 及 JPA
http://www.javaworld.com.tw/jute/post/view?bid=9&id=283078
2. 本範例是用Struts2.1.8, 建議是用一樣的版本, 比較不會有問題,下載之後把下面這些JAR檔, 放到Lib裡(2.1.8版比2.0版要多放兩個JAR檔, 要注意!)
1
2
3
4
5
6
7
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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<User> 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 (User user : users) {
        userDao.remove(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<User> getUsers() {
    return users;
  }
}


2. 新增struts設定檔: src/struts.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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
1
2
3
4
5
6
7
8
9
10
11
12
<%@ 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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>


J2 edited on 2010-07-29 08:09
reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
1531 Google App Engine 整合 Struts2, Spring 及 JPA J2 4586 2010-07-27 13:52
1127 Re:Google App Engine 整合 Struts2, Spring 及 JPA worookie 4 2010-07-27 13:55
963 Re:Google App Engine 整合 Struts2, Spring 及 JPA J2 313 2010-07-27 16:26
1061 Re:Google App Engine 整合 Struts2, Spring 及 JPA J2 181 2010-07-29 14:19
993 Re:Google App Engine 整合 Struts2, Spring 及 JPA stack 56 2010-08-08 21:18
» JWorld@TW »  Application Server » GAE

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8