1
当我尝试提交表单时,输入值出现在验证方法中,但在提交方法中不存在。为什么输入属性在程序到达提交函数时为空?提交时JSF表单值消失
输入:ASDF
genres.xhtml
<h:form>
<h:inputText
id="userGenre"
value="#{genres.input}"
validator="#{genres.validateLength}"
required="true" size="3"
/>
<h:commandButton
value="Add genre"
action="#{genres.submit}"
/>
<h:message for="userGenre" />
</h:form>
Genres.java
@Named
@SessionScoped
public class Genres {
static final Logger LOG = LoggerFactory.getLogger(Genres.class);
private String input = "";
public void validateLength(FacesContext context, UIComponent toValidate, Object value) {
LOG.info("Validating");
input = (String) value;
LOG.info("name:"+input); // result: "name:asdf"
int min = 3;
int max = 15;
int len = input.length();
if (len < min || len > max) {
((UIInput) toValidate).setValid(false);
FacesMessage message = new FacesMessage("Must be at least 3 and at most 15 characters.");
context.addMessage(toValidate.getClientId(context), message);
return;
}
}
public void submit() {
LOG.info("Submitting");
LOG.info("name:"+input); // result: "name:"
// process input
}
public String getInput() {
return input;
}
public void setInput(String input) {
this.input = input;
}
}
个 faces-config.xml中
<managed-bean>
<managed-bean-name>genres</managed-bean-name>
<managed-bean-class>no.krystah.Genres</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>input</property-name>
<property-class>java.lang.String</property-class>
<value/>
</managed-property>
</managed-bean>
这将是有道理的,因为无论我初始化“输入”字段(在此代码中,我使用“”)将在提交时发送。但是,添加@ManagedBean注释确实没有帮助。 – krystah 2014-12-04 00:57:38
我回过头来。单独添加ManagedBean不起作用,但是如果我也删除了@Named,它就起作用了。谢谢。 :) – krystah 2014-12-04 01:05:39
不客气,很高兴看到你的问题已经解决 – jfun 2014-12-04 01:07:45