Java Code Examples for javax.servlet.jsp.PageContext#getAttribute()

The following examples show how to use javax.servlet.jsp.PageContext#getAttribute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BindTagTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void bindTagWithToStringAndHtmlEscaping() throws JspException {
	PageContext pc = createPageContext();
	BindTag tag = new BindTag();
	tag.setPageContext(pc);
	tag.setPath("tb.doctor");
	tag.setHtmlEscape(true);
	TestBean tb = new TestBean("somebody", 99);
	NestedTestBean ntb = new NestedTestBean("juergen&eva");
	tb.setDoctor(ntb);
	pc.getRequest().setAttribute("tb", tb);
	tag.doStartTag();
	BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
	assertEquals("doctor", status.getExpression());
	assertTrue(status.getValue() instanceof NestedTestBean);
	assertTrue(status.getDisplayValue().indexOf("juergen&eva") != -1);
}
 
Example 2
Source File: BindTagTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void bindTagWithoutErrors() throws JspException {
	PageContext pc = createPageContext();
	Errors errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult();
	pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
	BindTag tag = new BindTag();
	tag.setPageContext(pc);
	tag.setPath("tb");
	assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
	BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
	assertTrue("Has status variable", status != null);
	assertTrue("Correct expression", status.getExpression() == null);
	assertTrue("Correct value", status.getValue() == null);
	assertTrue("Correct displayValue", "".equals(status.getDisplayValue()));
	assertTrue("Correct isError", !status.isError());
	assertTrue("Correct errorCodes", status.getErrorCodes().length == 0);
	assertTrue("Correct errorMessages", status.getErrorMessages().length == 0);
	assertTrue("Correct errorCode", "".equals(status.getErrorCode()));
	assertTrue("Correct errorMessage", "".equals(status.getErrorMessage()));
	assertTrue("Correct errorMessagesAsString", "".equals(status.getErrorMessagesAsString(",")));
}
 
Example 3
Source File: BindTagTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void bindTagWithToStringAndHtmlEscaping() throws JspException {
	PageContext pc = createPageContext();
	BindTag tag = new BindTag();
	tag.setPageContext(pc);
	tag.setPath("tb.doctor");
	tag.setHtmlEscape(true);
	TestBean tb = new TestBean("somebody", 99);
	NestedTestBean ntb = new NestedTestBean("juergen&eva");
	tb.setDoctor(ntb);
	pc.getRequest().setAttribute("tb", tb);
	tag.doStartTag();
	BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
	assertEquals("doctor", status.getExpression());
	assertTrue(status.getValue() instanceof NestedTestBean);
	assertTrue(status.getDisplayValue().contains("juergen&eva"));
}
 
Example 4
Source File: StoreTag.java    From JspMyAdmin2 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void doTag() throws JspException {
	PageContext pageContext = (PageContext) super.getJspContext();
	MessageReader messageReader = (MessageReader) pageContext
			.getAttribute(Constants.PAGE_CONTEXT_MESSAGES);
	Object temp = null;
	if (scope == null) {
		key = messageReader.getMessage(key);
		pageContext.setAttribute(name, key, PageContext.PAGE_SCOPE);
		return;
	} else if (Constants.COMMAND.equals(scope)) {
		temp = pageContext.getRequest().getAttribute(
				Constants.COMMAND);
		temp = super.getReflectValue(temp, key);
	} else if (Constants.PAGE.equals(scope)) {
		temp = pageContext.getAttribute(key);
	} else if (Constants.REQUEST.equals(scope)) {
		temp = pageContext.getRequest().getAttribute(key);
	}
	if (temp != null) {
		key = messageReader.getMessage(temp.toString());
		if (key != null) {
			pageContext.setAttribute(name, key, PageContext.PAGE_SCOPE);
		}
	}
}
 
Example 5
Source File: BindTagTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void bindTagWithToStringAndHtmlEscaping() throws JspException {
	PageContext pc = createPageContext();
	BindTag tag = new BindTag();
	tag.setPageContext(pc);
	tag.setPath("tb.doctor");
	tag.setHtmlEscape(true);
	TestBean tb = new TestBean("somebody", 99);
	NestedTestBean ntb = new NestedTestBean("juergen&eva");
	tb.setDoctor(ntb);
	pc.getRequest().setAttribute("tb", tb);
	tag.doStartTag();
	BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
	assertEquals("doctor", status.getExpression());
	assertTrue(status.getValue() instanceof NestedTestBean);
	assertTrue(status.getDisplayValue().contains("juergen&eva"));
}
 
