简体   繁体   English

web.xml中的JSF异常处理

[英]Exception handling with JSF in web.xml

The problem is that I don't completely understand what exceptions are handled and what aren't. 问题是我不完全了解处理了哪些异常以及未处理什么异常。 I created new com.me.exceptions.InvalidPasswordException and populated it in the web.xml along with other exceptions: 我创建了新的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>

I throw it from the my LoginBean: 我从我的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();
        }
    }
}

Why my InvalidPasswordException is not handled? 为什么我的InvalidPasswordException没有得到处理? Also if I throw IOException from the same place, it also doesn't work, though IOException is handled if it is thrown in the case of wrong URI in the request. 另外,如果我从同一个地方抛出IOException,它也将无法正常工作,尽管如果在请求中URI错误的情况下抛出了IOException,则它会被处理。 Though I can see stack traces of both in the logs. 虽然我可以在日志中看到两者的堆栈跟踪。 Also I tried to throw exceptions from filters, it also works fine. 我也试图从过滤器抛出异常,它也可以正常工作。

in general throw InvalidPasswordException or InvalidUsernameException is not good practice. 通常,抛出InvalidPasswordException或InvalidUsernameException不是一个好的做法。 User pass credentials if you find user from db you will do login, if not return null and redirect to another page or the same page show message "user not found" . 用户传递的凭据,如果您从db找到用户,则将登录,如果不返回null并重定向到另一页面或同一页面,则显示消息“未找到用户”。 You are not compare password for throw exception!. 您不是比较抛出异常的密码!。 The first your method must be change to 您的方法必须首先更改为

 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;
    }

After changing logic you can throw exception if you need instead of returning/redirecting , but throw / catch is not best practice " "Do NOT throw exceptions if you can avoid it, it makes your code MUCH slower ". 更改逻辑后,可以根据需要抛出异常,而不是返回/重定向,但是“抛出/捕获”不是最佳实践““如果可以避免,则不要抛出异常,这会使您的代码慢得多”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM