栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

JSF所需的URL重写解决方案

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/508812.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号