JSF - 画面遷移Advertisement単純な画面の遷移
画面遷移の情報は、faces-config.xmlにnavigation-ruleを記述する。単一の画面遷移はもちろんだが、条件によって異なる遷移先を記述することもできる。
例(faces-config.xml)
<navigation-rule>
<from-view-id>/pages/flow/index.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/pages/flow/result.jsp</to-view-id>
</navigation-case>
</navigation-rule>
from-view-idには遷移元の画面を記述する。navigation-case内に、画面遷移時に渡されるパラメータの値(from-outcome)と、それに対応する遷移先の画面(to-view-id)を記述する。 次に、遷移元の画面にfrom-outcomeで指定したパラメータを記述する。 <h:commandButton action="success" value="送信"/>これにより描画されるボタンを押下したときに、actionの値とfaces-config.xmlの設定を対応付けし、画面遷移が行われる。 サンプルコード階層![]() 図1:階層 BackingBean(Flow.java)
package backingbean.flow;
public class Flow {
private String name = null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
遷移元JSP(index.jsp)
<%@ page contentType="text/html;charset=Shift_JIS" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<html>
<head>
<title>Flow</title>
</head>
<body>
<f:view>
<h:form>
名前を入力して下さい:<h:inputText id="string" value="#{FlowBean.name}"/>
<h:commandButton action="success" value="送信"/>
</h:form>
</f:view>
</body>
</html>
遷移先JSP(result.jsp)
<%@ page contentType="text/html;charset=Shift_JIS" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<html>
<head>
<title>Result</title>
</head>
<body>
<f:view>
<h:form>
ようこそ、<h:outputText id="output" value="#{FlowBean.name}"/>!
</h:form>
</f:view>
</body>
</html>
faces-config.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
"http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
<faces-config>
<navigation-rule>
<from-view-id>/pages/flow/index.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/pages/flow/result.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<managed-bean>
<managed-bean-name>FlowBean</managed-bean-name>
<managed-bean-class>backingbean.flow.Flow</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
</faces-config>
web.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>
<taglib>
<taglib-uri>/tld/jsf-html</taglib-uri>
<taglib-location>/WEB-INF/tld/html_basic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tld/jsf-core</taglib-uri>
<taglib-location>/WEB-INF/tld/jsf_core.tld</taglib-location>
</taglib>
</web-app>
条件による複数の画面遷移
以下の手順で実行。
|