2015-05-06 73 views
0

這裏是我的DAO:如何從<s:select>字符串得到一個整數值

public ReportType getByName(String type) { 
    EntityManager em = emf.createEntityManager(); 
    try { 
     ReportType rptype2 = em.find(ReportType.class, type); 

     return rptype2; 
    } catch (Exception e) { 
     e.printStackTrace(); 
     em.close(); 
    } 
    return null; 
} 

這裏是我的操作:

ReportDAO dao = new ReportDAO(); 
    List<ReportType> reportType = dao.show(); 
    list = new ArrayList<>(); 
    for (ReportType reportType1 : reportType) { 
     list.add(reportType1.getName()); 
    } 
    ReportTypeDAO rpDAO = new ReportTypeDAO(); 
    reporttype = rpDAO.getByName(type); 

這裏是我的jsp:

<h3>Type: <s:select list="list" name="type"></s:select> 

這裏是我的桌子:

CREATE TABLE [dbo].[Report_Type](
[id] [int] IDENTITY(1,1) NOT NULL, 
[name] [nvarchar](100) NULL, 

當我提交時,我收到一個字符串格式(名稱)的記錄,但我想獲得這個記錄ID。 有沒有解決方法?

+0

'list.add(reportType1.getName());'。您正在將'name'添加到此列表中。您應該添加'id'代替。 – CKing

+0

@ChetanKinger我認爲OP將在他們的列表中需要名稱**和** ID –

回答

0

使用listKeylistValue

<s:select list = "list" 
     listKey = "id" 
    listValue = "name" 
      name = "type" /> 

瞭解更多關於the documentation

0

你需要指定使用listKey和listValue在你的選擇ID和名稱。

<s:select label="My Label" 
     name="name" 
     list="list" 
     listKey="id" 
     listValue="name" 
     "/> 
相關問題