我写了一个简单的JSF表单。问题是有一个我找不到的错误。当我打开主页并输入用户名和密码时,页面必须将我重定向到下一页,但这不会发生。你能帮我找到我的错吗?
这是主要的登录JSF页面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns:h="http://java.sun.com/jsf/html">
<head>
<title>Login</title>
<link rel="stylesheet" type="text/css" href="resources/css/style.css" />
<script src="resources/js/cufon-yui.js" type="text/javascript"></script>
<script src="resources/js/ChunkFive_400.font.js" type="text/javascript"></script>
<script type="text/javascript">
Cufon.replace('h1',{ textShadow: '1px 1px #fff'});
Cufon.replace('h2',{ textShadow: '1px 1px #fff'});
Cufon.replace('h3',{ textShadow: '0px 1px #000'});
Cufon.replace('.back');
</script>
</head>
<body>
<div class="wrapper">
<div class="content">
<div id="form_wrapper" class="form_wrapper">
<form class="login active">
<h3><center><img src="resources/images/title.png"/></center></h3>
<div>
<label>Username:</label>
<h:inputText value="#{loginController.user}"/>
<span class="error">This is an error</span>
</div>
<div>
<label>Password:</label>
<h:inputSecret value="#{loginController.password}"/>
<span class="error">This is an error</span>
</div>
<div class="bottom">
<h:commandButton label="Login" value="Login" action="#{loginController.user_compare}"/>
<div class="clear"></div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
这是托管bean
/** Bean for checking users and passwords.
If the user enters the correct username and password
the user will be redirected to main.xhtml
If not the page will refresh. */
package com.dx.sr_57;
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
@Named("loginController")
@SessionScoped
public class user_check implements Serializable {
private String user;
private String password;
public user_check(){
}
public user_check(String user, String password){
super();
this.user = user;
this.password = password;
}
/** get the content of the variables from the JSF Login page */
public String setUser(String newValue) {
user = newValue;
return user;
}
public String getUser(){
return user;
}
public String setPassword(String newValue) {
password = newValue;
return password;
}
public String getPassword(){
return password;
}
public String user_compare() {
return "success";
}
}
答案 0 :(得分:4)
您需要使用<h:form>
组件才能使JSF输入和命令起作用。
所以,替换
<form class="login active">
...
</form>
通过
<h:form styleClass="login active">
...
</h:form>
您还需要修复您的setter才能成为值得信赖的setter,否则您可能会遇到PropertyNotFoundException
。
所以,替换
public String setUser(String newValue) {
user = newValue;
return user;
}
public String setPassword(String newValue) {
password = newValue;
return password;
}
通过
public void setUser(String newValue) {
user = newValue;
}
public void setPassword(String newValue) {
password = newValue;
}
无关,自1998年以来,HTML <center>
标记已弃用,在XHTML严格中无效。去掉它。您需要在text-align: center
上设置CSS <img>
。