我有以下JSF 2.2 bean:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import com.tss.model.Revision;
import com.tss.services.RevisionService;
@ManagedBean
@RequestScoped
public class RevisionBean {
private Revision revision;
private RevisionService revisionService;
public RevisionBean() {
revision = new Revision();
revisionService = new RevisionService();
}
public Revision getRevision() {
return revision;
}
public void setRevision(Revision revision) {
this.revision = revision;
}
public void submit() {
revisionService.submitRevision(revision);
}
}
以及以下类:
public class Revision {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class RevisionService {
public void submitRevision(Revision revision) {
System.out.println("done=revision");
}
}
如果名称字段无效(空),则在提交时清除代码的值...
低于结果(值1丢失):
- >我不需要使用ajax,而且我确信我丢失了一些基本细节,因为这是一个非常常见的用例(用户在输入时无法放弃以前的有效输入很多信息)< -
在JSF页面下面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<h:body>
<h:form id="revisionForm">
<h1>JSF Validation Test</h1>
<h:panelGrid columns="2">
<h:outputLabel value="Code: " />
<h:inputText id="inputCode" required="true"
value="#{revisionBean.revision.id}"
requiredMessage="Id is required" />
<h:outputLabel value="Name: " />
<h:inputText id="inputname"
value="#{revisionBean.revision.name}"
requiredMessage="Name is required" required="true" />
</h:panelGrid>
<h:commandButton id="btnSubmit" value="Insert"
action="#{revisionBean.submit}" />
<h:messages></h:messages>
</h:form>
</h:body>
</html>
有人能指出我如何确保验证而不会丢失其他字段的有效值?
谢谢!
答案 0 :(得分:0)
我在maven文件(pom.xml)中使用了错误的引用
<!-- jsf-api -->
<dependency>
<groupId>javax.faces</groupId>
<artifactId>javax.faces-api</artifactId>
<version>2.2</version>
</dependency>
<!-- jsf-impl -->
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.2</version>
</dependency>
更改为正确的引用后,问题就消失了:
<!-- jsf-api -->
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.16</version>
</dependency>
<!-- jsf-impl -->
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.16</version>
</dependency>