Example 6
Source File: BindTagTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void bindTagWithoutErrors() throws JspException {
	PageContext pc = createPageContext();
	Errors errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult();
	pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
	BindTag tag = new BindTag();
	tag.setPageContext(pc);
	tag.setPath("tb");
	assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
	BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
	assertTrue("Has status variable", status != null);
	assertTrue("Correct expression", status.getExpression() == null);
	assertTrue("Correct value", status.getValue() == null);
	assertTrue("Correct displayValue", "".equals(status.getDisplayValue()));
	assertTrue("Correct isError", !status.isError());
	assertTrue("Correct errorCodes", status.getErrorCodes().length == 0);
	assertTrue("Correct errorMessages", status.getErrorMessages().length == 0);
	assertTrue("Correct errorCode", "".equals(status.getErrorCode()));
	assertTrue("Correct errorMessage", "".equals(status.getErrorMessage()));
	assertTrue("Correct errorMessagesAsString", "".equals(status.getErrorMessagesAsString(",")));
}
 
Example 7
Source File: BindTagTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void bindTagWithoutErrors() throws JspException {
	PageContext pc = createPageContext();
	Errors errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult();
	pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
	BindTag tag = new BindTag();
	tag.setPageContext(pc);
	tag.setPath("tb");
	assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
	BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
	assertTrue("Has status variable", status != null);
	assertTrue("Correct expression", status.getExpression() == null);
	assertTrue("Correct value", status.getValue() == null);
	assertTrue("Correct displayValue", "".equals(status.getDisplayValue()));
	assertTrue("Correct isError", !status.isError());
	assertTrue("Correct errorCodes", status.getErrorCodes().length == 0);
	assertTrue("Correct errorMessages", status.getErrorMessages().length == 0);
	assertTrue("Correct errorCode", "".equals(status.getErrorCode()));
	assertTrue("Correct errorMessage", "".equals(status.getErrorMessage()));
	assertTrue("Correct errorMessagesAsString", "".equals(status.getErrorMessagesAsString(",")));
}
 
Example 8
Source File: BindTagTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void nestedPathWithBindTagWithIgnoreNestedPath() throws JspException {
	PageContext pc = createPageContext();
	Errors errors = new ServletRequestDataBinder(new TestBean(), "tb2").getBindingResult();
	pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb2", errors);

	NestedPathTag tag = new NestedPathTag();
	tag.setPath("tb");
	tag.setPageContext(pc);
	tag.doStartTag();

	BindTag bindTag = new BindTag();
	bindTag.setPageContext(pc);
	bindTag.setIgnoreNestedPath(true);
	bindTag.setPath("tb2.name");

	assertTrue("Correct doStartTag return value", bindTag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
	BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
	assertTrue("Has status variable", status != null);
	assertEquals("tb2.name", status.getPath());
}
 
Example 9
Source File: BindTagTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void bindTagWithIndexedPropertiesAndCustomEditor() throws JspException {
	PageContext pc = createPageContext();
	IndexedTestBean tb = new IndexedTestBean();
	DataBinder binder = new ServletRequestDataBinder(tb, "tb");
	binder.registerCustomEditor(TestBean.class, null, new PropertyEditorSupport() {
		@Override
		public String getAsText() {
			return "something";
		}
	});
	Errors errors = binder.getBindingResult();
	errors.rejectValue("array[0]", "code1", "message1");
	errors.rejectValue("array[0]", "code2", "message2");
	pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);

	BindTag tag = new BindTag();
	tag.setPageContext(pc);
	tag.setPath("tb.array[0]");
	assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
	BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
	assertTrue("Has status variable", status != null);
	assertTrue("Correct expression", "array[0]".equals(status.getExpression()));
	// because of the custom editor getValue() should return a String
	assertTrue("Value is TestBean", status.getValue() instanceof String);
	assertTrue("Correct value", "something".equals(status.getValue()));
}
 
Example 10
Source File: ContextUtils.java    From ontopia with Apache License 2.0 5 votes vote down vote up
/**
 * EXPERIMENTAL: Get the topic map object the context tag is working
 * with. This method will give direct access to the same transaction
 * as the context tag is using.
 *
 * @since 3.2.1
 */
