Java EE Web应用程序中的动态目录 [英] Dynamic Directory in Java EE Web Application

查看:78
本文介绍了Java EE Web应用程序中的动态目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个使用JSF的Java EE应用程序.在我的web目录中,我有一个名为index.xhtml的文件.我的目标是根据父目录的名称在此网页上提供不同的内容.

I have created a Java EE application that uses JSF. In my web directory, I have a file named index.xhtml. My goal is to serve different content on this webpage based upon the parent directory's name.

例如:

http://localhost:8080/myapp/1/index.xhtml将打印You accessed through "1". http://localhost:8080/myapp/1234/index.xhtml将打印You accessed through "1234".

我不想为每个可能的号码创建目录;它应该是完全动态的.

I do not want to create a directory for every single possible number; it should be completely dynamic.

此外,我需要我的导航规则仍然可以使用.因此,如果我有这样的导航规则:

Additionally, I need my navigation rules to still be usable. So if I have a navigation rule such as this:

<navigation-rule>
    <display-name>*</display-name>
    <from-view-id>*</from-view-id>
    <navigation-case>
        <from-outcome>index</from-outcome>
        <to-view-id>/index.xhtml</to-view-id>
        <redirect />
    </navigation-case>
</navigation-rule>

然后,如果我位于目录1234中,它将仍然重定向到index.xhtml页面 1234中.

Then if I am in the directory 1234, it will still redirect to the index.xhtml page within 1234.

这可能吗?我该怎么办?

Is this possible? How can I do this?

推荐答案

为了将/[number]/index.xhtml转发到/index.xhtml,从而将[number]存储为请求属性,您需要servlet过滤器. doFilter()实现可以如下所示:

In order to forward /[number]/index.xhtml to /index.xhtml whereby [number] is been stored as a request attribute, you need a servlet filter. The doFilter() implementation can look like this:

@WebFilter("/*")
public class YourFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        String[] paths = request.getRequestURI().substring(request.getContextPath().length()).split("/");

        if (paths.length == 3 && paths[2].equals("index.xhtml") && paths[1].matches("[0-9]{1,9}")) {
            request.setAttribute("directory", Integer.valueOf(paths[1]));
            request.getRequestDispatcher("/index.xhtml").forward(req, res);
        }
        else {
            chain.doFilter(req, res);
        }
    }

    // ...
}

确保该数字与1到9个拉丁数字匹配,并将其存储为由directory标识的请求属性,最后转发到上下文根中的/index.xhtml.如果没有其他问题,它只会继续请求,就好像什么都没发生一样.

It makes sure the number matches 1 to 9 latin digits and stores it as a request attribute identified by directory and finally forwards to /index.xhtml in context root. If nothing maches, it simply continues the request as if nothing special happened.

/index.xhtml中,您可以通过#{directory}访问该号码.

In the /index.xhtml you can access the number by #{directory}.

<p>You accessed through "#{directory}"</p>

然后,为了确保JSF导航(和<h:form>!)能够正常工作,您需要自定义

Then, in order to make sure JSF navigation (and <h:form>!) keeps working, you need a custom view handler which overrides the getActionURL() to prepend the URL with the path represented by directory request attribute, if any. Here's a kickoff example:

public class YourViewHandler extends ViewHandlerWrapper {

    private ViewHandler wrapped;

    public YourViewHandler(ViewHandler wrapped) {
        this.wrapped = wrapped;
    }

    @Override
    public String getActionURL(FacesContext context, String viewId) {
        String actionURL = super.getActionURL(context, viewId);

        if (actionURL.endsWith("/index.xhtml")) {
            Integer directory = (Integer) context.getExternalContext().getRequestMap().get("directory");

            if (directory != null) {
                actionURL = actionURL.substring(0, actionURL.length() - 11) + directory + "/index.xhtml";
            }
        }

        return actionURL;
    }

    @Override
    public ViewHandler getWrapped() {
        return wrapped;
    }

}

要使其运行,请在faces-config.xml中进行如下注册.

In order to get it to run, register in faces-config.xml as below.

<application>
    <view-handler>com.example.YourViewHandler</view-handler>
</application>

这也是JSF定位的URL重写引擎(例如 PrettyFaces )工作的方式.

This is also pretty much how JSF targeted URL rewrite engines such as PrettyFaces work.

  • How to use a servlet filter in Java to change an incoming servlet request url?
  • How to create user-friendly and seo-friendly urls in jsf?
  • Get rewritten URL with query string

这篇关于Java EE Web应用程序中的动态目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