2013-11-04 111 views
0

我是使用struts2框架的新手,並且對它有一些困惑。我有一個表單提交給一個操作類。我希望將提交的字段保存到客戶列表清單中。在動作類,我宣佈一個名單,明確了getModel方法來返回客戶的名單,並在編制方法(我不知道這是否是正確的),我添加的對象列出使用Struts2添加對象到列表

JSP

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%@ taglib prefix="s" uri="/struts-tags" %> 
<!DOCTYPE html> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>Add Account </title> 
</head> 
<body> 
    <h2> 
      Customer details 
    </h2> 
<s:form action="AddCustomerAction" name="/example" method="post"> 
    <h1> 
     <s:textfield label="Enter First Name" name="firstName" key="firstName" required="true" size="25"/> 
    </h1> 
    <h1> 
     <s:textfield label="Enter Last Name" name="lastName" required="true" size="25" /> 
    </h1> 

    <h1> 
     <s:textfield label="Enter Address" name="address" required="true" size="25" /> 
    </h1> 
    <h1> 
     <s:select value ="state" name="state" list="stateList" label="Select State" listKey="code" listValue="desc" required="true"/> 
    <h1> 
    <h1> 
     <s:textfield label="Enter City" name="city" required="true" size="25" /> 
    </h1> 
    <h1> 
     <s:textfield label="Enter Zipcode" name="zipcode" required="true" size="25" /> 
    </h1> 

    <h1> 
     <s:submit name="OK"/> 
    </h1> 
    </s:form> 
</body> 

Action類

package example; 

import com.opensymphony.xwork2.ActionSupport; 
import com.opensymphony.xwork2.ModelDriven; 
import java.util.ArrayList; 
import java.util.List; 

public class AddCustomerAction extends ActionSupport implements ModelDriven{ 

private Customer customer = new Customer(); 
private List<Customer> customerList; 

public List<Customer> getModel() 
{ 
    return customerList; 
} 

public String execute() 
{ 
    this.printCustomer(); 
    return SUCCESS; 
} 

public void prepare(){ 
    customerList = new ArrayList<Customer>(); 
    customerList.add(new Customer("first", "last")); 
} 


private void printCustomer(){ 
    System.out.println("First Name = "+customer.getFirstName()); 
    System.out.println("Last Name = "+customer.getLastName()); 
    System.out.println("Add = "+customer.getAddress()); 
    System.out.println("State = "+customer.getState()); 
    System.out.println("City = "+customer.getCity()); 
    System.out.println("Zipcode = "+customer.getZipcode());  
} 

在動作類我想從保存在客戶列表表單提交的信息,這樣我就可以遍歷它和DISP奠定數據。

當我運行這個,名字和姓氏是空(即使我輸入值)。

是我的做法(根據我的設置getModel()和prepare())是否正確?我錯過了什麼?

+0

嘗試使用它作爲參考。 [Struts的2:來自用模型驅動的體系結構的形式更新的對象列表] [1] [1]:http://stackoverflow.com/questions/13028113/struts-2-updating-從模型驅動架構的對象列表 – Alan

+0

在你的問題中確實存在很多問題。閱讀Struts2文檔,瞭解您還不明白的內容,以結構化的方式組織它,將其分解爲多個問題,然後在此處詢問(或先搜索它們)。我現在可以看到:1)發送 - 在Struts2中從JSP接收參數; 2)發送 - 接收列表; 3)在prepare()方法中讀取參數; 4)手動對象實例化5)ModelDriven的用法(對我來說是沒用的,恕我直言)... –

回答

1

問題是關於List<Customer>。您的表單不提供客戶名單,而是提交Customer。 所以

public Object getModel() 
{ 
    return customerList; 
} 

如果你想獲得客戶的名單,你必須改變你的JSP和太。

還要使用ModelDriven操作,請確保將Model Driven Interceptor應用於您的操作。

0
<s:textfield label="Enter First Name" name="firstName" key="firstName" required="true" size="25"/> 

此字段的名稱是firstName和因此,它需要一個名爲firstName

爲了得到客戶豆在你的行動充滿了從參數屬性/字段的getter/setter請將名稱更改爲customer.firstName

然後您需要一個設置爲customer的字段,您已擁有該字段。

這裏唯一的前提是,你需要有一個在你的客戶類名爲firstName領域,但看着customer.getFirstName()我猜你有一個在你的customer豆命名firstName場。

我試圖解釋清楚,參數如何從請求映射到動作,請讓我知道如果你什麼都不懂。