Search in sources :

Example 31 with PageContext

use of jakarta.servlet.jsp.PageContext in project tomcat by apache.

the class JspFactoryImpl method internalGetPageContext.

private PageContext internalGetPageContext(Servlet servlet, ServletRequest request, ServletResponse response, String errorPageURL, boolean needsSession, int bufferSize, boolean autoflush) {
    PageContext pc;
    if (poolSize > 0) {
        PageContextPool pool = localPool.get();
        if (pool == null) {
            pool = new PageContextPool(poolSize);
            localPool.set(pool);
        }
        pc = pool.get();
        if (pc == null) {
            pc = new PageContextImpl();
        }
    } else {
        pc = new PageContextImpl();
    }
    try {
        pc.initialize(servlet, request, response, errorPageURL, needsSession, bufferSize, autoflush);
    } catch (IOException ioe) {
    // Implementation never throws IOE but can't change the signature
    // since it is part of the JSP API
    }
    return pc;
}
Also used : PageContext(jakarta.servlet.jsp.PageContext) IOException(java.io.IOException)

Example 32 with PageContext

use of jakarta.servlet.jsp.PageContext in project spring-framework by spring-projects.

the class TagIdGeneratorTests method assertNextId.

private void assertNextId() {
    PageContext pageContext = new MockPageContext();
    assertThat(TagIdGenerator.nextId("foo", pageContext)).isEqualTo("foo1");
    assertThat(TagIdGenerator.nextId("foo", pageContext)).isEqualTo("foo2");
    assertThat(TagIdGenerator.nextId("foo", pageContext)).isEqualTo("foo3");
    assertThat(TagIdGenerator.nextId("foo", pageContext)).isEqualTo("foo4");
    assertThat(TagIdGenerator.nextId("bar", pageContext)).isEqualTo("bar1");
}
Also used : MockPageContext(org.springframework.web.testfixture.servlet.MockPageContext) PageContext(jakarta.servlet.jsp.PageContext) MockPageContext(org.springframework.web.testfixture.servlet.MockPageContext)

Example 33 with PageContext

use of jakarta.servlet.jsp.PageContext in project spring-framework by spring-projects.

the class BindTagTests method nestedPathDoStartTagInternalWithNesting.

@Test
void nestedPathDoStartTagInternalWithNesting() throws JspException {
    PageContext pc = createPageContext();
    NestedPathTag tag = new NestedPathTag();
    tag.setPath("foo");
    tag.setPageContext(pc);
    tag.doStartTag();
    assertThat(pc.getAttribute(NestedPathTag.NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE)).isEqualTo("foo.");
    NestedPathTag anotherTag = new NestedPathTag();
    anotherTag.setPageContext(pc);
    anotherTag.setPath("bar");
    anotherTag.doStartTag();
    assertThat(pc.getAttribute(NestedPathTag.NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE)).isEqualTo("foo.bar.");
    NestedPathTag yetAnotherTag = new NestedPathTag();
    yetAnotherTag.setPageContext(pc);
    yetAnotherTag.setPath("boo");
    yetAnotherTag.doStartTag();
    assertThat(pc.getAttribute(NestedPathTag.NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE)).isEqualTo("foo.bar.boo.");
    yetAnotherTag.doEndTag();
    NestedPathTag andAnotherTag = new NestedPathTag();
    andAnotherTag.setPageContext(pc);
    andAnotherTag.setPath("boo2");
    andAnotherTag.doStartTag();
    assertThat(pc.getAttribute(NestedPathTag.NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE)).isEqualTo("foo.bar.boo2.");
}
Also used : PageContext(jakarta.servlet.jsp.PageContext) Test(org.junit.jupiter.api.Test)

Example 34 with PageContext

use of jakarta.servlet.jsp.PageContext in project spring-framework by spring-projects.

the class BindTagTests method bindTagWithFieldErrors.

