简体   繁体   English

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

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

I have created a login page for my JSF application. 我已经为我的JSF应用程序创建了一个登录页面。 I want to pass the username and password as parameters via the URL to later receive them as fields in a bean class. 我想通过URL将用户名和密码作为参数传递,以便稍后将它们作为bean类中的字段接收。 How can I do this? 我怎样才能做到这一点?

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默认执行的操作。您可以使用google快速查看带有JSF的Login页面,但是如果您想从URL读取请求参数,那么您可以这样做

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

You need something like this in your bean 你的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,因为JSPa 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。

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属性,但是您必须使用context来获取请求对象,您可以从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类中应该存在具有类似签名的方法,该值将直接映射到函数参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM