使用jsf的动态页面网址 [英] Dynamic page url using jsf

查看:92
本文介绍了使用jsf的动态页面网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个带有动态URL的页面.让我们假设用户个人资料页面.

I need a page with URL that is dynamic. Let's suppose user profile page.

示例:

  • 用户个人资料(salah)socail.com/salah的网址和页面必须显示salah数据
  • 用户个人资料(wael)的网址socail.com/wael和页面必须显示wael数据

如何使用JSF框架做到这一点.

How can I do this using JSF framework .

推荐答案

您需要具有一个servlet,该servlet具有适合您需要的映射,如下所示:

You need to have a servlet with mapping appropriate for your needs, something like this:

@WebServlet("/")

然后,当有人输入yourpage.com/<anything here>之类的URL时,该servlet将处理该请求.要从该网址获取用户名,您需要执行以下操作:

Then when someone enters URL like yourpage.com/<anything here> then this servlet is gonna handle the request. To get a username from that url you need to do something like:

String username = request.getRequestURI().substring(1)-将获得不带主机名的请求信息,并从中删除/.

String username = request.getRequestURI().substring(1) - that is gonna get request info without the hostname and strip / from it.

设置参数(您的信息),以便您可以在JSF中检索该参数:

Set the parameter (your info) so you can retrieve that in JSF:

request.setAttribute("userInfo", getSomeInfoByUsername(username));

然后,您可以将请求转发到您的JSF视图文件,如下所示:

Then you can forward the request to your JSF view file, like this:

request.getRequestDispatcher("faces/index.xhtml").forward(request,response);

然后在JSF文件中,您可以执行以下操作以从请求中获取信息:

Then in JSF file you can do something like this to get the info from request:

<h:outputText value="#{requestScope.userInfo}"/>

如果我什么都没想念,那么从URL获取用户名可能可以通过更好的方式进行检索,但是现在我什么都没想到.

If I didn't miss anything those are all the pieces you need, getting username from URL can probably be retrieved in a better way, but nothing comes to my mind right now.

此处有关servlet的详细信息

这篇关于使用jsf的动态页面网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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