开发者

JSF 2: outputLink whose parameters depend on user input

Here is my problem: I need to create a (seemingly) simple front-end for a report. The user enters a bunch of numbers, seperated by whitespace or commas, which are the IDs that will be brought up in a report.

I use a converter to change this from a string to a List, and then from there back into a form where the numbers are delimited with a comma:

@FacesConverter(value="multiProdConverter")
public class MultiProdConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        StringTokenizer splitter = new StringTokenizer(value, " \t\n\r\f,");

        List<Integer> ret = new ArrayList<Integer>();
        while (splitter.hasMoreTokens()) {
            String token = splitter.nextToken();
            ret.add(Integer.parseInt(token));
        }

        return ret;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return implode((List<Integer>)value);
    }

    private String implode(List<Integer> sdaList) {
        StringBuilder sb = new StringBuilder();
        if (!sdaList.isEmpty()) {
            sb.append(sdaList.get(0));
            for (int i = 1; i < sdaList.size(); i++) {
                sb.append(",");
                sb.append(sdaList.get(i));
            }
        }
        return sb.toString();
    }
}

However, what's the best way to actually get this formatted开发者_如何转开发 version of the numbers into a request parameter for an external page?

I want the user to just hit submit after giving the numbers, and then the page goes to this report. This would be pretty easy using just javascript, but what's the "JSF" way to do this?

Thanks, Zack


I would just redirect to the external resource with help of ExternalContext#redirect().

<h:form>
    <h:inputText value="#{bean.field}" />
    <h:commandLink value="click" action="#{bean.submit}" />
</h:form>

with

public void submit() {
    String url = "http://external.com";
    String query = "name=" + URLEncoder.encode(field.replaceAll("\\s+", ","), "UTF-8");
    FacesContext.getCurrentInstance().getExternalContext().redirect(url + "?" + query);
}


That's a nice approach, you could also disabled the submit button, or command link, until all the report parameters have been entered, something like.

<h:outputLink  id="reportURL" value="#{reportBean.url}" rendered="#{reportBean.enableUrl}">
       <h:outputText value="Go To Report" styleClass="someClass"/>   
</h:outputLink>

However, in your logic you'll have to determine when it is appropriate for the url to be rendered.

Hope this might help you.

Regards.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