@Test
void bindTagWithFieldErrors() throws JspException {
    PageContext pc = createPageContext();
    TestBean tb = new TestBean();
    tb.setName("name1");
    Errors errors = new ServletRequestDataBinder(tb, "tb").getBindingResult();
    errors.rejectValue("name", "code1", "message & 1");
    errors.rejectValue("name", "code2", "message2");
    errors.rejectValue("age", "code2", "message2");
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb.name");
    tag.setHtmlEscape(true);
    assertThat(tag.doStartTag() == Tag.EVAL_BODY_INCLUDE).as("Correct doStartTag return value").isTrue();
    BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertThat(status != null).as("Has status variable").isTrue();
    assertThat("name".equals(status.getExpression())).as("Correct expression").isTrue();
    assertThat("name1".equals(status.getValue())).as("Correct value").isTrue();
    assertThat("name1".equals(status.getDisplayValue())).as("Correct displayValue").isTrue();
    assertThat(status.isError()).as("Correct isError").isTrue();
    assertThat(status.getErrorCodes().length == 2).as("Correct errorCodes").isTrue();
    assertThat(status.getErrorMessages().length == 2).as("Correct errorMessages").isTrue();
    assertThat("code1".equals(status.getErrorCode())).as("Correct errorCode").isTrue();
    assertThat("code1".equals(status.getErrorCodes()[0])).as("Correct errorCode").isTrue();
    assertThat("code2".equals(status.getErrorCodes()[1])).as("Correct errorCode").isTrue();
    assertThat("message & 1".equals(status.getErrorMessage())).as("Correct errorMessage").isTrue();
    assertThat("message & 1".equals(status.getErrorMessages()[0])).as("Correct errorMessage").isTrue();
    assertThat("message2".equals(status.getErrorMessages()[1])).as("Correct errorMessage").isTrue();
    assertThat("message & 1 - message2".equals(status.getErrorMessagesAsString(" - "))).as("Correct errorMessagesAsString").isTrue();
    tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb.age");
    assertThat(tag.doStartTag() == Tag.EVAL_BODY_INCLUDE).as("Correct doStartTag return value").isTrue();
    status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertThat(status != null).as("Has status variable").isTrue();
    assertThat("age".equals(status.getExpression())).as("Correct expression").isTrue();
    assertThat(Integer.valueOf(0).equals(status.getValue())).as("Correct value").isTrue();
    assertThat("0".equals(status.getDisplayValue())).as("Correct displayValue").isTrue();
    assertThat(status.isError()).as("Correct isError").isTrue();
    assertThat(status.getErrorCodes().length == 1).as("Correct errorCodes").isTrue();
    assertThat(status.getErrorMessages().length == 1).as("Correct errorMessages").isTrue();
    assertThat("code2".equals(status.getErrorCode())).as("Correct errorCode").isTrue();
    assertThat("message2".equals(status.getErrorMessage())).as("Correct errorMessage").isTrue();
    assertThat("message2".equals(status.getErrorMessagesAsString(" - "))).as("Correct errorMessagesAsString").isTrue();
    tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb.*");
    assertThat(tag.doStartTag() == Tag.EVAL_BODY_INCLUDE).as("Correct doStartTag return value").isTrue();
    status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertThat(status != null).as("Has status variable").isTrue();
    assertThat("*".equals(status.getExpression())).as("Correct expression").isTrue();
    assertThat(status.getValue() == null).as("Correct value").isTrue();
    assertThat("".equals(status.getDisplayValue())).as("Correct displayValue").isTrue();
    assertThat(status.isError()).as("Correct isError").isTrue();
    assertThat(status.getErrorCodes().length == 3).as("Correct errorCodes").isTrue();
    assertThat(status.getErrorMessages().length == 3).as("Correct errorMessages").isTrue();
    assertThat("code1".equals(status.getErrorCode())).as("Correct errorCode").isTrue();
    assertThat("code1".equals(status.getErrorCodes()[0])).as("Correct errorCode").isTrue();
    assertThat("code2".equals(status.getErrorCodes()[1])).as("Correct errorCode").isTrue();
    assertThat("code2".equals(status.getErrorCodes()[2])).as("Correct errorCode").isTrue();
    assertThat("message & 1".equals(status.getErrorMessage())).as("Correct errorMessage").isTrue();
    assertThat("message & 1".equals(status.getErrorMessages()[0])).as("Correct errorMessage").isTrue();
    assertThat("message2".equals(status.getErrorMessages()[1])).as("Correct errorMessage").isTrue();
    assertThat("message2".equals(status.getErrorMessages()[2])).as("Correct errorMessage").isTrue();
}
Also used : Errors(org.springframework.validation.Errors) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) PageContext(jakarta.servlet.jsp.PageContext) BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.jupiter.api.Test)

Example 35 with PageContext

use of jakarta.servlet.jsp.PageContext in project spring-framework by spring-projects.

the class BindTagTests method bindTagWithFieldButWithoutErrorsInstance.

@Test
void bindTagWithFieldButWithoutErrorsInstance() throws JspException {
    PageContext pc = createPageContext();
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb.name");
    pc.getRequest().setAttribute("tb", new TestBean("juergen&eva", 99));
    tag.doStartTag();
    BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertThat(status.getExpression()).isEqualTo("name");
    assertThat(status.getValue()).isEqualTo("juergen&eva");
}
Also used : NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) PageContext(jakarta.servlet.jsp.PageContext) BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.jupiter.api.Test)

Aggregations

PageContext (jakarta.servlet.jsp.PageContext)70 Test (org.junit.jupiter.api.Test)62 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)28 NestedTestBean (org.springframework.beans.testfixture.beans.NestedTestBean)27 TestBean (org.springframework.beans.testfixture.beans.TestBean)27 ServletRequestDataBinder (org.springframework.web.bind.ServletRequestDataBinder)22 BindStatus (org.springframework.web.servlet.support.BindStatus)19 Errors (org.springframework.validation.Errors)17 DateFormat (java.text.DateFormat)6 SimpleDateFormat (java.text.SimpleDateFormat)6 CustomDateEditor (org.springframework.beans.propertyeditors.CustomDateEditor)6 JspContext (jakarta.servlet.jsp.JspContext)4 DefaultMessageSourceResolvable (org.springframework.context.support.DefaultMessageSourceResolvable)3 BeforeEach (org.junit.jupiter.api.BeforeEach)2 MessageSourceResolvable (org.springframework.context.MessageSourceResolvable)2 RequestContext (org.springframework.web.servlet.support.RequestContext)2 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 JspException (jakarta.servlet.jsp.JspException)1 FeatureDescriptor (java.beans.FeatureDescriptor)1