Search in sources :

Example 16 with OgnlValueStack

use of com.opensymphony.xwork2.ognl.OgnlValueStack in project struts by apache.

the class OgnlValueStackTest method testConvertStringArrayToList.

public void testConvertStringArrayToList() {
    Foo foo = new Foo();
    OgnlValueStack vs = createValueStack();
    vs.push(foo);
    vs.setValue("strings", new String[] { "one", "two" });
    assertNotNull(foo.getStrings());
    assertEquals("one", foo.getStrings().get(0));
    assertEquals("two", foo.getStrings().get(1));
}
Also used : Foo(com.opensymphony.xwork2.util.Foo)

Example 17 with OgnlValueStack

use of com.opensymphony.xwork2.ognl.OgnlValueStack in project struts by apache.

the class FreemarkerResult method createModel.

/**
 * Build the instance of the ScopesHashModel, including JspTagLib support
 * <p>
 * Objects added to the model are
 * </p>
 *
 * <ul>
 * <li>Application - servlet context attributes hash model
 * <li>JspTaglibs - jsp tag lib factory model
 * <li>Request - request attributes hash model
 * <li>Session - session attributes hash model
 * <li>request - the HttpServletRequst object for direct access
 * <li>response - the HttpServletResponse object for direct access
 * <li>stack - the OgnLValueStack instance for direct access
 * <li>ognl - the instance of the OgnlTool
 * <li>action - the action itself
 * <li>exception - optional : the JSP or Servlet exception as per the servlet spec (for JSP Exception pages)
 * <li>struts - instance of the StrutsUtil class
 * </ul>
 *
 * @return TemplateModel returns the created template model
 * @throws TemplateModelException in case of errors during creating the model
 */