public static TopicMapIF getTopicMap(PageContext pageContext) {
  NavigatorPageIF ctxt = (NavigatorPageIF)
    pageContext.getAttribute(NavigatorApplicationIF.CONTEXT_KEY,
                             PageContext.REQUEST_SCOPE);
  if (ctxt == null)
    throw new OntopiaRuntimeException("Could not find navigator context.");
  return ctxt.getTopicMap();
}
 
Example 11
Source File: ContextUtils.java    From ontopia with Apache License 2.0 5 votes vote down vote up
/**
 * PUBLIC: Sets the value of the named variable to the collection
 * given. The value can be null, which effectively unsets the variable.
 */
public static void setValue(String name, PageContext pageContext,
                            Collection value) {
  NavigatorPageIF ctxt = (NavigatorPageIF)
    pageContext.getAttribute(NavigatorApplicationIF.CONTEXT_KEY,
                             PageContext.REQUEST_SCOPE);
  if (ctxt == null) throw new OntopiaRuntimeException("Could not find navigator context.");
  ctxt.getContextManager().setValue(name, value);
}
 
Example 12
Source File: ContextUtils.java    From ontopia with Apache License 2.0 5 votes vote down vote up
/**
 * PUBLIC: Sets the value of the named variable to a collection
 * consisting only of the single value given.
 */
public static void setSingleValue(String name, PageContext pageContext, 
                                  Object value) {
  NavigatorPageIF ctxt = (NavigatorPageIF)
    pageContext.getAttribute(NavigatorApplicationIF.CONTEXT_KEY,
                             PageContext.REQUEST_SCOPE);
  if (ctxt == null) throw new OntopiaRuntimeException("Could not find navigator context.");
  Collection coll = new ArrayList(1);
  coll.add(value);
  ctxt.getContextManager().setValue(name, coll);
}
 
Example 13
Source File: ImplicitObjectELResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 *
 * Creates the Map that "wraps" request-scoped attributes
 **/
public static Map createRequestScopeMap (PageContext pContext)
{
  final PageContext context = pContext;
  return new EnumeratedMap ()
    {
      public Enumeration enumerateKeys () 
      {
        return context.getAttributeNamesInScope
          (PageContext.REQUEST_SCOPE);
      }

      public Object getValue (Object pKey) 
      {
        if (pKey instanceof String) {
          return context.getAttribute
            ((String) pKey, 
             PageContext.REQUEST_SCOPE);
        }
        else {
          return null;
        }
      }

      public boolean isMutable ()
      {
        return true;
      }
    };
}
 
Example 14
Source File: TagUtils.java    From ontopia with Apache License 2.0 5 votes vote down vote up
/**
 * INTERNAL: Returns the values retrieved from the given variable
 * names or qnames in the order given.
 *
 * @param params - variable names or qnames, separated by whitespaces.
 */
private static List getMultipleValuesAsList(String params, 
                                            PageContext pageContext)
  throws JspTagException {
  log.debug("getMultipleValuesAsList");
  // find parsecontext
  NavigatorPageIF ctxt = (NavigatorPageIF)
    pageContext.getAttribute(NavigatorApplicationIF.CONTEXT_KEY,
                             PageContext.REQUEST_SCOPE);
  ParseContextIF pctxt = (ParseContextIF) ctxt.getDeclarationContext();

  // get the values
  String[] names = StringUtils.split(params);
  List varlist = new ArrayList(names.length);
  for (int i = 0; i < names.length; i++) {
    Collection values;
    
    if (names[i].indexOf(':') != -1) {
      // it's a qname
      try {
        values = Collections.singleton(pctxt.getObject(new QName(names[i])));
      } catch (AntlrWrapException e) {
        throw new JspTagException(e.getException().getMessage() +
                                  " (in action parameter list)");
      }
    } else
      // it's a variable name
      values = InteractionELSupport.extendedGetValue(names[i], pageContext);
    
    varlist.add(values);
  }
  return varlist;
}
 
Example 15
Source File: ImplicitObjectELResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 *
 * Creates the Map that "wraps" session-scoped attributes
 **/
public static Map createSessionScopeMap (PageContext pContext)
{
  final PageContext context = pContext;
  return new EnumeratedMap ()
    {
      public Enumeration enumerateKeys () 
      {
        return context.getAttributeNamesInScope
          (PageContext.SESSION_SCOPE);
      }

      public Object getValue (Object pKey) 
      {
        if (pKey instanceof String) {
          return context.getAttribute
            ((String) pKey, 
             PageContext.SESSION_SCOPE);
        }
        else {
          return null;
        }
      }

      public boolean isMutable ()
      {
        return true;
      }
    };
}
 
