http://zhou137520.iteye.com/blog/1571670   struts2上傳範例

 

 

Ps: http://struts.apache.org/development/2.x/docs/struts-2-spring-2-jpa-ajax.html 

最下面有介紹 struts2和hibernate 需要用到的jar

======================================================== 

http://zh.scribd.com/doc/25244173/Java-Struts-Spring-Hibernate-Tutorial-github-com-chrishulbert-JavaTutorial

Struts2+hibernate 的基礎教學

=========================================================

http://www.dzone.com/tutorials/java/struts-2/struts-2-tutorial/struts-2-tutorial.html

上課時常用到的教學網站(非常實用)

 

===============================================================

 

上一篇 "上"  用到了intercepter 是屬於內建的定義

這篇要介紹自定的 intercepter

 

 

首先自訂一個interceptor class:

package duke.soccer.web.ctrler.interceptor;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class RegisterFormInterceptor implements Interceptor {
private String[] seasons_list;

@Override
public void destroy() {
// TODO Auto-generated method stub

}

@Override
public void init() {
// TODO Auto-generated method stub
String a = "a ";
}

@Override
public String intercept(ActionInvocation invocation) throws Exception {
ServletContext ctx = ServletActionContext.getServletContext();
seasons_list = ctx.getInitParameter("seasons_list").split(",");

ActionContext act=invocation.getInvocationContext();
act.put("seasons_list", seasons_list);
return invocation.invoke();
}

}

 

接著struts.xml 內也要有interceptors設定

<package name="duke_league" extends="struts-default">


<interceptors>  // interceptors 一定要放在 package 下
<interceptor name="registerGetForm" class="duke.soccer.web.ctrler.interceptor.RegisterFormInterceptor" />
</interceptors>

<action name="add_league" class="duke.soccer.web.ctrler.action.AddLeagueAction"
method="add">
<interceptor-ref name="registerGetForm"/>      // 這邊就擋住了下方執行 validate();
<result name="success">/jsp/success.jsp</result>
<result name="input">/jsp/add_league.jsp</result>
</action>

 

以上,當我們突然插入了一個 interceptor-ref 則就檔住了我們原本該跑的validate驗證

也堵住了 success的執行  則便會跳 null 錯誤

...但如果我們再插入了 一個<interceptor-ref  name ="defaultStack"   />

ex:

 <interceptor-ref name="registerGetForm"/> 

<interceptor-ref  name ="defaultStack"   />

則struts.xml 又會去跑原先預設的interceptor狀態

//也就是說 不但會跑registerGetForm也會跑預設的validate也就是要下defaultStack的interceptor-ref

 

 

未完待續 ....

 

 

 

 

struts2一些tag的介紹及範例 http://www.journaldev.com/2266/struts2-ui-tags-example-tutorial  

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 JoshS 的頭像
    JoshS

    JoshS的部落格

    JoshS 發表在 痞客邦 留言(0) 人氣()