Search in sources :

Example 36 with ActionMessages

use of org.apache.struts.action.ActionMessages in project sonarqube by SonarSource.

the class TagUtils method getActionMessages.

/**
     * Retrieves the value from request scope and if it isn't already an
     * <code>ActionMessages</code>, some classes are converted to one.
     *
     * @param pageContext The PageContext for the current page
     * @param paramName   Key for parameter value
     * @return ActionErrors in page context.
     * @throws JspException
     */
public ActionMessages getActionMessages(PageContext pageContext, String paramName) throws JspException {
    ActionMessages am = new ActionMessages();
    Object value = pageContext.findAttribute(paramName);
    if (value != null) {
        try {
            if (value instanceof String) {
                am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage((String) value));
            } else if (value instanceof String[]) {
                String[] keys = (String[]) value;
                for (int i = 0; i < keys.length; i++) {
                    am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(keys[i]));
                }
            } else if (value instanceof ActionErrors) {
                ActionMessages m = (ActionMessages) value;
                am.add(m);
            } else if (value instanceof ActionMessages) {
                am = (ActionMessages) value;
            } else {
                throw new JspException(messages.getMessage("actionMessages.errors", value.getClass().getName()));
            }
        } catch (JspException e) {
            throw e;
        } catch (Exception e) {
            log.warn("Unable to retieve ActionMessage for paramName : " + paramName, e);
        }
    }
    return am;
}
Also used : JspException(javax.servlet.jsp.JspException) ActionMessages(org.apache.struts.action.ActionMessages) ActionMessage(org.apache.struts.action.ActionMessage) ActionErrors(org.apache.struts.action.ActionErrors) JspException(javax.servlet.jsp.JspException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 37 with ActionMessages

use of org.apache.struts.action.ActionMessages in project sonarqube by SonarSource.

the class MessagesPresentTag method condition.

/**
     * Evaluate the condition that is being tested by this particular tag, and
     * return <code>true</code> if there is at least one message in the class
     * or for the property specified.
     *
     * @param desired Desired outcome for a true result
     * @throws JspException if a JSP exception occurs
     */
protected boolean condition(boolean desired) throws JspException {
    ActionMessages am = null;
    String key = name;
    if ((message != null) && "true".equalsIgnoreCase(message)) {
        key = Globals.MESSAGE_KEY;
    }
    try {
        am = TagUtils.getInstance().getActionMessages(pageContext, key);
    } catch (JspException e) {
        TagUtils.getInstance().saveException(pageContext, e);
        throw e;
    }
    Iterator iterator = (property == null) ? am.get() : am.get(property);
    return (iterator.hasNext() == desired);
}
Also used : JspException(javax.servlet.jsp.JspException) ActionMessages(org.apache.struts.action.ActionMessages) Iterator(java.util.Iterator)

Example 38 with ActionMessages

use of org.apache.struts.action.ActionMessages in project sonarqube by SonarSource.

the class TestTagUtils method testActionMessages_getActionMessages_PageContext_String3.

// -- using String
public void testActionMessages_getActionMessages_PageContext_String3() {
    request.setAttribute("foo", "bar");
    try {
        ActionMessages messages = tagutils.getActionMessages(pageContext, "foo");
        assertNotNull("messages should not be null", messages);
        assertNotNull("messages prop should not be null", messages.get("prop"));
        String key = null;
        int i = 0;
        for (Iterator iter = messages.get(ActionMessages.GLOBAL_MESSAGE); iter.hasNext(); ) {
            ActionMessage message = (ActionMessage) iter.next();
            key = message.getKey();
            Object[] values = message.getValues();
            assertNull(values);
            i++;
        }
        assertEquals("only 1 message", i, 1);
        assertEquals("key should match", key, "bar");
    } catch (JspException e) {
        fail(e.getMessage());
    }
}
Also used : JspException(javax.servlet.jsp.JspException) ActionMessages(org.apache.struts.action.ActionMessages) Iterator(java.util.Iterator) ActionMessage(org.apache.struts.action.ActionMessage)

Example 39 with ActionMessages

use of org.apache.struts.action.ActionMessages in project sonarqube by SonarSource.

the class TestTagUtils method testGetActionErrors1c.

// String Array
public void testGetActionErrors1c() {
    String[] vals = new String[] { "bar", "baz" };
    request.setAttribute("foo", vals);
    try {
        ActionMessages errors = tagutils.getActionMessages(pageContext, "foo");
        assertNotNull("errors should not be null", errors);
        assertNotNull("errors prop should not be null", errors.get("prop"));
        String key = null;
        int i = 0;
        for (Iterator iter = errors.get(ActionMessages.GLOBAL_MESSAGE); iter.hasNext(); ) {
            ActionMessage error = (ActionMessage) iter.next();
            key = error.getKey();
            Object[] values = error.getValues();
            assertNull((values));
            assertEquals("1st key should match", key, vals[i]);
            i++;
        }
        assertEquals("only 1 error", i, 2);
    } catch (JspException e) {
        fail(e.getMessage());
    }
}
Also used : JspException(javax.servlet.jsp.JspException) ActionMessages(org.apache.struts.action.ActionMessages) Iterator(java.util.Iterator) ActionMessage(org.apache.struts.action.ActionMessage)

Example 40 with ActionMessages

use of org.apache.struts.action.ActionMessages in project sonarqube by SonarSource.

the class TestTagUtils method testGetActionErrors1d.

// String Array (thrown JspException)
public void testGetActionErrors1d() {
    request.setAttribute("foo", new MockFormBean());
    ActionMessages errors = null;
    try {
        errors = tagutils.getActionMessages(pageContext, "foo");
        fail("should have thrown JspException");
    } catch (JspException e) {
        assertNull("errors should be null", errors);
    }
}
Also used : JspException(javax.servlet.jsp.JspException) ActionMessages(org.apache.struts.action.ActionMessages) MockFormBean(org.apache.struts.mock.MockFormBean)

Aggregations

ActionMessages (org.apache.struts.action.ActionMessages)47 ActionMessage (org.apache.struts.action.ActionMessage)31 JspException (javax.servlet.jsp.JspException)15 ActionForward (org.apache.struts.action.ActionForward)14 Iterator (java.util.Iterator)11 AbstractContest (cn.edu.zju.acm.onlinejudge.bean.AbstractContest)10 Problem (cn.edu.zju.acm.onlinejudge.bean.Problem)6 UserProfile (cn.edu.zju.acm.onlinejudge.bean.UserProfile)6 ContestPersistence (cn.edu.zju.acm.onlinejudge.persistence.ContestPersistence)4 UserPersistence (cn.edu.zju.acm.onlinejudge.persistence.UserPersistence)4 UserSecurity (cn.edu.zju.acm.onlinejudge.security.UserSecurity)4 Date (java.util.Date)4 Submission (cn.edu.zju.acm.onlinejudge.bean.Submission)3 UserPreference (cn.edu.zju.acm.onlinejudge.bean.UserPreference)3 ProblemPersistence (cn.edu.zju.acm.onlinejudge.persistence.ProblemPersistence)3 Contest (cn.edu.zju.acm.onlinejudge.bean.Contest)2 Course (cn.edu.zju.acm.onlinejudge.bean.Course)2 Reference (cn.edu.zju.acm.onlinejudge.bean.Reference)2 Language (cn.edu.zju.acm.onlinejudge.bean.enumeration.Language)2 LogCriteria (cn.edu.zju.acm.onlinejudge.bean.request.LogCriteria)2