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

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/javaee
   https://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.

JSF-Action-Method-Navigation-4

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

Finally, below image shows the project structure in Eclipse.

JSF-Action-Method-Navigation-Project-Example

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

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

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

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

jsf 导航

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值