close

 

ps: 使用validate()在錯誤訊息呈現上<s:actionerror/> 有兩種常見的狀態

1.

this.addFieldError("season","Please select a league season.");

這個也就是在season下顯示"Please select a league season"

2.

this.addActionError("The 'year' field must within 2000 to 2010.");

也就是直接在<s:actionerror/> 這顯示

 

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

 

今天課程稍微感一點,一次教了兩個單元

主要是講了 驗證(validate) 以及 interceptor

首先先來看 validate

 

struts.xml 下:

<action name="add_league" class="duke.soccer.web.ctrler.action.AddLeagueAction" method="add">
<result name="success">/jsp/success.jsp</result>   
<result name="input">/jsp/add_league.jsp</result>  //(增加一個”input”的result,以提供驗證錯誤時能回到輸入資料的畫面)
</action>

 

如果驗證成功,則跑success ,若失敗 則是跑 input結果

ps(當有任何資料驗證的錯誤時,workflow interceptor會停止後續的處理並回覆”input”的結果.)

 

 

AddLeagueAction.java  class 下:

public String input() throws Exception{
return INPUT;
}
public String add() throws Exception{

// Perform business logic
ActionContext actCtx = ActionContext.getContext();
Map<String,Object> application = actCtx.getApplication();

LeagueService leagueSvc = (LeagueService)application.get("leagueSvc");
int year = Integer.parseInt(this.year);
league = leagueSvc.createLeague(year, season, title);

// Send the Success view
String result = "success";

return result;
}

public void validate(){

int year = -1;
try {
year = Integer.parseInt(this.year);
} catch (NumberFormatException nfe) {
this.addFieldError("year","The 'year' field must be a positive integer.");
}

if ( (year != -1) && ((year < 2000) || (year > 2010)) ) {
this.addFieldError("year","The 'year' field must within 2000 to 2010.");
}
if ( season.equals("UNKNOWN") ) {
this.addFieldError("season","Please select a league season.");
}
if ( title.length() == 0 ) {
this.addFieldError("title","Please enter the title of the league.");
}
}

 

validate()     

預設 Interceptor 定義在 struts-default.xml 中.
負責資料驗證的Interceptor

可以參考官網的網頁: http://struts.apache.org/release/2.1.x/docs/interceptors.html    

會發現 下面定義的部分

<interceptor-ref name="validation">
   <param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
   <param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
而 workflow 就是負責執行Action 中自訂的 validate() 而validation負責執行struts中的validators

OK後 當我們按下jsp頁面下的Button ,Action 所帶領到的class 便會先去執行我們所要驗證的validate()內

this.addFieldError("控制項名稱","message"); 利用這個屬性來設計驗證

//順道一提: 也可用 this.addActionError 則jsp頁面也記得要放 <s:actionerror /> 這個tag

add_league.jsp :

<s:actionerror/>
<%-- Generate main body --%>
<p>
This form allows you to create a new soccer league.
</p>
<s:form action="add_league" method="post">
<%-- Repopulate the year field --%>
<s:textfield name="year" label="Year" />

<%-- Repopulate the season drop-down menu --%>
<s:select name="season" label="Season"
headerKey="UNKNOWN" headerValue="UNKNOWN"
list="seasons_list" />

<%-- Repopulate the title field --%>
<s:textfield name="title" label="Title" />

<%-- The submit button --%>
<s:submit value="Add League"/>
</s:form>


已上,驗證及完成

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

    JoshS的部落格

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