Search in sources :

Example 41 with PageContext

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

the class BindTagTests method bindTagWithFieldButWithoutErrorsInstanceAndHtmlEscaping.

@Test
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);
    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)

Example 42 with PageContext

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

the class BindTagTests method bindTagWithIndexedPropertiesAndCustomEditor.

@Test
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]");
    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("array[0]".equals(status.getExpression())).as("Correct expression").isTrue();
    // because of the custom editor getValue() should return a String
    boolean condition = status.getValue() instanceof String;
    assertThat(condition).as("Value is TestBean").isTrue();
    assertThat("something".equals(status.getValue())).as("Correct value").isTrue();
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) Errors(org.springframework.validation.Errors) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) PageContext(jakarta.servlet.jsp.PageContext) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) DataBinder(org.springframework.validation.DataBinder) BindStatus(org.springframework.web.servlet.support.BindStatus) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.jupiter.api.Test)

Example 43 with PageContext

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

the class BindTagTests method nestingInFormTag.

/**
 * SPR-4022
 */
@SuppressWarnings("serial")
@Test
void nestingInFormTag() throws JspException {
    PageContext pc = createPageContext();
    TestBean tb = new TestBean();
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    ServletRequestDataBinder binder = new ServletRequestDataBinder(tb, "tb");
    CustomDateEditor l = new CustomDateEditor(df, true);
    binder.registerCustomEditor(Date.class, l);
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", binder.getBindingResult());
    FormTag formTag = new FormTag() {

        @Override
        protected TagWriter createTagWriter() {
            return new TagWriter(new StringWriter());
        }
    };
    String action = "/form.html";
    String commandName = "tb";
    String name = "formName";
    String enctype = "my/enctype";
    String method = "POST";
    String onsubmit = "onsubmit";
    String onreset = "onreset";
    String cssClass = "myClass";
    String cssStyle = "myStyle";
    String acceptCharset = "iso-8859-1";
    formTag.setName(name);
    formTag.setCssClass(cssClass);
    formTag.setCssStyle(cssStyle);
    formTag.setAction(action);
    formTag.setModelAttribute(commandName);
    formTag.setEnctype(enctype);
    formTag.setMethod(method);
    formTag.setOnsubmit(onsubmit);
    formTag.setOnreset(onreset);
    formTag.setAcceptCharset(acceptCharset);
    formTag.setPageContext(pc);
    formTag.doStartTag();
    BindTag bindTag1 = new BindTag();
    bindTag1.setPageContext(pc);
    bindTag1.setPath("date");
    bindTag1.doStartTag();
    bindTag1.doEndTag();
    BindTag bindTag2 = new BindTag();
    bindTag2.setPageContext(pc);
    bindTag2.setPath("tb.date");
    bindTag2.doStartTag();
    bindTag2.doEndTag();
    BindTag bindTag3 = new BindTag();
    bindTag3.setPageContext(pc);
    bindTag3.setPath("tb");
    bindTag3.doStartTag();
    bindTag3.doEndTag();
    formTag.doEndTag();
}
Also used : FormTag(org.springframework.web.servlet.tags.form.FormTag) StringWriter(java.io.StringWriter) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) CustomDateEditor(org.springframework.beans.propertyeditors.CustomDateEditor) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) TagWriter(org.springframework.web.servlet.tags.form.TagWriter) PageContext(jakarta.servlet.jsp.PageContext) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.jupiter.api.Test)

Example 44 with PageContext

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

the class BindTagTests method transformTagOutsideBindTag.

@Test
void transformTagOutsideBindTag() throws JspException {
    // first set up the pagecontext and the bean
    PageContext pc = createPageContext();
    TestBean tb = new TestBean();
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    ServletRequestDataBinder binder = new ServletRequestDataBinder(tb, "tb");
    CustomDateEditor l = new CustomDateEditor(df, true);
    binder.registerCustomEditor(Date.class, l);
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", binder.getBindingResult());
    // now try to execute the tag outside a bindtag
    TransformTag transform = new TransformTag();
    transform.setPageContext(pc);
    transform.setVar("var");
    transform.setValue("bla");
    assertThatExceptionOfType(JspException.class).as("executed outside BindTag").isThrownBy(transform::doStartTag);
    // now try to execute the tag outside a bindtag, but inside a messageTag
    MessageTag message = new MessageTag();
    message.setPageContext(pc);
    transform = new TransformTag();
    transform.setPageContext(pc);
    transform.setVar("var");
    transform.setValue("bla");
    transform.setParent(message);
    assertThatExceptionOfType(JspException.class).as("executed outside BindTag and inside messagetag").isThrownBy(transform::doStartTag);
}
Also used : NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) CustomDateEditor(org.springframework.beans.propertyeditors.CustomDateEditor) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) PageContext(jakarta.servlet.jsp.PageContext) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.jupiter.api.Test)

Example 45 with PageContext

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

the class BindTagTests method bindTagWithNestedFieldErrors.

@Test
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");
    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("spouse.name".equals(status.getExpression())).as("Correct expression").isTrue();
    assertThat("name2".equals(status.getValue())).as("Correct value").isTrue();
    assertThat("name2".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("code1".equals(status.getErrorCode())).as("Correct errorCode").isTrue();
    assertThat("message1".equals(status.getErrorMessage())).as("Correct errorMessage").isTrue();
    assertThat("message1".equals(status.getErrorMessagesAsString(" - "))).as("Correct errorMessagesAsString").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)

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