【发布时间】:2010-03-14 12:22:10
【问题描述】:
我在简单的 jsf 系统中遇到导航问题。
我有 MainBean 有两种方法:public String register() 和 public String login().
我已经和faces-config.xml 玩了几个小时,我觉得我错过了一些非常重要的东西,因为我认为到目前为止我已经尝试了所有简单的解决方案:)。
我已将 MySQL 连接器 (jdbc) 添加到 Tomcat 的 lib 文件夹,并且可以将表注册到 MySQL 数据库。它甚至允许我的用户登录到该页面。
唯一的问题是我不能在login.xhtml 以外的任何其他页面中使用导航。似乎导航只在这个上有效。我尝试使用<from-view-id>*,但并不快乐。我相信有一个简单的解决方案,很快就会有人提出正确的解决方案。让我们跳过所有 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>
login_failed.xhtml 的最后一个导航规则根本不起作用。
这里是login.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"
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="Username2:" />
<h:inputText id="username" value="#{mainBean.username}" />
<h:outputText value="Password:" />
<h:inputSecret id="password" value="#{mainBean.password}" />
</h:panelGrid>
<h:commandButton value="Log in" action="#{mainBean.login}" />
<h:commandButton value="Sign up" action="sign" />
</h:form>
</ui:define>
</ui:composition>
</body>
</html>
login_failed.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"
xmlns:f="http://java.sun.com/jsf/core">
<body>
<ui:composition template="/template.xhtml">
<ui:define name="body">
<f:view>
<h:outputText value="Unknown username or password." />
<h:commandButton value="Sign up" action="sign" />
<h:commandButton value="Log in" action="back" />
</f:view>
</ui:define>
</ui:composition>
</body>
</html>
在这里我尝试了很多选择。我使用了action="#{mainBean.register})" 方法返回“sign”字符串,但这些都不起作用。还有一个文件(未在faces-config.xml 文件中指定,因为也没有工作 - 但通过按钮从login.xhtml 开始工作正常。我首先尝试从login_failed.xhtml 管理导航,然后我将应用相同的规则当客户注册他的昵称时,注册返回登录页面。
这里是register.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"
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>
基本上mainBean.register 现在调用数据库并返回字符串,但显然它不会导航到任何视图(但会提供数据库条目)。
我相信对于大多数有经验的 Web 开发人员来说,这是一个简单的解决方法,我们将不胜感激。
如果有帮助,我会使用 eclipse、tomcat 6 和 Widows Vista :)
提前谢谢你。 最诚挚的问候。
【问题讨论】:
标签: jsf navigation facelets