How to “throw” JSF2 404 error?

前端 未结 3 1910
一向
一向 2020-12-16 16:45

Let\'s say that I have an application which manages users. You can add new user, delete them, edit detail etc. Each user has na ID and has detail page on URL like this:

3条回答
  •  旧巷少年郎
    2020-12-16 17:04

    Just attach a validator to the id view parameter and if validation fails, set error code 404 on the response.

    e.g.

    Consider this simple Facelet:

    
    
        
            
        
    
        
    
            
    
        
    
    
    

    And the following backing bean:

    @ManagedBean
    @ViewScoped
    public class MyBean {
    
        private Long id;
    
        public void validate(FacesContext context, UIComponent component, Object object) {
            // Do some validation
            // And if failed:
            context.getExternalContext().setResponseStatus(404);
            context.responseComplete();
        }
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
    }
    

提交回复
热议问题