我是一个尝试学习JSF的新手。你可以帮我解决日食中的错误404! 我尝试通过进入属性更改服务器详细信息,但它仍然给我错误。
请帮我解决这个问题。提前谢谢。
我的登录代码如下:
<!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">
<head><title>JSF Login</title></head>
<body>
<h1>Login</h1>
<h:form>
<table>
<tr>
<td><h:outputText value="Username: " /></td>
<td><h:inputText id="loginname"
value="#{loginBean.uname}" />
</td>
</tr>
<tr>
<td><h:outputText value="Password: " /></td>
<td><h:inputSecret id="password"
value="#{loginBean.password}" />
</td>
</tr>
<tr>
<td> </td>
<td><h:commandButton value="Login"
action="#{loginBean.loginProject()}"/>
</td>
</tr>
</h:form>
</body>
</html>
我的loginbean文件:
package beans;
import dao.UserDAO;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
@ManagedBean(name = "loginBean")
@SessionScoped
/**
*
* @author User
*/
public class LoginBean implements Serializable {
private static final long serialVersionUID = 1L;
private String password;
private String message, uname;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String loginProject() {
boolean result = UserDAO.login(uname, password);
if (result) {
// get Http Session and store username
HttpSession session = Util.getSession();
session.setAttribute("username", uname);
return "home";
} else {
FacesContext.getCurrentInstance().addMessage(
null,
new FacesMessage(FacesMessage.SEVERITY_WARN,
"Invalid Login!",
"Please Try Again!"));
// invalidate session, and redirect to other pages
//message = "Invalid Login. Please Try Again!";
return "login";
}
}
public String logout() {
HttpSession session = Util.getSession();
session.invalidate();
return "login";
}
}
Web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<listener>
<listener-class>
com.sun.faces.config.ConfigureListener
</listener-class>
</listener>
<welcome-file-list>
<welcome-file>login.xhtml</welcome-file>
</welcome-file-list>
</web-app>
答案 0 :(得分:0)
假设存在所有依赖项和所有其他配置,您只需将以下内容添加到web.xml即可。我还建议您阅读java ee 7教程并使用Netbeans,因为您刚刚开始。
<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>*.xthml</url-pattern>
</servlet-mapping>