JSF导航问题Facelets和Bean

JSF导航问题Facelets和Bean,jsf,navigation,facelets,Jsf,Navigation,Facelets,我的简单jsf系统中的导航有问题 我有MainBean,它有两种方法:publicstringregister()和publicstringlogin()。 我已经玩了几个小时的faces config.xml,我觉得我错过了一些非常重要的东西,因为我觉得我已经尝试了所有简单的解决方案:) 我已经将MySQL连接器(jdbc)添加到Tomcat的lib文件夹中,并且能够将该表注册到MySQL数据库中。它甚至允许我的用户登录到该页面 唯一的问题是除了login.xhtml之外,我不能在任何其他页面

我的简单jsf系统中的导航有问题

我有
MainBean
,它有两种方法:
publicstringregister()
publicstringlogin()。

我已经玩了几个小时的
faces config.xml
,我觉得我错过了一些非常重要的东西,因为我觉得我已经尝试了所有简单的解决方案:)

我已经将MySQL连接器(jdbc)添加到Tomcat的lib文件夹中,并且能够将该表注册到MySQL数据库中。它甚至允许我的用户登录到该页面

唯一的问题是除了
login.xhtml
之外,我不能在任何其他页面中使用导航。似乎导航仅在这一个上处于活动状态。我试着使用
*
,但没有乐趣。我相信有一个简单的解决方法,很快就会有人提出正确的解决方案。让我们跳过所有MySQL部分,并尝试修复导航问题

以下是
faces config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
    version="1.2">
  <application>
    <view-handler>com.sun.facelets.FaceletViewHandler</view-handler>    
  </application>
 <managed-bean>
  <managed-bean-name>mainBean</managed-bean-name>
  <managed-bean-class>dk.itu.beans.MainBean</managed-bean-class>
  <managed-bean-scope>session</managed-bean-scope>
 </managed-bean>
 <managed-bean>
  <managed-bean-name>registerBean</managed-bean-name>
  <managed-bean-class>dk.itu.beans.RegisterBean</managed-bean-class>
  <managed-bean-scope>session</managed-bean-scope>
 </managed-bean>
 <navigation-rule>
  <from-view-id>/login.xhtml</from-view-id>
  <navigation-case>
   <from-outcome>success</from-outcome>
   <to-view-id>/welcome.xhtml</to-view-id>
  </navigation-case>
        <navigation-case>
      <from-outcome>failure</from-outcome>
      <to-view-id>/login_failed.xhtml</to-view-id>
    </navigation-case>
        <navigation-case>
      <from-outcome>sign</from-outcome>
      <to-view-id>/register.xhtml</to-view-id>
    </navigation-case>
 </navigation-rule>
  <navigation-rule>
  <from-view-id>/login_failed.xhtml</from-view-id>
  <navigation-case>
  <from-outcome>back</from-outcome>
      <to-view-id>/login.xhtml</to-view-id>
  </navigation-case>
  </navigation-rule>
</faces-config>
<!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"
      xmlns:f="http://java.sun.com/jsf/core">
<body>
<ui:composition template="/template.xhtml">
  <ui:define name="body">
    <h:form id="helloForm">
    <h:panelGrid columns="2">
       <h:outputText value="User Name:"/>
      <h:inputText type="text" id="userName" value=""/>
   <h:outputText value="Password:"/>
      <h:inputSecret type="text" id="userPassword" value=""/>
      <h:outputText value="Member Level:"/>
      <h:selectOneRadio value="">
                              <f:selectItem itemLabel="Platinium" itemValue="Platinum" />
                              <f:selectItem itemLabel="Gold" itemValue="Gold" />
                              <f:selectItem itemLabel="Normal" itemValue="Normal" />
                          </h:selectOneRadio>
      <h:outputText value="Full Name:"/>
      <h:inputText type="text" id="userFull" value=""/>
      <h:outputText value="Address:"/>
      <h:inputTextarea type="text" id="userAddress" value=""/>
      <h:outputText value="Zip Code:"/>
      <h:inputText type="text" id="userZip" value=""/>
      <h:outputText value="City:"/>
      <h:inputText type="text" id="userCity" value=""/>
   <h:commandButton value="Sign Up" action="#{mainBean.register}" />
    </h:panelGrid>
    </h:form>
  </ui:define>  
