package demo;
/**
* Copyright 2004, James M. Turner.
* All Rights Reserved
*
* A Struts action that sends the new comments to the console
*/
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.validator.DynaValidatorForm;
public class PRocessFormAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
DynaValidatorForm df = (DynaValidatorForm) form;
Person[] p = (Person[]) df.get("people");
for (int i = 0; i < p.length; i++) {
System.out.println(p[i].getFirstName() + " " + p[i].
getLastName() + ":" + p[i].getComment());
}
return mapping.findForward("sUCcess");
}
}
在實(shí)際的應(yīng)用中,這就是數(shù)據(jù)寫(xiě)回到數(shù)據(jù)庫(kù)的地方。在這種情況下,他只將數(shù)據(jù)倒在控制臺(tái)上所以我們可以看到他是正確收到的。假設(shè)我們?yōu)槊總€(gè)演員都填充了恰當(dāng)?shù)闹担覀冊(cè)诳刂婆_(tái)上會(huì)看到下列內(nèi)容:
Harrison Ford:Indiana Jones
Carrie Fisher:Postcards from the Edge
Mark Hamill:Wing Commander
正如我在文章開(kāi)頭提到的一樣,還有另一個(gè)方法可以解決這個(gè)問(wèn)題,而且它不需要使用會(huì)話期范圍內(nèi)的表格。這個(gè)方法就是使用HashMaps 來(lái)存儲(chǔ)行。我們來(lái)看看使用HashMaps編寫(xiě)的同一段代碼:
首先,我們添加一個(gè)新表格到struts-config.xml:
現(xiàn)在,我們不使用beans的排列,改為使用HashMap 來(lái)存儲(chǔ)每個(gè)人的數(shù)據(jù)。另外,我們需要一個(gè)新的HashMap 來(lái)存儲(chǔ)注釋,原因我稍后再解釋。我們也需要一個(gè)新的動(dòng)作來(lái)填充數(shù)據(jù):
package demo;
/**
* Copyright 2004, James M. Turner.
* All Rights Reserved
*
* A Struts action that sets up a DynaForm which is globally scoped
*/
import java.io.IOException;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.validator.DynaValidatorForm;
public class SetupHashFormAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response)
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注