我有一个jsf页面,它有两个部分:一个显示记录列表的表和一个添加新记录的对话框。我添加了一些验证功能,但无法使它们正常工作。我需要它:
1) 首次出现对话框时未出现验证错误
2) 如果发生任何验证错误,请将其打开并显示错误消息。
3) 如果没有验证错误并且后端执行成功,请更新表。
这就是我所拥有的:
<h:body>
<h:form id="form01">
<p:dataTable id="tbl1" value="#{welcomeController.teams}" var="team" >
<p:column headerText="Id">
<h:outputText value="#{team.seq}" />
</p:column>
<p:column headerText="Name">
<h:outputText value="#{team.name}" />
</p:column>
</p:dataTable>
<p:dialog id="teamDialog" closable="false" visible="#{welcomeController.addMode}"
widgetVar="teamDialog_w" modal="true" resizable="false" draggable="true"
header="New Team Detail">
<p:messages />
<p:panelGrid columns="2">
<h:outputText value="Name" />
<p:inputText id="name" value="#{welcomeController.newTeam.name}" />
</p:panelGrid>
<p:commandButton value="Submit" ajax="true" actionListener="#{welcomeController.addNewTeam}"
update=":form01"oncomplete="teamDialog_w.hide(); console.log(args);" />
</p:dialog>
<p:commandButton value="ADD" actionListener="#{welcomeController.startAdd}"
oncomplete="teamDialog_w.show(); console.log(args);" update="teamDialog" />
</h:form>
豆子:
@Named
@ConversationScoped
public class WelcomeController implements Serializable {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private static final long serialVersionUID = 1L;
private List<TeamDto> teams;
@Inject SessionManager sessionMan;
@Inject DatabaseUtil dbCache;
@Inject TeamService teamService;
@Inject Conversation conversation;
private TeamDto newTeam = new TeamDto();
private boolean addMode = false;
public List<TeamDto> getTeams() throws IOException {
if (teams == null || teams.size() == 0) {
teams = teamService.getAll();
}
return teams;
}
public void setTeams(List<TeamDto> teams) {
this.teams = teams;
}
public void reload() {
conversationBegin();
}
public void conversationBegin() {
if (conversation.isTransient()) {
conversation.begin();
}
}
public void conversationEnd() {
if(!conversation.isTransient()){
conversation.end();
}
}
public void startAdd() {
reload();
newTeam = new TeamDto();
addMode = true;
}
public TeamDto getNewTeam() {
return newTeam;
}
public void setNewTeam(TeamDto newTeam) {
this.newTeam = newTeam;
}
public void addNewTeam() throws IOException, ValidatorException {
if (newTeam.getName().isEmpty()) {
sessionMan.addGlobalMessageFatal("INVALID INFO", null);
return;
}
teamService.addTeam(newTeam);
teams.add(newTeam);
newTeam = new TeamDto();
addMode = false;
}
public boolean isAddMode() {
return addMode;
}
public void setAddMode(boolean addMode) {
this.addMode = addMode;
}
}
我有两个问题:
1) 在我提交一个空字符串后,我希望对话框仍然打开(因为addMode为true),但事实并非如此。为什么?
2) 如果我像这样按下"添加"按钮:
<p:commandButton value="ADD" actionListener="#{welcomeController.startAdd}" oncomplete="teamDialog_w.show(); console.log(args);" >
<f:ajax render="teamDialog" />
</p:commandButton>
至少当我再次打开对话框时,我可以看到错误消息。但在我的代码中,我看不到错误消息。为什么会这样?它们不是对等的吗?
我的理解有什么问题吗?
请帮忙。非常感谢。
对话框应该在JSF中以自己的形式运行,这一点至关重要。
为了防止验证时对话框关闭,您可以在Primefaces:中使用
if (args && !args.validationFailed){PF('eventDialog').hide();}
像这样:
<p:commandButton value="ADD"
actionListener="#{welcomeController.startAdd()}"
oncomplete="if (args && !args.validationFailed){PF('teamDialog').hide();}"
update="your_dialog_formID:messages, other_updated_IDs" />
这里if (args && !args.validationFailed){PF('teamDialog').hide();}
您可以明显地添加else子句并执行#3"如果没有验证错误并且后端成功执行,则更新表。"
这里,BalusC给出了很好的解释:当提交后出现验证错误时,保持p:对话框打开