我有一个包含少量输入字段和一个选择菜单的表单 . 所有字段都是必需的=“true”,firstName,lastName,emailAddress,密码,国家/地区 .
测试用例#1:1)在所有输入字段中输入值,保留firstName字段而不输入任何值,
选择一个国家/地区(例如:美国)并提交表单,我们可以看到firstName字段所需的错误消息 .
2)保持表单不变,保留firstName字段而不输入任何值,然后选择country to“select one”,并提交表单 . 我们可以看到firstName和country字段的错误消息,但是在下拉列表中它没有显示“Select One”,它显示美国(之前的选择) .
我对输入字段有同样的问题,但我用转换器解决了 . 有没有办法解决这个问题 . 我在stackoverflow中发布了一些帖子,后来才知道这是mojarra中的一个bug .
这是我的代码......
userRegistration.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core" >
<h:body>
<h:form id="registrationForm">
<h:messages layout="table" infoClass="infoMsgs" errorClass="errMsgs"/>
<table>
<tr>
<td>First Name:</td>
<td>
<h:inputText size="40" maxlength="100"
required="true"
styleClass="#{component.valid ? '' : 'text_error'}"
value="#{registrationAction.firstName}" />
</td>
</tr>
<tr>
<td>Last Name:</td>
<td>
<h:inputText size="40" maxlength="100"
required="true"
styleClass="#{component.valid ? '' : 'text_error'}"
value="#{registrationAction.lastName}" />
</td>
</tr>
<tr>
<td>Email Address:</td>
<td>
<h:inputText size="40" maxlength="100"
required="true"
styleClass="#{component.valid ? '' : 'text_error'}"
value="#{registrationAction.emailAddress}" />
</td>
</tr>
<tr>
<td>Password:</td>
<td>
<h:inputSecret size="20" maxlength="15"
required="true"
styleClass="#{component.valid ? '' : 'text_error'}"
value="#{registrationAction.password}" />
</td>
</tr>
<tr>
<td>Country:</td>
<td>
<h:selectOneMenu id="yourCountry" name="yourCountry"
value="#{shortRegistrationAction.shortRegistrationForm.selectedCountry}"
required="true"
styleClass="#{component.valid ? '' : 'text_error'}">
<f:selectItem itemValue="#{null}" itemLabel="-Select One-" />
<f:selectItems value="#{registrationAction.countryList}" />
</h:selectOneMenu>
</td>
</tr>
<tr>
<td> </td>
<td>
<h:commandButton name="RegisterButton" id="RegisterButton"
styleClass="submitbutton"
value="Register"
action="#{registrationAction.registerUser}" />
</td>
</table>
</h:form>
</h:body>
</html>
RegistrationAction.java
package com.ebiz.web.bean;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import com.ebiz.service.ICountryService;
import com.ebiz.service.IUserService;
import com.ebiz.vo.UserVO;
import org.apache.log4j.Logger;
@ManagedBean(name = "registrationAction")
@ViewScoped
public class RegistrationAction implements Serializable {
private static final long serialVersionUID = 1L;
private static final Logger log = Logger.getLogger(RegistrationAction.class);
@ManagedProperty("#{countryService}")
private ICountryService countryService;
@ManagedProperty("#{userService}")
private IUserService userService;
private String firstName;
private String lastName;
private String emailAddress;
private String password;
private String selectedCountry;
private List<String> countryList;
@PostConstruct
public void init(){
countryList = countryService.getCountryList();
}
public void registerUser() {
UserVO userVO = new UserVO();
userVO.setFirstName(firstName);
userVO.setLastName(lastName);
userVO.setEmailAddress(emailAddress);
userVO.setPassword(password);
userVO.setCountry(selectedCountry);
userService.registerUser(userVO);
}
}
InputTextTrimmer.java
package com.ebiz.web.converter;
import javax.faces.component.EditableValueHolder;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
@FacesConverter(forClass=String.class)
public class InputTextTrimmer implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
//return value != null ? value.trim() : null;
if (value == null || value.trim().isEmpty()) {
if (component instanceof EditableValueHolder) {
((EditableValueHolder) component).setSubmittedValue(null);
((EditableValueHolder) component).resetValue();
}
return null;
}
return value;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
return (value == null) ? null : value.toString();
}
}
2 回答
经过长时间的研究,我没有找到任何方法在JSF中实现它 . 所以我使用jQuery来处理它的客户端,可能这个答案不是最好的解决方案,但至少对需要解决方案的人有用 .
此处下拉值将显示为旧值,但仍显示下拉列表的验证错误 . 在上面的xhtml页面中,如果你查看下拉菜单部分,
即使它显示验证失败案例的旧值,仍然styleClass变为text_error . 在关闭
<h:body>
标记后,我在xhtml页面中添加了以下代码 . ie(</h:body>
)它仅适用于此特定xhtml页面中的所有下拉菜单,但是我们可以将它放在commonTemplate.xhtml这样的公共位置,以便在所有页面上执行它 .看看以下 BalusC 博文Reset non-processed input components on ajax update
您可以将OmniFaces添加到您的项目并使用其ResetInputAjaxActionListener
因此,您必须将代码更新为此(您最好也使用
f:ajax
):另外,看看这个 BalusC 回答How can I populate a text field using primefaces ajax after validation errors occur?