FileDocCategorySizeDatePackage
StrutsUtil.javaAPI DocExample9283Mon Jul 23 13:26:56 BST 2007org.apache.struts2.util

StrutsUtil

public class StrutsUtil extends Object
Struts base utility class, for use in Velocity and Freemarker templates

Fields Summary
protected static final Log
log
protected HttpServletRequest
request
protected HttpServletResponse
response
protected Map
classes
protected org.apache.struts2.views.jsp.ui.OgnlTool
ognl
protected com.opensymphony.xwork2.util.ValueStack
stack
Constructors Summary
public StrutsUtil(com.opensymphony.xwork2.util.ValueStack stack, HttpServletRequest request, HttpServletResponse response)



           
        this.stack = stack;
        this.request = request;
        this.response = response;
    
Methods Summary
public java.lang.Objectbean(java.lang.Object aName)

        String name = aName.toString();
        Class c = (Class) classes.get(name);

        if (c == null) {
            c = ClassLoaderUtils.loadClass(name, StrutsUtil.class);
            classes.put(name, c);
        }

        return ObjectFactory.getObjectFactory().buildBean(c, stack.getContext());
    
public java.lang.StringbuildUrl(java.lang.String url)

        return UrlHelper.buildUrl(url, request, response, null);
    
public java.lang.ObjectfindString(java.lang.String name)

        return stack.findValue(name, String.class);
    
public java.lang.ObjectfindValue(java.lang.String expression, java.lang.String className)

        return stack.findValue(expression, Class.forName(className));
    
public java.lang.StringgetContext()

        return (request == null)? "" : request.getContextPath();
    
public java.lang.StringgetText(java.lang.String text)

        return (String) stack.findValue("getText('" + text + "')");
    
public java.lang.StringhtmlEncode(java.lang.Object obj)

        if (obj == null) {
            return null;
        }

        return TextUtils.htmlEncode(obj.toString());
    
public java.lang.Stringinclude(java.lang.Object aName)

        return include(aName, request, response);
    
public java.lang.Stringinclude(java.lang.Object aName, javax.servlet.http.HttpServletRequest aRequest, javax.servlet.http.HttpServletResponse aResponse)

deprecated
the request and response are stored in this util class, please use include(string)

        try {
            RequestDispatcher dispatcher = aRequest.getRequestDispatcher(aName.toString());

            if (dispatcher == null) {
                throw new IllegalArgumentException("Cannot find included file " + aName);
            }

            ResponseWrapper responseWrapper = new ResponseWrapper(aResponse);

            dispatcher.include(aRequest, responseWrapper);

            return responseWrapper.getData();
        }
        catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    
public booleanisTrue(java.lang.String expression)

        Boolean retVal = (Boolean) stack.findValue(expression, Boolean.class);
        if (retVal == null) {
            return false;
        }
        return retVal.booleanValue();
    
public java.util.ListmakeSelectList(java.lang.String selectedList, java.lang.String list, java.lang.String listKey, java.lang.String listValue)
the selectedList objects are matched to the list.listValue

listKey and listValue are optional, and if not provided, the list item is used

param
selectedList the name of the action property that contains the list of selected items or single item if its not an array or list
param
list the name of the action property that contains the list of selectable items
param
listKey an ognl expression that is exaluated relative to the list item to use as the key of the ListEntry
param
listValue an ognl expression that is exaluated relative to the list item to use as the value of the ListEntry
return
a List of ListEntry

        List selectList = new ArrayList();

        Collection selectedItems = null;

        Object i = stack.findValue(selectedList);

        if (i != null) {
            if (i.getClass().isArray()) {
                selectedItems = Arrays.asList((Object[]) i);
            } else if (i instanceof Collection) {
                selectedItems = (Collection) i;
            } else {
                // treat it is a single item
                selectedItems = new ArrayList();
                selectedItems.add(i);
            }
        }

        Collection items = (Collection) stack.findValue(list);

        if (items != null) {
            for (Iterator iter = items.iterator(); iter.hasNext();) {
                Object element = (Object) iter.next();
                Object key = null;

                if ((listKey == null) || (listKey.length() == 0)) {
                    key = element;
                } else {
                    key = ognl.findValue(listKey, element);
                }

                Object value = null;

                if ((listValue == null) || (listValue.length() == 0)) {
                    value = element;
                } else {
                    value = ognl.findValue(listValue, element);
                }

                boolean isSelected = false;

                if ((value != null) && (selectedItems != null) && selectedItems.contains(value)) {
                    isSelected = true;
                }

                selectList.add(new ListEntry(key, value, isSelected));
            }
        }

        return selectList;
    
public inttoInt(long aLong)

        return (int) aLong;
    
public longtoLong(int anInt)

        return (long) anInt;
    
public longtoLong(java.lang.String aLong)

        if (aLong == null) {
            return 0;
        }

        return Long.parseLong(aLong);
    
public java.lang.StringtoString(long aLong)

        return Long.toString(aLong);
    
public java.lang.StringtoString(int anInt)

        return Integer.toString(anInt);
    
public java.lang.StringurlEncode(java.lang.String s)

        try {
            return URLEncoder.encode(s, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            return s;
        }