在web.xml中使用JSF进行异常处理

时间:2015-01-25 05:09:45

标签: jsf exception-handling

问题在于我并不完全理解处理的异常和不处理的异常。 我创建了新的com.me.exceptions.InvalidPasswordException并在web.xml中填充了它以及其他例外:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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-app_3_0.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <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>*.xhtml</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>/view/login.xhtml</welcome-file>
    </welcome-file-list>
    <error-page>
    <exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/view/login.xhtml?faces-redirect=true</location>
<error-page>
    <exception-type>com.me.exceptions.InvalidPasswordException</exception-type>
    <location>/view/loginWrongPass.xhtml?faces-redirect=true</location>
</error-page>
<error-page>
    <exception-type>java.io.IOException</exception-type>
    <location>/view/404.xhtml?faces-redirect=true</location>
</error-page>
<filter>
<filter-name>AuthFulter</filter-name>
<filter-class>com.me.beans.AuthFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AuthFulter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
 </web-app>

我从我的LoginBean中抛出它:

@ManagedBean(name = "loginBean")
@SessionScoped
public class LoginBean implements Serializable {
    private static final long serialVersionUID = 1L;
    private String userName;
    private String password;

    !!!getters and setters are omitted

    public String login() throws InvalidPasswordException, IOException {
        int result = UserDao.login(userName, password);
        if (result == 0) {
            System.out.println("home");
            return "home";
        }
        else if (result == 1) {
            throw new InvalidPasswordException();
        }
    }
}

为什么我的InvalidPasswordException未被处理?此外,如果我从同一个地方抛出IOException,它也不起作用,但如果在请求中错误的URI的情况下抛出IOException,则会处理IOException。 虽然我可以在日志中看到两者的堆栈跟踪。 此外,我试图从过滤器中抛出异常,它也可以正常工作。

1 个答案:

答案 0 :(得分:0)

一般抛出InvalidPasswordException或InvalidUsernameException不是好习惯。用户传递凭据如果您从db中找到用户您将进行登录,如果没有返回null并重定向到另一个页面或同一页面显示消息&#34;用户未找到&#34; 。你不是比较抛出异常的密码! 第一个方法必须改为

 public String login(){
        UserObject resultObject = UserDao.login(userName, password);
        if (resultObject  == null) {
            System.out.println("user not found ");
            return "home";
        }

       if (resultObject != null) {
            System.out.println("user in database ");
            System.out.println("create user profile context "    +resultObject.getEmail() + " saving user object to session);    
return "userProfilePage";
        }

return null;
    }

更改逻辑后,如果需要而不是返回/重定向,则可以抛出异常,但抛出/捕获不是最佳实践&#34; &#34;如果可以避免异常,请不要抛出异常,这会使代码变得更慢&#34;。