提交表单后,使用 java ee 和 jsf 2,2,我的支持 bean 属性仍然为空
After form submission , using java ee and jsf 2,2, my backing bean properties are still null
基本上,在 input.xhtml 中有一个表单接受用户名和密码,如果它们等于特定值(与值无关),程序应在浏览器中打印一条消息,但这不会发生。为了确定问题所在,我添加了 2 行 "System.out.println(...)" 并在其中打印 属性 的值,我发现即使提交后属性仍然为空。因此,在我单击发送后,控制台中会显示 "null null"。感谢您的帮助!
这是 UserBean class(支持 bean)
package bean;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class UserBean implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private String password;
private String output_message;
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 go(){
output_message = "";
if(name == null && password == null){
output_message += "Both fields cannot be empty!";
}else if(name == "name" && password == "pass"){
output_message += "Success!";
}else{
output_message += "Data is wrong!";
}
System.out.println(name);
System.out.println(password);
return output_message;
}
public String getOutput_message() {
return output_message;
}
public void setOutput_message(String output_message) {
this.output_message = output_message;
}
public String ret(String r){
return r;
}
}
这是 input.xhtml 文件,其中包含将数据提交到 bean 的表单。 (忽略 url 到 template.xhtml,它只是一个具有页眉和页脚的父模板,而不是 input.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://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:cc="http://java.sun.com/jsf/composite/myComponent">
<ui:composition template="/WEB-INF/templates/template.xhtml">
<ui:define name="content">
<style type="text/css">
#form1{
position: absolute;
top: 20%;
left:40%;
border: 1px solid black;
background-color: orange;
}
td{
padding-top:10px;
}
input[type="text"]{
backgorund-color:blue;
}
</style>
<form id="form1" method="post">
<table columns="2">
<tr>
<td><span>Emri:</span></td>
<td><input type="text" id="emri" value="#{userBean.name}"/></td>
</tr>
<tr>
<td><span>Password:</span></td>
<td><input type="password" id="pass" value="#{userBean.password}"/></td>
</tr>
<tr>
<td colspan="2" align="center"><button type="submit" onclick="#{userBean.go()}">Send</button></td>
</tr>
</table>
</form>
<p>#{user.output_message}</p>
</ui:define>
</ui:composition>
</html>
按钮html标签引起的问题。如果您想使用 JSF 机制,请不要使用 HTML 按钮或锚点。而是使用 (button/link) 组件来提交页面。定义导航规则 ot userBean.go 应该传回下一个页面名称。
我刚刚解决了这个问题。问题出在输入标签上,值属性不代表文本框中写入的值只是某种默认值或某种预值。无论如何,我使用的不是输入和表单标签,而且效果很好
h 是一个命名空间 url xmlns:h="http://xmlns.jcp.org/jsf/html"
基本上,在 input.xhtml 中有一个表单接受用户名和密码,如果它们等于特定值(与值无关),程序应在浏览器中打印一条消息,但这不会发生。为了确定问题所在,我添加了 2 行 "System.out.println(...)" 并在其中打印 属性 的值,我发现即使提交后属性仍然为空。因此,在我单击发送后,控制台中会显示 "null null"。感谢您的帮助!
这是 UserBean class(支持 bean)
package bean;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class UserBean implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private String password;
private String output_message;
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 go(){
output_message = "";
if(name == null && password == null){
output_message += "Both fields cannot be empty!";
}else if(name == "name" && password == "pass"){
output_message += "Success!";
}else{
output_message += "Data is wrong!";
}
System.out.println(name);
System.out.println(password);
return output_message;
}
public String getOutput_message() {
return output_message;
}
public void setOutput_message(String output_message) {
this.output_message = output_message;
}
public String ret(String r){
return r;
}
}
这是 input.xhtml 文件,其中包含将数据提交到 bean 的表单。 (忽略 url 到 template.xhtml,它只是一个具有页眉和页脚的父模板,而不是 input.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://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:cc="http://java.sun.com/jsf/composite/myComponent">
<ui:composition template="/WEB-INF/templates/template.xhtml">
<ui:define name="content">
<style type="text/css">
#form1{
position: absolute;
top: 20%;
left:40%;
border: 1px solid black;
background-color: orange;
}
td{
padding-top:10px;
}
input[type="text"]{
backgorund-color:blue;
}
</style>
<form id="form1" method="post">
<table columns="2">
<tr>
<td><span>Emri:</span></td>
<td><input type="text" id="emri" value="#{userBean.name}"/></td>
</tr>
<tr>
<td><span>Password:</span></td>
<td><input type="password" id="pass" value="#{userBean.password}"/></td>
</tr>
<tr>
<td colspan="2" align="center"><button type="submit" onclick="#{userBean.go()}">Send</button></td>
</tr>
</table>
</form>
<p>#{user.output_message}</p>
</ui:define>
</ui:composition>
</html>
按钮html标签引起的问题。如果您想使用 JSF 机制,请不要使用 HTML 按钮或锚点。而是使用 (button/link) 组件来提交页面。定义导航规则 ot userBean.go 应该传回下一个页面名称。
我刚刚解决了这个问题。问题出在输入标签上,值属性不代表文本框中写入的值只是某种默认值或某种预值。无论如何,我使用的不是输入和表单标签,而且效果很好 h 是一个命名空间 url xmlns:h="http://xmlns.jcp.org/jsf/html"