基本上,在input.xhtml中有一个表单,它接受用户名和密码,如果它们等于一个特定的值(与值无关),程序应该在浏览器中打印一条消息,但这不是不会发生。为了确定问题,我添加了2行“System.out.println(...)”,其中我打印属性的值,我发现的是即使我提交后属性仍然为null。所以我在控制台中点击发送后写成“null null”。任何帮助表示赞赏!
这是UserBean类(支持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的表单。 (忽略对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>
答案 0 :(得分:0)
按钮html标记引起的问题。如果要使用JSF机制,请不要使用HTML按钮或锚点。而是使用(按钮/链接)组件来提交页面。定义导航规则,userBean.go应该传回下一个页面名称。
答案 1 :(得分:0)
我刚刚解决了这个问题。问题出在input标签上,value属性并不代表文本框中写的值只是某种默认值或某种前置值。无论如何,而不是我使用的输入和表单标签,并且工作得很好 h是url xmlns的命名空间:h =“http://xmlns.jcp.org/jsf/html”