gpt4 book ai didi

jsf-2 - JSF - JSF 实现如何识别操作?

转载 作者:行者123 更新时间:2023-12-02 01:55:03 29 4
gpt4 key购买 nike

我试图了解 JSF 实现如何识别用户的各种可能操作。在我放在一起的简单应用程序中,我在 login.xhtml 页面中配置了以下字段。

用户名 - 输入字段

密码 - 密码字段

登录按钮

取消按钮

登录.xhtml

<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">

<h:head id="head_id" ></h:head>
<h:body id="body_id">
<h:form id="loginForm_id">
<h:panelGrid id="loginPanelGrid_id">
<h:outputText id="nameLabel_id" value="Name"></h:outputText>
<h:inputText id="nameInput_id" value="#{loginBean.name}"></h:inputText>
<h:outputText id="passwordLabel_id" value="Password"></h:outputText>
<h:inputSecret id="passwordInput_id" value="#{loginBean.password}"></h:inputSecret>
</h:panelGrid>
<h:commandButton id="loginBtn_id" value="Do Login" action="#{loginBean.login}"></h:commandButton>
<h:commandButton id="CancelBtn_id" value="Cancel Login" action="#{loginBean.cancel}"></h:commandButton>
</h:form>
</h:body>
</html>

LoginBean.java

package com.ila;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped

public class LoginBean {
private String name;
private String password;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

public String login() {
if ("a".equals(name)) return "success";
else return "failure";
}

public String cancel() {
return "cancel";
}
}

从 login.xhtml 生成的 html 代码

<!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">
<head></head>
<body>
<form id="loginForm_id" name="loginForm_id" method="post"
action="/JSF2TestProject/faces/simple.xhtml"
enctype="application/x-www-form-urlencoded">

<input type="hidden" name="loginForm_id" value="loginForm_id" />

<table id="loginForm_id:loginPanelGrid_id">
<tbody>
<tr>
<td><span id="loginForm_id:nameLabel_id">Name</span></td>
</tr>
<tr>
<td><input id="loginForm_id:nameInput_id" type="text"
name="loginForm_id:nameInput_id" value="a" /></td>
</tr>
<tr>
<td><span id="loginForm_id:passwordLabel_id">Password</span></td>
</tr>
<tr>
<td><input id="loginForm_id:passwordInput_id" type="password"
name="loginForm_id:passwordInput_id" value="" /></td>
</tr>
</tbody>
</table>
<input id="loginForm_id:loginBtn_id" type="submit"
name="loginForm_id:loginBtn_id" value="Do Login" />

<input id="loginForm_id:CancelBtn_id" type="submit"
name="loginForm_id:CancelBtn_id" value="Cancel Login" />

<input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState"
value="H4sIAAAAAAAAAI1QTUoDMRj9Ou1QW2ztDwouBBduVJgD6MIiWCwqFUQRXWicie2UTBKTzHS6KR7BAwhewEuIawUXbryDHsCVSR2nuij4Qd6XfCHvvbz7d7C5FFDtoQg5ofKJs41kdw9xO//28Dh3/pIFqwlFwpDXRK5iogUF1RVYdhnxYr7RAFPT/SmNFb0sBQuEdXzaZCI48701jqTsM+G1KA+VHoQC6qe7IzmCaMdpX/Swq9Zvno/vKnKZWAAx1zy58AqGkDWM6c7mutJTfihg1fDEziVysXRcFnBGMVXOYasdKi23tC8Yx0INdvBAQlI1rSCgPHawRcPg9yVXYEeIhDgSkIuY78G4Yq5VVyapjv44UdQYL3xz+16a+cjCJmMEI/q0KK5fbz8/LMicJB5inhmah2UFJcJcRI7M9AAraaZV4Nz0Wa5Nz//JnaIAp5n/ZPaPVM2maKBkYMZAzUA90Ula/AXEIFMaOgIAAA=="
autocomplete="off" />
</form>
</body>
</html>

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_1.xsd"
version="2.1">
<navigation-rule>
<display-name>login.xhtml</display-name>
<from-view-id>/login.xhtml</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/welcome.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<display-name>login.xhtml</display-name>
<from-view-id>/login.xhtml</from-view-id>
<navigation-case>
<from-outcome>failure</from-outcome>
<to-view-id>/login.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<display-name>login.xhtml</display-name>
<from-view-id>/login.xhtml</from-view-id>
<navigation-case>
<from-outcome>cancel</from-outcome>
<to-view-id>/login.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>

呈现的 UI(屏幕截图)

enter image description here

问题一

鉴于生成的 html 没有发送任何参数来标识单击了哪个按钮(至少我看不到),JSF 实现如何确定是单击了“登录”按钮还是单击了“取消登录”按钮被点击了吗?

问题二

两个隐藏(由 JSF 实现生成)字段的用途是什么(如下所示)?这些与问题1有关吗?

<input type="hidden" name="loginForm_id" value="loginForm_id" />
<input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState"
value="H4sIAAAAAAAAAI1QTUoDMRj9Ou1QW2ztDwouBBduVJgD6MIiWCwqFUQRXWicie2UTBKTzHS6KR7BAwhewEuIawUXbryDHsCVSR2nuij4Qd6XfCHvvbz7d7C5FFDtoQg5ofKJs41kdw9xO//28Dh3/pIFqwlFwpDXRK5iogUF1RVYdhnxYr7RAFPT/SmNFb0sBQuEdXzaZCI48701jqTsM+G1KA+VHoQC6qe7IzmCaMdpX/Swq9Zvno/vKnKZWAAx1zy58AqGkDWM6c7mutJTfihg1fDEziVysXRcFnBGMVXOYasdKi23tC8Yx0INdvBAQlI1rSCgPHawRcPg9yVXYEeIhDgSkIuY78G4Yq5VVyapjv44UdQYL3xz+16a+cjCJmMEI/q0KK5fbz8/LMicJB5inhmah2UFJcJcRI7M9AAraaZV4Nz0Wa5Nz//JnaIAp5n/ZPaPVM2maKBkYMZAzUA90Ula/AXEIFMaOgIAAA=="
autocomplete="off" />

最佳答案

操作由控件的名称标识,JSF 在内部将这些映射到 bean 中的实际方法。

ViewState 是表单在呈现之前的状态,这样可以很容易地将先前的表单状态与当前的表单状态进行比较(以便触发更改事件等)。

关于jsf-2 - JSF - JSF 实现如何识别操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20721536/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com