You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Java生成的URL参数传递至JSF输出组件?

Got it, let's work through this problem step by step. You've already nailed the email sending and URL generation parts—now we just need to bridge the gap between that URL parameter and displaying it in your JSF page. Let's break this down:

Right now, you're using matrix parameters (the ;Foo suffix after your XHTML file) to pass the inviter's name. While matrix parameters are valid, query parameters (like ?inviterName=Foo) are more standard for this kind of invitation use case and easier to work with in most scenarios. Let's adjust your Builder class to generate a cleaner, more intuitive URL:

public class Builder {
    Mail mail = new Mail();
    public void sendInvite(String emailDestiny, User user) {
        UriBuilder builder = UriBuilder
                .fromUri("http://localhost:8080/") // Add http:// to make it a fully valid clickable URL
                .path("BuilderTest/faces/index.xhtml")
                .queryParam("inviterName", user.getName()) // Use query param for inviter name
                .queryParam("inviterEmail", user.getEmail()); // Optional: include email if you need it later
        URI uri = builder.build();
        
        String emailSubject = "INVITE!";
        String emailBody = "Congrats! You got invited by: " + user.getName() + "\nClick the link below to access our services!\n" + uri ;
        mail.sendMailPwd(emailDestiny, emailSubject, emailBody);
    }
}

If you really want to stick with matrix parameters, we can handle that too—just skip to the alternative code in step 2.

2. Create a Managed Bean to Fetch the Parameter

You'll need a JSF managed bean to pull the parameter from the URL and store it so your page can access it. Let's make a view-scoped bean (it stays alive as long as the user is on this page):

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import java.io.Serializable;

@ManagedBean
@ViewScoped
public class InviteBean implements Serializable {
    private String inviterName;
    private String inviterEmail;

    // Initialize the bean and fetch parameters when it's created
    public InviteBean() {
        FacesContext context = FacesContext.getCurrentInstance();
        
        // For query parameters (use this if you updated the Builder class)
        inviterName = context.getExternalContext().getRequestParameterMap().get("inviterName");
        inviterEmail = context.getExternalContext().getRequestParameterMap().get("inviterEmail");

        // --- Alternative: If you want to use matrix parameters instead ---
        // String requestPath = context.getExternalContext().getRequestServletPath() + context.getExternalContext().getPathInfo();
        // if (requestPath != null && requestPath.contains(";")) {
        //     // Extract the matrix part after the semicolon
        //     inviterName = requestPath.split(";")[1];
        // }
    }

    // Getters and Setters (required for JSF to access the values)
    public String getInviterName() {
        return inviterName;
    }

    public void setInviterName(String inviterName) {
        this.inviterName = inviterName;
    }

    public String getInviterEmail() {
        return inviterEmail;
    }

    public void setInviterEmail(String inviterEmail) {
        this.inviterEmail = inviterEmail;
    }
}

3. Display the Inviter Info in Your JSF Page

Now you can use JSF output components to show the inviter's details from the bean. Update your XHTML page like this:

<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:pt="http://xmlns.jcp.org/jsf/passthrough" xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui">
    <h:head>
        <meta charset="utf-8" />
    </h:head>
    <h:body>
        <p:outputLabel value="Hello! You've been invited by:"/>
        <br/>
        <h:outputText value="#{inviteBean.inviterName}" style="font-weight: bold; font-size: 1.2em;"/>
        <br/>
        <h:outputText value="Contact them at: #{inviteBean.inviterEmail}"/>
    </h:body>
</ui:composition>

4. Quick Extra Tips

  • Null Checks: Add a check in your bean to handle cases where someone visits the page without the invite link (e.g., show an error message like "Invalid invite link" or redirect to a home page).
  • Special Characters: Don't worry about encoding names with spaces or accents—UriBuilder automatically handles URL encoding for you.
  • Modern JSF: If you're using JSF 2.3 or newer, you can replace @ManagedBean with @Named and use javax.faces.view.ViewScoped for CDI compatibility instead of the older JSF view scope.

内容的提问来源于stack exchange,提问作者Vitor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:47:43