【问题标题】:User input don't get validated when user navigate between pages用户在页面之间导航时未验证用户输入
【发布时间】:2014-09-23 15:09:57
【问题描述】:

我的问题是,当用户在页面之间导航时,用户输入不会被提交和验证。

当时我正在学习 JSF 并编写应用程序。该应用程序由多个页面组成。我使用默认模板插入导航模板。

这是我的导航模板

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:pt="http://xmlns.jcp.org/jsf/passthrough">


<ui:insert name="navigation">
            <nav>           
                <h:panelGroup>
                <h:form>
                <h:commandButton value="GO BACK" styleClass="button" action="GOBACK"/>
                <h:commandButton value="Reset" pt:type="Reset" styleClass="button" action="RESET"/>
                <h:commandButton value="GO FORWARD" action="GOFORWARD" styleClass="button"/>
                </h:form>
            </h:panelGroup>
            </nav>
    </ui:insert>
</ui:composition>

这是我的 default.xhtml 模板

<!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:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html">
<h:head>
  <title><ui:insert name="title">Page</ui:insert></title>
  <h:outputStylesheet library="css" name="normalize.css"/>
  <h:outputStylesheet library="css" name="atmosphere.css"/>
  <h:outputStylesheet library="css" name="layout.css"/>


</h:head>

<h:body>
<div id="container">
<div id="header">
        <ui:include src="header.xhtml"/>
</div>


<div id="content">
  <ui:insert name="content">
        Content area.  See comments below this line in the source.
  </ui:insert>
</div>
<div id="navigation">
    <ui:insert name="navigation">
        <ui:include src="navigation.xhtml"></ui:include>
    </ui:insert>
</div>
</div>
</h:body>

</html>

在 faces-config 我有一些导航规则,例如:

<navigation-rule>
    <display-name>firstPage.xhtml</display-name>
    <from-view-id>/firstPage.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>GOFORWARD</from-outcome>
        <to-view-id>/secondPage.xhtml</to-view-id>
    </navigation-case>
</navigation-rule>

现在我想制作一个内容模板客户端,其中包含&lt;h:form&gt;。但是现在的问题是,当用户按下其中一个导航按钮时,用户输入没有得到验证和提交(因为导航是另一种形式)。你知道解决办法吗? (或者我在额外模板中的导航走错路了?)

我唯一的想法是在 default.xhtml 模板中的内容和导航部分周围写一个&lt;h:form&gt;

谢谢。

【问题讨论】:

  • 这在技术上与您的问题无关,但您真的不应该使用您的 faces-config 文件来定义导航案例,这些应该在您的个人文件中甚至在您的支持豆。

标签: forms jsf navigation submit


【解决方案1】:

我同意 RevanProdigalKnight 的观点。例如,除了通过 faces-config 导航。您可以通过像以下代码一样放入支持 bean 来导航:

@ManagedBean
@ViewScoped
class Bean{

public String goToNextPage(){
return "nextPage.xhtml";
}

如果您想将信息发送到下一页

   public String goToNextPage(){
    FacesContext context = FacesContext.getCurrentInstance();
     context.getExternalContext().getRequestMap()
            .put("info", yourInformation);
     return "nextPage.xhtml"

然后在xhtml中,你会有

     h:commandButton value="GO FORWARD" action = "#{bean.goToNextPage()}"

现在关于提交和验证,在后台 bean 中,您将获取在字段中输入的信息并进行验证,有很多方法可以进行验证。假设您在第一页中有一个文本字段名称 userField 并且其中一个要求是用户输入内容,那么

首页.xhtml

   h:inputText value = "#{bean.userField}" required = "true"  requiredMessage = "Input something please"

然后在豆子里 创建一个名为 userField 的字段

  private String userField = "";

然后为它制作getter和setter

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    • 2015-10-31
    • 2021-10-09
    • 2017-09-11
    相关资源
    最近更新 更多