JSF所需的URL重写解决方案

JSF所需的URL重写解决方案,第1张

JSF所需的URL重写解决方案

我正在发布解决方案,这可能会对其他面临相同问题的人有所帮助。我要做的就是实现自己的

javax.faces.application.ViewHandler
并将其注册到
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<String, List<String>> 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:

<faces-config 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-facesconfig_2_0.xsd"   version="2.0">  <application>    <view-handler>test.CustomViewHandler</view-handler>  </application></faces-config>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5501203.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存