java 如何使用 JSF 通过 URL 将参数传递给 bean 类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15870755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:08:05  来源:igfitidea点击:

How to pass parameters via URL to a bean class using JSF?

javajsf

提问by smk

I have created a login page for my JSF application. I want to pass the username and password as parameters via the URL to later receive them as fields in a bean class. How can I do this?

我为我的 JSF 应用程序创建了一个登录页面。我想通过 URL 将用户名和密码作为参数传递,以便稍后将它们作为 bean 类中的字段接收。我怎样才能做到这一点?

回答by Avinash Singh

You should pass it as POST parameter which is what JSF does by default , You can google for a quick example of a Login page with JSF , however if you want to read the request parameters from URL then you can do like

您应该将它作为 POST 参数传递,这是 JSF 默认情况下所做的,您可以谷歌搜索一个使用 JSF 的登录页面的快速示例,但是如果您想从 URL 读取请求参数,那么您可以这样做

        <a href="name.jsf?id=#{testBean.id}" />

You need something like this in your bean

你的豆子里需要这样的东西

@ManagedBean
@RequestScoped
public class TestBean {

  @ManagedProperty(value = "#{param.id}")
  private String id;

  .....
}

You can also do this in your xhtml to get the same outcome, this will work with JSF 2.x as viewParam is not available in JSF 1.2

您也可以在 xhtml 中执行此操作以获得相同的结果,这将适用于 JSF 2.x,因为 JSF 1.2 中不提供 viewParam

<f:metadata>
    <f:viewParam name="id" value="#{testBean.id}" />
</f:metadata>

Above line will set the parameter id in bean from the request parameter id when your bean is created.

创建 bean 时,上面的行将从请求参数 id 中设置 bean 中的参数 id。

回答by Himanshu Bhardwaj

Well first of all, if you are thinking of appending the username and password as part of query string. THEN DO NOT DO IT, you are making your system vulnerable.

首先,如果您想将用户名和密码作为查询字符串的一部分附加。那么不要这样做,你会让你的系统容易受到攻击。

Regarding the answer to your question:

关于你的问题的答案:

<h:commandLink action="#{ttt.goToViewPage()}" value="View">
    <!-- To access via f:param tag, this does not maps directly to bean. Hence using context fetch the request parameter from request map. -->
    <!-- <f:param name="selectedProfileToView" value="#{profile.id}" /> -->

    <!-- Using this to replace the f:param tag to avoid getting the request object -->
    <f:setPropertyActionListener target="#{ttt.selectedStudentProfile}" value="#{profile.id}" />

</h:commandLink>

f:param(as mentioned in comment), this will not map directly to the bean attribute, but you will have to use context to get the request object from which you can reference the value from the requestparametermap.

f:param(如评论中所述),这不会直接映射到 bean 属性,但您必须使用上下文来获取请求对象,您可以从中引用 requestparametermap 的值。

FacesContext context = FacesContext.getCurrentInstance();
Map<String, String> requestMap = context.getExternalContext().getRequestParameterMap();

f:setPropertyActionListener, this is the other attribute, which will map directly to the attribute of the managed bean.

f:setPropertyActionListener,这是另一个属性,它将直接映射到托管 bean 的属性。

<h:commandLink action="#{ttt.goToEditPage(profile.id)}" value="Edit">

If you look here I have mentioned the argument in the function. A method with similar signature should be present in the managed bean class, the value will be mapped directly to the function argument.

如果你看这里,我已经提到了函数中的参数。托管 bean 类中应该存在具有类似签名的方法,该值将直接映射到函数参数。