protected TemplateModel createModel() throws TemplateModelException {
    ServletContext servletContext = ServletActionContext.getServletContext();
    HttpServletRequest request = ServletActionContext.getRequest();
    HttpServletResponse response = ServletActionContext.getResponse();
    ValueStack stack = ActionContext.getContext().getValueStack();
    Object action = null;
    // Added for NullPointException
    if (invocation != null)
        action = invocation.getAction();
    return freemarkerManager.buildTemplateModel(stack, action, servletContext, request, response, wrapper);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ValueStack(com.opensymphony.xwork2.util.ValueStack) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 18 with OgnlValueStack

use of com.opensymphony.xwork2.ognl.OgnlValueStack in project struts by apache.

the class TagModelTest method testGetWriter.

public void testGetWriter() throws Exception {
    OgnlValueStack stack = (OgnlValueStack) ActionContext.getContext().getValueStack();
    final InternalBean bean = new InternalBean(stack);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    Map params = new LinkedHashMap();
    // Try to test out the commons Freemarker's Template Model
    // TemplateBooleanModel
    params.put("property1", new TemplateBooleanModel() {

        public boolean getAsBoolean() throws TemplateModelException {
            return true;
        }
    });
    params.put("property2", new TemplateBooleanModel() {

        public boolean getAsBoolean() throws TemplateModelException {
            return false;
        }
    });
    // TemplateScalarModel
    params.put("property3", new TemplateScalarModel() {

        public String getAsString() throws TemplateModelException {
            return "toby";
        }
    });
    params.put("property4", new TemplateScalarModel() {

        public String getAsString() throws TemplateModelException {
            return "phil";
        }
    });
    // TemplateNumberModel
    params.put("property5", new TemplateNumberModel() {

        public Number getAsNumber() throws TemplateModelException {
            return new Integer("10");
        }
    });
    params.put("property6", new TemplateNumberModel() {

        public Number getAsNumber() throws TemplateModelException {
            return new Float("1.1");
        }
    });
    // TemplateHashModel
    params.put("property7", new TemplateHashModel() {

        public TemplateModel get(String arg0) throws TemplateModelException {
            return null;
        }

        public boolean isEmpty() throws TemplateModelException {
            return true;
        }
    });
    // TemplateSequenceModel
    params.put("property8", new TemplateSequenceModel() {

        public TemplateModel get(int arg0) throws TemplateModelException {
            return null;
        }

        public int size() throws TemplateModelException {
            return 0;
        }
    });
    // TemplateCollectionModel
    params.put("property9", new TemplateCollectionModel() {

        public TemplateModelIterator iterator() throws TemplateModelException {
            return new TemplateModelIterator() {

                private Iterator i;

                {
                    i = Collections.EMPTY_LIST.iterator();
                }

                public boolean hasNext() throws TemplateModelException {
                    return i.hasNext();
                }

                public TemplateModel next() throws TemplateModelException {
                    return (TemplateModel) i.next();
                }
            };
        }
    });
    // AdapterTemplateModel
    params.put("property10", new AdapterTemplateModel() {

        public Object getAdaptedObject(Class arg0) {
            return ADAPTER_TEMPLATE_MODEL_CONTAINED_OBJECT;
        }
    });
    // WrapperTemplateModel
    params.put("property11", new WrapperTemplateModel() {

        public Object getWrappedObject() {
            return WRAPPING_TEMPLATE_MODEL_CONTAINED_OBJECT;
        }
    });
    TagModel tagModel = new TagModel(stack, request, response) {

        protected Component getBean() {
            return bean;
        }
    };
    tagModel.getWriter(new StringWriter(), params);
    assertNotNull(bean);
    assertEquals(bean.getProperty1(), true);
    assertEquals(bean.getProperty2(), false);
    assertEquals(bean.getProperty3(), "toby");
    assertEquals(bean.getProperty4(), "phil");
    assertEquals(bean.getProperty5(), new Integer(10));
    assertEquals(bean.getProperty6(), new Float(1.1));
    assertNotNull(bean.getProperty7());
    assertTrue(bean.getProperty7() instanceof Map);
    assertNotNull(bean.getProperty8());
    assertTrue(bean.getProperty8() instanceof Collection);
    assertNotNull(bean.getProperty9());
    assertTrue(bean.getProperty9() instanceof Collection);
    assertEquals(bean.getProperty10(), ADAPTER_TEMPLATE_MODEL_CONTAINED_OBJECT);
    assertEquals(bean.getProperty11(), WRAPPING_TEMPLATE_MODEL_CONTAINED_OBJECT);
}
Also used : OgnlValueStack(com.opensymphony.xwork2.ognl.OgnlValueStack) TemplateModelException(freemarker.template.TemplateModelException) TemplateSequenceModel(freemarker.template.TemplateSequenceModel) TemplateModelIterator(freemarker.template.TemplateModelIterator) WrapperTemplateModel(freemarker.ext.util.WrapperTemplateModel) AdapterTemplateModel(freemarker.template.AdapterTemplateModel) TemplateModel(freemarker.template.TemplateModel) LinkedHashMap(java.util.LinkedHashMap) TemplateHashModel(freemarker.template.TemplateHashModel) StringWriter(java.io.StringWriter) Iterator(java.util.Iterator) TemplateModelIterator(freemarker.template.TemplateModelIterator) TemplateCollectionModel(freemarker.template.TemplateCollectionModel) WrapperTemplateModel(freemarker.ext.util.WrapperTemplateModel) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) TemplateBooleanModel(freemarker.template.TemplateBooleanModel) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) TemplateNumberModel(freemarker.template.TemplateNumberModel) AdapterTemplateModel(freemarker.template.AdapterTemplateModel) Collection(java.util.Collection) TemplateScalarModel(freemarker.template.TemplateScalarModel) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 19 with OgnlValueStack

use of com.opensymphony.xwork2.ognl.OgnlValueStack in project struts by apache.

the class OgnlValueStack method readResolve.

