jsf 导航

Navigations can be handled in JSF by writing methods in the managed bean. These methods should be public, take no parameters and should returns an object or a view name. The method is invoked in the action attribute of the JSF page.

可以通过在托管Bean中编写方法来在JSF中处理导航。 这些方法应该是公共的,不带任何参数,并且应该返回一个对象或视图名称。 在JSF页面的action属性中调用该方法。

Let’s understand this concept more clearly with an example.

让我们通过一个例子更清楚地理解这个概念。

Create addmob.xhtml as

创建addmob.xhtml

addmob.xhtml

addmob.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml"xmlns:h="https://java.sun.com/jsf/html">
<h:head>
</h:head>
<h:body><h3>Add Mobile Details</h3><h:form><h:panelGrid columns="3"><h:outputLabel for="mname">Mobile Name:</h:outputLabel><h:inputText value="#{mobile.mname}"></h:inputText><br /><br /><h:outputLabel for="color">Color:</h:outputLabel><h:inputText value="#{mobile.color}"></h:inputText><br /><br /><h:outputLabel for="model">Model Number:</h:outputLabel><h:inputText value="#{mobile.modelno}"></h:inputText><br /><br /><h:commandButton value="Submit" action="#{mobile.add()}"></h:commandButton></h:panelGrid></h:form></h:body>
</html>

Here we are invoking the add method of the mobile managed bean in the action attribute to render the page on click of submit.

在这里,我们在action属性中调用移动托管bean的add方法,以在单击Submit时呈现页面。

Create viewmob.xhtml that is called from the add method of the bean and displayed.

创建从bean的add方法调用并显示的viewmob.xhtml

viewmob.xhtml

viewmob.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml"xmlns:h="https://java.sun.com/jsf/html">
<h:head><title>Mobile Details</title>
</h:head>
<h:body>Mobile Name:#{mobile.mname}<br /><br />Mobile color:#{mobile.color}<br /><br />Model Number:#{mobile.modelno}<br /><br /></h:body>
</html>

Create the managed bean Mobile.java as;

创建托管bean Mobile.java为;

package com.journaldev.jsf.beans;import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;@ManagedBean
@SessionScoped
public class Mobile implements Serializable {private static final long serialVersionUID = 6544437175802702885L;private String mname;private String modelno;private String color;public Mobile() {}public Mobile(String mname, String modelno, String color) {this.mname = mname;this.modelno = modelno;this.color = color;}public String getMname() {return mname;}public void setMname(String mname) {this.mname = mname;}public String getModelno() {return modelno;}public void setModelno(String modelno) {this.modelno = modelno;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public String add() {return "viewmob";}}

Note that we are returning the viewmob page in the add method which displays the details of the mobile entered by the user upon click of submit.

请注意,我们将在add方法中返回viewmob页面,该页面显示用户在单击“提交”后输入的手机的详细信息。

Now run the application and you should see below response pages.

现在运行该应用程序,您应该看到下面的响应页面。

On clicking submit button in above page, you should get below output.

单击上一页中的提交按钮,您应该获得以下输出。

Another way of handling navigation through a method is by specifying a string outcome in the method and map the returned string to a JSF page. This is done by making an entry in faces-config.xml file.

处理方法导航的另一种方法是在方法中指定字符串结果,并将返回的字符串映射到JSF页面。 这是通过在faces-config.xml文件中进行输入来完成的。

Create addmobstring.xhtml as;

创建addmobstring.xhtml为;

addmobstring.xhtml

addmobstring.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml"xmlns:h="https://java.sun.com/jsf/html">
<h:head>
</h:head>
<h:body><h3>Add Mobile Details</h3><h:form><h:panelGrid columns="3"><h:outputLabel for="mname">Mobile Name:</h:outputLabel><h:inputText value="#{mobileBean.mname}"></h:inputText><br /><br /><h:outputLabel for="color">Color:</h:outputLabel><h:inputText value="#{mobileBean.color}"></h:inputText><br /><br /><h:outputLabel for="model">Model Number:</h:outputLabel><h:inputText value="#{mobileBean.modelno}"></h:inputText><br /><br /><h:commandButton value="Submit" action="#{mobileBean.add}"></h:commandButton></h:panelGrid></h:form></h:body>
</html>

Create viewmobstring.xhtml as

创建viewmobstring.xhtml

viewmobstring.xhtml

viewmobstring.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml"xmlns:h="https://java.sun.com/jsf/html">
<h:head><title>Mobile Details</title>
</h:head>
<h:body>Mobile Name:#{mobileBean.mname}<br /><br />Mobile color:#{mobileBean.color}<br /><br />Model Number:#{mobileBean.modelno}<br /><br /></h:body>
</html>

Create the managed bean MobileBean.java as;

创建托管bean MobileBean.java为;

package com.journaldev.jsf.beans;import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;@ManagedBean
@SessionScoped
public class MobileBean {private String mname;private String modelno;private String color;public MobileBean() {}public MobileBean(String mname, String modelno, String color) {this.mname = mname;this.modelno = modelno;this.color = color;}public String getMname() {return mname;}public void setMname(String mname) {this.mname = mname;}public String getModelno() {return modelno;}public void setModelno(String modelno) {this.modelno = modelno;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public String add() {return "for";}}

Here we are returning the string “for” from the add method.

在这里,我们从add方法返回字符串“ for”。

Now lets create the faces-config.xml as;

现在让我们将faces-config.xml创建为:

<?xml version='1.0' encoding='UTF-8'?>
<faces-config xmlns="https://java.sun.com/xml/ns/javaee"xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="https://java.sun.com/xml/ns/javaeehttps://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"version="2.0"><navigation-rule><from-view-id>addmobstring.xhtml</from-view-id><navigation-case><from-action>#{mobileBean.add}</from-action><from-outcome>for</from-outcome><to-view-id>/viewmobstring.xhtml</to-view-id></navigation-case></navigation-rule>
</faces-config>

If we run the application , we get the expected behavior as shown in below images.

如果运行该应用程序,则将获得预期的行为,如下图所示。

Upon clicking submit button, you should see below response page.

单击提交按钮后,您将看到下面的响应页面。

Finally, below image shows the project structure in Eclipse.

最后,下图显示了Eclipse中的项目结构。

Please download the project zip from below link and play around with it to learn more.

请从下面的链接下载项目zip并进行试用以了解更多信息。

Download JSF Action Method Navigation Example Project下载JSF动作方法导航示例项目

翻译自: https://www.journaldev.com/7051/jsf-action-method-navigation-example-tutorial-from-action-tag

jsf 导航

jsf 导航_JSF动作方法导航示例教程– from-action标记相关推荐

