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

时间:2020-02-23 14:33:42  来源:igfitidea点击:

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

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

创建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>

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

创建从bean的add方法调用并显示的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>

创建托管bean" Mobile.java"为;

package com.theitroad.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";
	}

}

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

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

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

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

创建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>

创建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>

创建托管beanMobileBean.java

package com.theitroad.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";
	}

}

其中我们从add方法返回字符串" for"。

现在让我们创建" 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>

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

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