使用 PrettyFaces 从 servlet 重定向/转发

发布于 2024-12-13 15:10:37 字数 882 浏览 2 评论 0原文

我正在使用 PrettyFaces 3.3.0 我想从 servlet 进行自定义重定向和转发,

我在他们的文档中找到了以下代码:

public class CustomRedirector 
{
    public void redirect(HttpServletRequest request, HttpServletResponse response, 
                            String mappingId, Map<String, String[]>params)
    {
        PrettyContext context = PrettyContext.getCurrentInstance(request);
        PrettyURLBuilder builder = new PrettyURLBuilder();

        URLMapping mapping = context.getConfig().getMappingById(mappingId);
        String targetURL = builder.build(mapping, params);

        targetURL = response.encodeRedirectURL(targetURL);
        response.sendRedirect(targetURL);
    }       
}

我想知道如何从 servlet 调用重定向方法,mappingId(requestURI?)是什么以及什么是Mapparams 的值,我需要一个从 servlet 调用上述方法的小例子吗?

以及如何使用漂亮面孔从 servlet 进行转发,请指教。

i am using PrettyFaces 3.3.0
and i want to make custom redirect and forward from a servlet

i found the following code on their documentation:

public class CustomRedirector 
{
    public void redirect(HttpServletRequest request, HttpServletResponse response, 
                            String mappingId, Map<String, String[]>params)
    {
        PrettyContext context = PrettyContext.getCurrentInstance(request);
        PrettyURLBuilder builder = new PrettyURLBuilder();

        URLMapping mapping = context.getConfig().getMappingById(mappingId);
        String targetURL = builder.build(mapping, params);

        targetURL = response.encodeRedirectURL(targetURL);
        response.sendRedirect(targetURL);
    }       
}

and i was wondering how to call the redirect method from the servlet, what will be the mappingId (the requestURI ?) and what will be the value of Map<String, String[]>params, i need a small example please of calling above method from a servlet?

and how to do forwarding from servlet with prettyfaces too, please advise.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黄昏下泛黄的笔记 2024-12-20 15:10:37

“String mappingId”是 PrettyFaces 配置中 url 映射的 ID。每个 url 映射都应该有一个 ID(在 XML 或注释配置中)。Map

params 是名称-值对中的参数列表,用于根据指定的 URL 映射模式生成出站链接 。身份证号。

例如:

<url-mapping id="foo">
    <pattern value="/#{cat}/#{item}" />
    <view-id value="/bar.xhtml" />
</url-mapping>

所以你会像这样调用你的方法:

Map<String, String[]> map = new HashMap<>();
map.put("cat", "blah");
map.put("item", "45");
new CustomRedirector.redirect(request, response, "foo", map);

你将被重定向到:

/blah/45

The "String mappingId" is the ID of the url-mapping in your PrettyFaces configuration. Each url-mapping should have an ID (either in the XML, or the Annotations configuration.)

The Map params is a list of parameters in name-value pairs that is used to generate the outbound link based on the URL-mapping pattern specified by the id.

For example:

<url-mapping id="foo">
    <pattern value="/#{cat}/#{item}" />
    <view-id value="/bar.xhtml" />
</url-mapping>

So you would call your method like so:

Map<String, String[]> map = new HashMap<>();
map.put("cat", "blah");
map.put("item", "45");
new CustomRedirector.redirect(request, response, "foo", map);

And you will be redirected to:

/blah/45
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文