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");
}
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();
}
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();
}
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);
}
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();
}
Aggregations