| 1234567891011121314151617181920212223242526272829303132333435363738 |
- package commons.web.mvc;
- import jakarta.servlet.jsp.JspException;
- import jakarta.servlet.jsp.PageContext;
- import jakarta.servlet.jsp.tagext.Tag;
- public class ExpEval {
- public static Object evalObject(String attrName, String attrValue, Tag tagObject, PageContext pageContext) throws JspException {
- return evalExpression(attrName, attrValue, Object.class, tagObject, pageContext);
- }
- public static String evalString(String attrName, String attrValue, Tag tagObject, PageContext pageContext) throws JspException {
- return (String) evalExpression(attrName, attrValue, String.class, tagObject, pageContext);
- }
- public static Integer evalInteger(String attrName, String attrValue, Tag tagObject, PageContext pageContext) throws JspException {
- return (Integer) evalExpression(attrName, attrValue, Integer.class, tagObject, pageContext);
- }
- public static Boolean evalBoolean(String attrName, String attrValue, Tag tagObject, PageContext pageContext) throws JspException {
- return (Boolean) evalExpression(attrName, attrValue, Boolean.class, tagObject, pageContext);
- }
- private static Object evalExpression(String attrName, String attrValue, Class<?> expectedType, Tag tagObject, PageContext pageContext) throws JspException {
- if (attrValue == null) return null;
- if (attrValue.indexOf("${") == -1) {
- return attrValue;
- }
- try {
- jakarta.el.ELProcessor elProcessor = new jakarta.el.ELProcessor();
- elProcessor.defineBean("pageContext", pageContext);
- elProcessor.defineBean("param", pageContext.getRequest().getParameterMap());
- return elProcessor.eval(attrValue);
- } catch (Exception e) {
- throw new JspException("Error evaluating expression: " + attrValue, e);
- }
- }
- }
|