  1. jsf集成spring_JSF Spring Hibernate集成示例教程

    jsf集成spring Welcome to JSF Spring Hibernate Integration example tutorial. In our last tutorial, we s ...

  2. java jsf 入门_JSF入门、简单示例

    JSF入门 1. 什么是 Java Server Faces(jsf)?   JSF为JAVA的 Web应用用户界面的开发人员提供了标准的编程接口.丰富可扩展的UI组件库(一个核心的JSP标记库用来处 ...

  3. jsf tree组件_JSF表单组件示例教程

    jsf tree组件 JSF Form component is a collection of fields along with the data and submit functionality ...

  4. jsf 导航_JSF页面导航示例教程

    jsf 导航 Page navigation is the redirection of a page based on the events performed for instance – on ...

  5. android 导航抽屉_Android导航抽屉示例教程

    android 导航抽屉 In this tutorial we'll implement a Navigation Drawer in our android application. Androi ...

  6. jsf表单验证_JSF验证示例教程–验证器标签,定制验证器

    jsf表单验证 JSF validation model defines a set of standard classes for validating the UI components. The ...

  7. 八月微信小程序导航:官方文档+精品教程+demo集合(8月25...

    2019独角兽企业重金招聘Python工程师标准>>> 1:官方工具: https://mp.weixin.qq.com/debug/w ... tml?t=147643467846 ...

  8. jsf 配置_JSF Tomcat配置示例

    jsf 配置 JavaServer Faces (JSF)是一个Web应用程序框架,旨在简化基于Web的用户界面的开发集成. 它用于开发和构建服务器端用户界面组件,并在Web应用程序中使用它们. JS ...

  9. JSF Spring Hibernate集成示例教程

    JSF Spring Hibernate集成示例教程 欢迎使用JSF Spring Hibernate Integration示例教程.在上一篇教程中,我们了解了如何将JSF和Spring框架集成在一 ...

最新文章

  1. java 字符串写入word,JAVA 将字符串hollow 替换成word肿么实现?
  2. 【深度学习】ReLU激活函数的缺点
  3. 计组-数据通路的功能和基本结构
  4. Unity编译Mono
  5. 大厂高级前端面试题答案
  6. NET问答: 如何让 HttpClient 支持 Http 2.0 协议?
  7. JS内置方法(Array)
  8. docker 守护进程
  9. matplotlib Artist 教程
  10. clickhouse MergeTree系列引擎
  11. Django 1.8.2 文档 1
  12. 关于EasyExcel 优化,实现格式自定义,数据字典自动转化。
  13. dmx512协议的编程c语言,我在此分享一份DMX512协议的发送程序,希望对做灯光控制的人有一定的帮助(我测试过了跟DMX512控制台发出的方波是一样一样的)...
  14. RF无线射频电路设计难点分析
  15. Android中TextView中文字体粗体的设置方法
  16. nginx网页支持WebP图片
  17. Android 桌面小组件 AppWidgetProvider
  18. 如何避免高不成低不就? 疫情当下Java学习路线分享
  19. activiti学习(二十一)——流程虚拟机源码分析(三)——从进入到离开userTask
  20. 计算机专业本科考教资可以考哪些,高中教师资格证计算机专业考什么内容

热门文章

  1. 云服务器上安装Anaconda3 (亲测有效)
  2. [转载] Python简介、linux上Python及其IDE的安装和详细配置
  3. [转载] python 卷积_40 行 Python 代码,实现卷积特征可视化
  4. [转载] Python大数据文本分析及应用
  5. [转载] 在java中,如何将方法作为参数传递
  6. 如何实现一个Servlet中的多个功能
  7. JFinal Web开发学习(一)开启HelloWorld
  8. 实用机器人设计(二)-传感器
  9. PHP 利用curl 模拟get post 请求
  10. 【转载】深入分析 ThreadLocal 内存泄漏问题