</ui:composition>
</body>
</html>
在这里,我尝试了很多选择。我使用了
action=“#{mainBean.register})”
方法返回“sign”字符串,但这些方法都不起作用。还有一个文件(未在
faces config.xml
文件中指定,因为也不起作用-但从
login.xhtml
by按钮开始可以正常工作。我尝试从
login\u管理导航失败。xhtml
首先,然后在客户注册其昵称时,我将应用相同的注册规则返回登录页面)

这里是register.xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
    version="1.2">
  <application>
    <view-handler>com.sun.facelets.FaceletViewHandler</view-handler>    
  </application>
 <managed-bean>
  <managed-bean-name>mainBean</managed-bean-name>
  <managed-bean-class>dk.itu.beans.MainBean</managed-bean-class>
  <managed-bean-scope>session</managed-bean-scope>
 </managed-bean>
 <managed-bean>
  <managed-bean-name>registerBean</managed-bean-name>
  <managed-bean-class>dk.itu.beans.RegisterBean</managed-bean-class>
  <managed-bean-scope>session</managed-bean-scope>
 </managed-bean>
 <navigation-rule>
  <from-view-id>/login.xhtml</from-view-id>
  <navigation-case>
   <from-outcome>success</from-outcome>
   <to-view-id>/welcome.xhtml</to-view-id>
  </navigation-case>
        <navigation-case>
      <from-outcome>failure</from-outcome>
      <to-view-id>/login_failed.xhtml</to-view-id>
    </navigation-case>
        <navigation-case>
      <from-outcome>sign</from-outcome>
      <to-view-id>/register.xhtml</to-view-id>
    </navigation-case>
 </navigation-rule>
  <navigation-rule>
  <from-view-id>/login_failed.xhtml</from-view-id>
  <navigation-case>
  <from-outcome>back</from-outcome>
      <to-view-id>/login.xhtml</to-view-id>
  </navigation-case>
  </navigation-rule>
</faces-config>
<!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"
      xmlns:f="http://java.sun.com/jsf/core">
<body>
<ui:composition template="/template.xhtml">
  <ui:define name="body">
    <h:form id="helloForm">
    <h:panelGrid columns="2">
       <h:outputText value="User Name:"/>
      <h:inputText type="text" id="userName" value=""/>
   <h:outputText value="Password:"/>
      <h:inputSecret type="text" id="userPassword" value=""/>
      <h:outputText value="Member Level:"/>
      <h:selectOneRadio value="">
                              <f:selectItem itemLabel="Platinium" itemValue="Platinum" />
                              <f:selectItem itemLabel="Gold" itemValue="Gold" />
                              <f:selectItem itemLabel="Normal" itemValue="Normal" />
                          </h:selectOneRadio>
      <h:outputText value="Full Name:"/>
      <h:inputText type="text" id="userFull" value=""/>
      <h:outputText value="Address:"/>
      <h:inputTextarea type="text" id="userAddress" value=""/>
      <h:outputText value="Zip Code:"/>
      <h:inputText type="text" id="userZip" value=""/>
      <h:outputText value="City:"/>
      <h:inputText type="text" id="userCity" value=""/>
   <h:commandButton value="Sign Up" action="#{mainBean.register}" />
    </h:panelGrid>
    </h:form>
  </ui:define>  
</ui:composition>
</body>
</html>

basicali
mainBean.register
现在调用数据库并返回字符串,但显然它不会导航到任何视图(而是提供一个数据库条目)

我相信对于大多数有经验的web开发人员来说,这是一个简单的解决方案,任何帮助都将不胜感激

我使用eclipse、tomcat 6和Widows Vista(如果有帮助的话):)

先谢谢你。
致以最诚挚的问候。

一般来说,我建议在开始使用模板等高级功能之前,先尝试一些简单的功能。几点建议:

  • 在登录页面的导航规则中使用重定向。这样,你就不需要任何特殊的呼喊就能让后退按钮正常工作
  • 在验证登录之前创建会话bean通常不是一个好主意。一个常见的解决方案是在登录页面上使用一个请求范围的标识bean,然后将其注入一个登录操作,该操作验证凭据并创建用户会话
我有一个相当完整的Facelets/JSF2在线示例,演示了这种行为:

  • Facelets登录和页面
  • JSF
  • 请求作用域bean和验证用户凭据的

您可以随意将复制粘贴设计模式应用到您的代码中

UICommand
组件应该放在
UIForm
元素中。在
login\u中失败。xhtml
您忘记了使用
h:form
。添加它,它就会工作。

这些链接已断开。答案仍然有些有用,但你有没有可能修复这些链接?