หน้าเว็บ

วันพุธที่ 7 พฤษภาคม พ.ศ. 2557

JSF Request (for get query string parameter)



JSFRequest.java
package com.blogspot.na5cent.jsflibrary.util;

import java.util.Map;
import javax.faces.context.FacesContext;

/**
 *
 * @author redcrow
 * source http://na5cent.blogspot.com/2014/05/jsf-request-for-get-query-string.html
 */
public class JSFRequest {

    public static String requestString(String parameterName) {
        String request = requestMap().get(parameterName);
        if (request == null) {
            throw new NullPointerException("request parameter \"" + parameterName + "\" is null.");
        }

        return request;
    }

    public static Map<String, String> requestMap() {
        return FacesContext
                .getCurrentInstance()
                .getExternalContext()
                .getRequestParameterMap();
    }

    public static Integer requestInteger(String parameterName) {
        return Integer.valueOf(requestString(parameterName));
    }

    public static Long requestLong(String parameterName) {
        return Long.valueOf(requestString(parameterName));
    }

    public static Float requestFloat(String parameterName) {
        return Float.valueOf(requestString(parameterName));
    }

    public static Double requestDouble(String parameterName) {
        return Double.valueOf(requestString(parameterName));
    }

    public static Boolean requestBoolean(String parameterName) {
        return Boolean.valueOf(requestString(parameterName));
    }

    public static String getQueryString(RequestFilter filter) {
        String queryString = "";
        Map<String, String> parameterMap = requestMap();

        if (parameterMap != null) {
            StringBuilder builder = new StringBuilder();
            for (Map.Entry<String, String> entry : parameterMap.entrySet()) {
                if (filter.filterParam(entry)) {
                    builder.append("&")
                            .append(entry.getKey())
                            .append("=")
                            .append(entry.getValue());
                }
            }

            queryString = builder.toString();
            if (queryString.startsWith("&")) {
                queryString = queryString.substring(1);
            }
        }

        return queryString;
    }

    public static String getQueryString() {
        return getQueryString(new RequestFilter() {

            @Override
            public boolean filterParam(Map.Entry<String, String> entryParam) {
                return true;
            }
        });
    }

    public static String getQueryStringNotFaces() {
        return getQueryString(new RequestFilter() {

            @Override
            public boolean filterParam(Map.Entry<String, String> entryParam) {
                return !entryParam.getKey().startsWith("javax.faces")
                        && !entryParam.getKey().startsWith("j_id");
            }
        });
    }

    public static abstract class RequestFilter {

        public abstract boolean filterParam(Map.Entry<String, String> entryParam);
    }
}
Example for get query string of browser


...
...
...
@ManagedBean
@RequestScoped
public class MyManagedBean{

    @PostConstruct
    public void postConstruct(){ 
        Integer postId = JSFRequest.requestInteger('postId');
        //postId = 51
    }
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น