JSF教程 – JSF隐式导航示例
使用JSF,我们可以在配置文件faces-config.xml中定义页面导航规则。
我们还可以将导航规则放在托管bean中。
以下代码显示第三个选项,即隐式导航。
JSF 2.0提供了隐式导航,哪些页面可以自动解析。我们只需要在action属性中放置视图名称,JSF将在部署的应用程序中自动搜索正确的视图页面。
例如,在以下的JSF表单标签中。我们在action属性中设置视图名称。
这里当点击Page2按钮时,JSF将解析视图名称,page2解析为page2.xhtml扩展名,并在当前目录中找到相应的视图文件page2.xhtml。
<h:form> <h:commandButton action="page2" value="Page2" /> </h:form>
例子
下面的代码来自UserBean.java。
package cn.w3cschool.common;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name="user")
@SessionScoped
public class UserBean implements Serializable{
private static final long serialVersionUID = 1L;
public String moveToPage1(){
return "demo";
}
public String moveToPage2(){
return "page2";
}
}
以下代码来自demo.xhtml。
<?xml version="1.0" encoding="UTF-8"?>
<!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">
<h:body>
<h2>This is page1.xhtml</h2>
<h:form>
<h:commandButton action="page2" value="Move to page2.xhtml" />
<h:commandButton action="#{user.moveToPage2}"
value="Move to page2.xhtml by managed bean" />
</h:form>
</h:body>
</html>
以下代码来自page2.xhtml。
<?xml version="1.0" encoding="UTF-8"?>
<!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">
<h:body>
<h2>This is page2.xhtml</h2>
<h:form>
<h:commandButton action="demo" value="Move to demo.xhtml" />
<h:commandButton action="#{user.moveToPage1}"
value="Move to demo.xhtml by managed bean" />
</h:form>
</h:body>
</html>
下载 Implicit-Navigation.zip
运行
将生成的WAR文件从目标文件夹复制到Tomcat部署文件夹,并运行Tomcat-Install-folder/bin/startup.bat。
Tomcat完成启动后,在浏览器地址栏中键入以下URL。
http://localhost:8080/simple-webapp/demo.xhtml

国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码













