顯示具有 struts2 標籤的文章。 顯示所有文章
顯示具有 struts2 標籤的文章。 顯示所有文章

2012年4月3日 星期二

心得 Struct2

最近又報告一次,整理一下投影片放在這裡

2011年6月16日 星期四

心得 google app engine 1.5 支援 struct2 有問題

看來google 又失誤了 還是 struct2 太少人用了 哈
目前解決的方式 是 改sdk 到1.4 或 1.43 版

下載舊的版本可以到下面link
選擇 search alldownload, 預設是顯示最新版的API.



參考:http://code.google.com/p/googleappengine/issues/detail?id=5053

2011年1月26日 星期三

struts 2 首頁

如題,想要在首頁就使用 structs 2 定義的頁面 是有點問題,會顯示不出來, 網路上搜尋 struts2 index.action 就有很多結果

目前還沒全部看懂,限介紹一個簡單的方法,就是先建一個 假的首頁 ,在利用 location.href 的方式轉到真正的首頁

入以下的 index.htm


<!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=BIG5">
<title>index</title>

<SCRIPT>

function Init(){
location.href= "index.action ";
}
</SCRIPT>
</head>
<body onload="Init()">

</body>
</html>



為何要那樣麻煩,在我的應用是為了要支援多國語言的首頁.

2011年1月24日 星期一

struts2 多國語言依據瀏覽器語言選項

主要參考了這篇文章 struct2 國際化

我是在GAE 上執行的 所以相關設定可以參考 轉貼 struts2 整合 Google Appengine

的設定,主要包含 下載 struts2 , web.xml 設定 , structs.xml ,ServletContextListener 和 freemarker.core.TextBlock 等步驟

要使用 struct 的多國語言,不只一種方式,這裡我介紹的是在 struts.xml 中的宣告

在 struts.xml 加入 以下的敘述


<constant name="struts.custom.i18n.resources" value="applicationResource" />


value 就是自訂的名稱, 因為 struts2 資源檔的定義格式就是 自訂的名稱_語系簡碼_國家簡碼.properties 例如: applicationResource_zh_tw.properties

修改後的 struts.xml 如下

<!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.devMode" value="false" />

<constant name="struts.custom.i18n.resources" value="applicationResource" />

</struts>



若在eclipse 下開發的話還可以下載 “PropEdit”(http://propedit.sourceforge.jp/eclipse/updates/)這個外掛
來源參考 Struts 2.0系列教程(四)Struts2国际化(i18n)您的应用程序




簡單的jsp 如下


<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>

<body>
<h1>Struts 2 i18n tag example</h1>

<s:text name="hello"/>


</body>
</html>


properties 檔的設定文字必須要先轉成 unicode 的形式字串

在java中 可以呼叫native2ascii 來協助達成, native2ascii -編碼 來源檔案 目標檔案


例如
native2ascii -encoding utf8 applicationResource_zh_tw.properties applicationResource_zh_tw2.properties




加入 properties 檔 在 src 資料夾下面 如圖




就可以開始執行

結果如範例網站 ,這裡支援 俄文(ru)、繁體中文(zh_tw)、簡體中文(zh_CN)、英文(en)
http://11.latest.catontest.appspot.com/HelloWorld.jsp

要改預覽語言的方式:

IE:
工具-> 網際網路選項-> 語言


Firefox
工具->選項->內容-> 語言->選擇

2011年1月21日 星期五

轉貼 struts2 與 servlet 共用

在 web.xml 下加入以下設定, 否則 servlet 也會被 struts2 濾掉


<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>

轉貼 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框架