Search in sources :

Example 6 with JspException

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

the class UrlTag method replaceUriTemplateParams.

/**
 * Replace template markers in the URL matching available parameters. The
 * name of matched parameters are added to the used parameters set.
 * <p>Parameter values are URL encoded.
 * @param uri the URL with template parameters to replace
 * @param params parameters used to replace template markers
 * @param usedParams set of template parameter names that have been replaced
 * @return the URL with template parameters replaced
 */
protected String replaceUriTemplateParams(String uri, List<Param> params, Set<String> usedParams) throws JspException {
    String encoding = this.pageContext.getResponse().getCharacterEncoding();
    for (Param param : params) {
        String template = URL_TEMPLATE_DELIMITER_PREFIX + param.getName() + URL_TEMPLATE_DELIMITER_SUFFIX;
        if (uri.contains(template)) {
            usedParams.add(param.getName());
            String value = param.getValue();
            try {
                uri = StringUtils.replace(uri, template, (value != null ? UriUtils.encodePath(value, encoding) : ""));
            } catch (UnsupportedCharsetException ex) {
                throw new JspException(ex);
            }
        } else {
            template = URL_TEMPLATE_DELIMITER_PREFIX + '/' + param.getName() + URL_TEMPLATE_DELIMITER_SUFFIX;
            if (uri.contains(template)) {
                usedParams.add(param.getName());
                String value = param.getValue();
                try {
                    uri = StringUtils.replace(uri, template, (value != null ? UriUtils.encodePathSegment(value, encoding) : ""));
                } catch (UnsupportedCharsetException ex) {
                    throw new JspException(ex);
                }
            }
        }
    }
    return uri;
}
Also used : JspException(jakarta.servlet.jsp.JspException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException)

Example 7 with JspException

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

the class EvalTag method doEndTag.

@Override
public int doEndTag() throws JspException {
    EvaluationContext evaluationContext = (EvaluationContext) this.pageContext.getAttribute(EVALUATION_CONTEXT_PAGE_ATTRIBUTE);
    if (evaluationContext == null) {
        evaluationContext = createEvaluationContext(this.pageContext);
        this.pageContext.setAttribute(EVALUATION_CONTEXT_PAGE_ATTRIBUTE, evaluationContext);
    }
    if (this.var != null) {
        Object result = (this.expression != null ? this.expression.getValue(evaluationContext) : null);
        this.pageContext.setAttribute(this.var, result, this.scope);
    } else {
        try {
            String result = (this.expression != null ? this.expression.getValue(evaluationContext, String.class) : null);
            result = ObjectUtils.getDisplayString(result);
            result = htmlEscape(result);
            result = (this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(result) : result);
            this.pageContext.getOut().print(result);
        } catch (IOException ex) {
            throw new JspException(ex);
        }
    }
    return EVAL_PAGE;
}
Also used : JspException(jakarta.servlet.jsp.JspException) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) IOException(java.io.IOException)

Example 8 with JspException

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

the class RequestContextAwareTag method doStartTag.

/**
 * Create and expose the current RequestContext.
 * Delegates to {@link #doStartTagInternal()} for actual work.
 * @see #REQUEST_CONTEXT_PAGE_ATTRIBUTE
 * @see org.springframework.web.servlet.support.JspAwareRequestContext
 */
@Override
public final int doStartTag() throws JspException {
    try {
        this.requestContext = (RequestContext) this.pageContext.getAttribute(REQUEST_CONTEXT_PAGE_ATTRIBUTE);
        if (this.requestContext == null) {
            this.requestContext = new JspAwareRequestContext(this.pageContext);
            this.pageContext.setAttribute(REQUEST_CONTEXT_PAGE_ATTRIBUTE, this.requestContext);
        }
        return doStartTagInternal();
    } catch (JspException | RuntimeException ex) {
        logger.error(ex.getMessage(), ex);
        throw ex;
    } catch (Exception ex) {
        logger.error(ex.getMessage(), ex);
        throw new JspTagException(ex.getMessage());
    }
}
Also used : JspException(jakarta.servlet.jsp.JspException) JspAwareRequestContext(org.springframework.web.servlet.support.JspAwareRequestContext) JspTagException(jakarta.servlet.jsp.JspTagException) JspException(jakarta.servlet.jsp.JspException) JspTagException(jakarta.servlet.jsp.JspTagException)

Example 9 with JspException

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

the class TransformTag method doStartTagInternal.

@Override
protected final int doStartTagInternal() throws JspException {
    if (this.value != null) {
        // Find the containing EditorAwareTag (e.g. BindTag), if applicable.
        EditorAwareTag tag = (EditorAwareTag) TagSupport.findAncestorWithClass(this, EditorAwareTag.class);
        if (tag == null) {
            throw new JspException("TransformTag can only be used within EditorAwareTag (e.g. BindTag)");
        }
        // OK, let's obtain the editor...
        String result = null;
        PropertyEditor editor = tag.getEditor();
        if (editor != null) {
            // If an editor was found, edit the value.
            editor.setValue(this.value);
            result = editor.getAsText();
        } else {
            // Else, just do a toString.
            result = this.value.toString();
        }
        result = htmlEscape(result);
        if (this.var != null) {
            this.pageContext.setAttribute(this.var, result, TagUtils.getScope(this.scope));
        } else {
            try {
                // Else, just print it out.
                this.pageContext.getOut().print(result);
            } catch (IOException ex) {
                throw new JspException(ex);
            }
        }
    }
    return SKIP_BODY;
}
Also used : JspException(jakarta.servlet.jsp.JspException) PropertyEditor(java.beans.PropertyEditor) IOException(java.io.IOException)

Example 10 with JspException

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

the class BindTagTests method bindTagWithoutBean.

@Test
void bindTagWithoutBean() throws JspException {
    PageContext pc = createPageContext();
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb");
    assertThatExceptionOfType(JspException.class).isThrownBy(tag::doStartTag);
}
Also used : JspException(jakarta.servlet.jsp.JspException) PageContext(jakarta.servlet.jsp.PageContext) Test(org.junit.jupiter.api.Test)

Aggregations

JspException (jakarta.servlet.jsp.JspException)10 IOException (java.io.IOException)4 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)2 ServletRequest (jakarta.servlet.ServletRequest)1 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 JspTagException (jakarta.servlet.jsp.JspTagException)1 PageContext (jakarta.servlet.jsp.PageContext)1 Tag (jakarta.servlet.jsp.tagext.Tag)1 PropertyEditor (java.beans.PropertyEditor)1 Test (org.junit.jupiter.api.Test)1 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)1 BeansException (org.springframework.beans.BeansException)1 EvaluationContext (org.springframework.expression.EvaluationContext)1 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)1 Authentication (org.springframework.security.core.Authentication)1 SecurityContext (org.springframework.security.core.context.SecurityContext)1 JspAwareRequestContext (org.springframework.web.servlet.support.JspAwareRequestContext)1 RequestDataValueProcessor (org.springframework.web.servlet.support.RequestDataValueProcessor)1