Example 16
Source File: ListTagUtil.java    From spacewalk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Locates the current ListCommand
 * @param caller tag calling the method
 * @param ctx caller's page context
 * @return ListCommand if found, otherwise null
 */
public static ListCommand getCurrentCommand(Tag caller, PageContext ctx) {
    ListTag parent = null;
    if (!(caller instanceof ListTag)) {
        parent = (ListTag) BodyTagSupport.findAncestorWithClass(caller, ListTag.class);
    }
    else {
        parent = (ListTag) caller;
    }
    if (parent != null) {
        return (ListCommand) ctx.getAttribute(parent.getUniqueName() + "_cmd");
    }
    return null;
}
 
Example 17
Source File: BindTagTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void bindTagWithFieldButWithoutErrorsInstanceAndHtmlEscaping() throws JspException {
	PageContext pc = createPageContext();
	BindTag tag = new BindTag();
	tag.setPageContext(pc);
	tag.setPath("tb.name");
	tag.setHtmlEscape(true);
	pc.getRequest().setAttribute("tb", new TestBean("juergen&eva", 99));
	tag.doStartTag();
	BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
	assertEquals("name", status.getExpression());
	assertEquals("juergen&amp;eva", status.getValue());
}
 
Example 18
Source File: BindTagTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void bindTagWithNestedFieldErrors() throws JspException {
	PageContext pc = createPageContext();
	TestBean tb = new TestBean();
	tb.setName("name1");
	TestBean spouse = new TestBean();
	spouse.setName("name2");
	tb.setSpouse(spouse);
	Errors errors = new ServletRequestDataBinder(tb, "tb").getBindingResult();
	errors.rejectValue("spouse.name", "code1", "message1");
	pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
	BindTag tag = new BindTag();
	tag.setPageContext(pc);
	tag.setPath("tb.spouse.name");
	assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
	BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
	assertTrue("Has status variable", status != null);
	assertTrue("Correct expression", "spouse.name".equals(status.getExpression()));
	assertTrue("Correct value", "name2".equals(status.getValue()));
	assertTrue("Correct displayValue", "name2".equals(status.getDisplayValue()));
	assertTrue("Correct isError", status.isError());
	assertTrue("Correct errorCodes", status.getErrorCodes().length == 1);
	assertTrue("Correct errorMessages", status.getErrorMessages().length == 1);
	assertTrue("Correct errorCode", "code1".equals(status.getErrorCode()));
	assertTrue("Correct errorMessage", "message1".equals(status.getErrorMessage()));
	assertTrue("Correct errorMessagesAsString", "message1".equals(status.getErrorMessagesAsString(" - ")));
}
 
Example 19
Source File: BindTagTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void nestedPathWithBindTag() throws JspException {
	PageContext pc = createPageContext();
	Errors errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult();
	pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);

	NestedPathTag nestedPathTag = new NestedPathTag();
	nestedPathTag.setPath("tb");
	nestedPathTag.setPageContext(pc);
	nestedPathTag.doStartTag();

	BindTag bindTag = new BindTag();
	bindTag.setPageContext(pc);
	bindTag.setPath("name");

	assertTrue("Correct doStartTag return value", bindTag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
	BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
	assertTrue("Has status variable", status != null);
	assertEquals("tb.name", status.getPath());
	assertEquals("Correct field value", "", status.getDisplayValue());

	BindTag bindTag2 = new BindTag();
	bindTag2.setPageContext(pc);
	bindTag2.setPath("age");

	assertTrue("Correct doStartTag return value", bindTag2.doStartTag() == Tag.EVAL_BODY_INCLUDE);
	BindStatus status2 = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
	assertTrue("Has status variable", status2 != null);
	assertEquals("tb.age", status2.getPath());
	assertEquals("Correct field value", "0", status2.getDisplayValue());

	bindTag2.doEndTag();

	BindStatus status3 = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
	assertSame("Status matches previous status", status, status3);
	assertEquals("tb.name", status.getPath());
	assertEquals("Correct field value", "", status.getDisplayValue());

	bindTag.doEndTag();
	nestedPathTag.doEndTag();
}
 
Example 20
Source File: TagUtils.java    From ontopia with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the name of the action group as an attribute value (residing
 * in the page scope).
 */
public static String getActionGroup(PageContext pageContext) {
  return (String) pageContext.getAttribute(Constants.RA_ACTIONGROUP,
                                           PageContext.REQUEST_SCOPE);
}