URL rewriting solution needed for JSF

后端 未结 3 541
花落未央
花落未央 2020-12-31 11:44

Suppose the following application landscape:

+-----------------+
| App server      |
+-----------------+
|                 |                                          


        
3条回答
  •  猫巷女王i
    2020-12-31 12:35

    I'm posting solution which may be helpful for others facing the same problem. All I needed to do is implementing my own javax.faces.application.ViewHandler and register it in faces-config.xml :

    public class CustomViewHandler extends ViewHandlerWrapper {
      private ViewHandler wrappped;
    
      public CustomViewHandler(ViewHandler wrappped) {
        super();
        this.wrappped = wrappped;
      }
    
      @Override
      public ViewHandler getWrapped() {
        return wrappped;
      }
    
      @Override
      public String getActionURL(FacesContext context, String viewId) {
        String url =  super.getActionURL(context, viewId);
        return removeContextPath(context, url);
      }
    
      @Override
      public String getRedirectURL(FacesContext context, String viewId, Map> parameters, boolean includeViewParams) {
        String url =  super.getRedirectURL(context, viewId, parameters, includeViewParams);
        return removeContextPath(context, url);
      }
    
      @Override
      public String getResourceURL(FacesContext context, String path) {
        String url = super.getResourceURL(context, path);
        return removeContextPath(context, url);
      }
    
      private String removeContextPath(FacesContext context, String url) {
        ServletContext servletContext = (ServletContext) context.getExternalContext().getContext();
        String contextPath = servletContext.getContextPath();
        if("".equals(contextPath)) return url; // root context path, nothing to remove
        return url.startsWith(contextPath) ? url.substring(contextPath.length()) : url;
      }
    }
    

    faces-config.xml :

    
      
        test.CustomViewHandler
      
    
    

提交回复
热议问题