gpt4 book ai didi

java - 如何从同名的 Tapestry 文本字段中获取数据?

转载 作者:行者123 更新时间:2023-11-30 02:14:58 25 4
gpt4 key购买 nike

我对 Tapestry 很陌生,我在使用它时遇到了几个问题,如何从同名的 Tapestry 文本字段中获取值?

例如:

<t:form t:id="names">
<t:errors/>
<div class="input-box">
<t:textfield type="text" name="birthdate[Day]"/>
<t:textfield type="text" name="birthdate[Month]"/>
<t:textfield type="text" name="birthdate[Year]"/>
</div>
<div class="input-box">
<div class="col-md-12">
<input type="submit" name="proceed" class="btn" value="Proceed" />
</div>
</div>
</t:form>

我尝试用birthdate[Day] = 20、birthdate[Month] = 08、birthdate[Year] = 1992填充它,然后在后端调试它,如下所示:

@Property
@Persist(PersistenceConstants.FLASH)
private List<String> birthdate;

Object onSuccess() {
logger.info("data birthdate: "+birthdate); // print null
logger.info("data birthdate toString: "+birthdate.toString()); // print null
logger.info("data birthdate 0: "+birthdate.get(0)); // print null

return null;
}

它们都只返回 null..

如何解决这个问题?

提前谢谢

最佳答案

来自 Spring 和 Hibernate 时代,Tapestry 感觉很奇怪。根据我 10 分钟的阅读时间,您没有正确映射字段,并且缺少一些内容,例如

  • 您没有使用t:id
  • 您的 .tml 中有 3 个文本字段,.java 中有一个列表,不确定您打算在那里做什么

作为一个例子,我做了如下的事情 Names.java

package com.raf.test.pages;

import java.util.Date;

import org.apache.tapestry5.PersistenceConstants;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.slf4j.Logger;

public class Names {
@Inject
private Logger logger;

@Property
@Persist(PersistenceConstants.FLASH)
private String birthDay;

@Property
@Persist(PersistenceConstants.FLASH)
private String birthMonth;

@Property
@Persist(PersistenceConstants.FLASH)
private String birthYear;

@Property
@Persist(PersistenceConstants.FLASH)
private String aDate;

@Property
@Persist(PersistenceConstants.FLASH)
private String monthAndYear;

@Property
@Persist(PersistenceConstants.FLASH)
private Date actualDateField;

Object onSuccess() {
logger.info("birthDay: " + birthDay);
logger.info("birthMonth: " + birthMonth);
logger.info("birthYear: " + birthYear);

if(aDate != null && !aDate.isEmpty()) {
String[] chunks = aDate.split("-");
if(chunks.length > 2) {
logger.info("aDate [Year]: " + chunks[0]);
logger.info("aDate [Month]: " + chunks[1]);
logger.info("aDate [Day]: " + chunks[2]);
}
}

logger.info("monthAndYear: " + monthAndYear);

logger.info("actualDateField: " + actualDateField);

return null;
}
}

Names.tml如下

<html t:type="layout" title="test com.example"
xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd"
xmlns:p="tapestry:parameter">

<t:form t:id="names">
<t:errors/>
<div class="input-box">
<t:textfield type="text" name="birthdate[Day]" placeholder="birthdate[Day]" t:id="birthDay"/>
<t:textfield type="text" name="birthdate[Month]" placeholder="birthdate[Month]" t:id="birthMonth"/>
<t:textfield type="text" name="birthdate[Year]" placeholder="birthdate[Year]" t:id="birthYear"/>
<!-- Uses html5 date type-->
<t:textfield type="date" name="normladate" placeholder="Normal date" t:id="aDate"/>

<!-- Uses html5 month -->
<t:textfield type="month" name="justmonth" placeholder="Month Year" t:id="monthAndYear"/>

<!-- Actual date field -->
<t:datefield name="actualDateField" placeholder="Actual date" t:id="actualDateField"/>
</div>
<div class="input-box">
<div class="col-md-12">
<input type="submit" name="proceed" class="btn" value="Proceed" />
</div>
</div>
</t:form>
</html>

这是 eclipse 中的示例输出

[INFO] pages.Names birthDay: bb
[INFO] pages.Names birthMonth: aa
[INFO] pages.Names birthYear: 2nineteen
[INFO] pages.Names aDate [Year]: 2018
[INFO] pages.Names aDate [Month]: 02
[INFO] pages.Names aDate [Day]: 18
[INFO] pages.Names monthAndYear: 2018-02
[INFO] pages.Names actualDateField: Fri Feb 16 00:00:00 EST 2018

如您所见,Names.tml 中的每个字段都映射到 Names.java POJO 中的相应字段。

关于java - 如何从同名的 Tapestry 文本字段中获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48859225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com