views:

68

answers:

2

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

A: 

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.

StudiousJoseph
The only criteria for the report are a list of IDs. They can go to the report at any time, they just might not have any response. I could do an onchange for the text area, format the values, and then update the link...that should work.
Zack
That should definitely work. :)
StudiousJoseph
+1  A: 

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);
}
BalusC
if I redirect, will it keep it's GET query parameters?
Zack
They are just been passed into the redirect URL.
BalusC