private Object readResolve() {
    // TODO: this should be done better
    ActionContext ac = ActionContext.getContext();
    Container cont = ac.getContainer();
    XWorkConverter xworkConverter = cont.getInstance(XWorkConverter.class);
    CompoundRootAccessor accessor = (CompoundRootAccessor) cont.getInstance(PropertyAccessor.class, CompoundRoot.class.getName());
    TextProvider prov = cont.getInstance(TextProvider.class, "system");
    final boolean allowStaticMethod = BooleanUtils.toBoolean(cont.getInstance(String.class, StrutsConstants.STRUTS_ALLOW_STATIC_METHOD_ACCESS));
    final boolean allowStaticField = BooleanUtils.toBoolean(cont.getInstance(String.class, StrutsConstants.STRUTS_ALLOW_STATIC_FIELD_ACCESS));
    OgnlValueStack aStack = new OgnlValueStack(xworkConverter, accessor, prov, allowStaticMethod, allowStaticField);
    aStack.setOgnlUtil(cont.getInstance(OgnlUtil.class));
    aStack.setRoot(xworkConverter, accessor, this.root, allowStaticMethod, allowStaticField);
    return aStack;
}
Also used : Container(com.opensymphony.xwork2.inject.Container) CompoundRootAccessor(com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor) XWorkConverter(com.opensymphony.xwork2.conversion.impl.XWorkConverter) ActionContext(com.opensymphony.xwork2.ActionContext) TextProvider(com.opensymphony.xwork2.TextProvider)

Example 20 with OgnlValueStack

use of com.opensymphony.xwork2.ognl.OgnlValueStack in project struts by apache.

the class PortletFreemarkerResult method createModel.

/**
 * <p>
 * Build the instance of the ScopesHashModel, including JspTagLib support
 * </p>
 *
 * <p>Objects added to the model are:</p>
 *
 * <ul>
 * <li>Application - servlet context attributes hash model
 * <li>JspTaglibs - jsp tag lib factory model
 * <li>Request - request attributes hash model
 * <li>Session - session attributes hash model
 * <li>request - the HttpServletRequst object for direct access
 * <li>response - the HttpServletResponse object for direct access
 * <li>stack - the OgnLValueStack instance for direct access
 * <li>ognl - the instance of the OgnlTool
 * <li>action - the action itself
 * <li>exception - optional : the JSP or Servlet exception as per the
 * servlet spec (for JSP Exception pages)
 * <li>struts - instance of the StrutsUtil class
 * </ul>
 *
 * @return freemarker template model
 *
 * @throws TemplateModelException in case of template model errors
 */
protected TemplateModel createModel() throws TemplateModelException {
    ServletContext servletContext = ServletActionContext.getServletContext();
    HttpServletRequest request = ServletActionContext.getRequest();
    HttpServletResponse response = ServletActionContext.getResponse();
    ValueStack stack = ServletActionContext.getContext().getValueStack();
    return freemarkerManager.buildTemplateModel(stack, invocation.getAction(), servletContext, request, response, wrapper);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ValueStack(com.opensymphony.xwork2.util.ValueStack) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Aggregations

Foo (com.opensymphony.xwork2.util.Foo)14 ValueStack (com.opensymphony.xwork2.util.ValueStack)6 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)5 OgnlException (ognl.OgnlException)5 StrutsException (org.apache.struts2.StrutsException)5 OgnlValueStack (com.opensymphony.xwork2.ognl.OgnlValueStack)4 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 ParseException (ognl.ParseException)4 TextProvider (com.opensymphony.xwork2.TextProvider)2 XWorkConverter (com.opensymphony.xwork2.conversion.impl.XWorkConverter)2 WrapperTemplateModel (freemarker.ext.util.WrapperTemplateModel)2 AdapterTemplateModel (freemarker.template.AdapterTemplateModel)2 TemplateBooleanModel (freemarker.template.TemplateBooleanModel)2 TemplateCollectionModel (freemarker.template.TemplateCollectionModel)2 TemplateHashModel (freemarker.template.TemplateHashModel)2 TemplateModel (freemarker.template.TemplateModel)2 TemplateModelException (freemarker.template.TemplateModelException)2 TemplateModelIterator (freemarker.template.TemplateModelIterator)2 TemplateNumberModel (freemarker.template.TemplateNumberModel)2