Struts2 INPUT Result: How Does It Work? How are conversion / validation errors handled?

Main question

The workflow should look like this: if the string is entered differently than the number, it must first go through the exception hook, and when passing through the para-hook, when converting to int, it will not be able to do this Integer.parseInt, and an exception will occur; shouldn't this exception (i.e. NumberFormatException) in the Value Stack? Why doesn't it show NumberFormatExceptionand show the result, although the result should not be printed instead?

Side question

Whenever I add an alphabet in a form, it changes to zero ...? Why is that?

index.jsp

<%@ taglib uri="/struts-tags" prefix="s"%>
<s:form action="divide">
    <s:textfield name="number1" label="number1"/>
    <s:textfield name="number2" label="number2"/>
    <s:submit value="divide"/>
</s:form>

divide.java

package actions;

public class divide {
    int number1,number2,result;
    public String execute() throws Exception
    {
        result=number1/number2;
        return "success";
    }
    public int getNumber1() {
        return number1;
    }
    public void setNumber1(int number1) {
        this.number1 = number1;
    }
    public int getNumber2() {
        return number2;
    }
    public void setNumber2(int number2) {
        this.number2 = number2;
    }
    public int getResult() {
        return result;
    }


}

result.jsp

<%@taglib uri="/struts-tags" prefix="s" %>
<b>
    the result of division is
    <s:property value="result"/>
</b>
<jsp:include page="index.jsp"></jsp:include>

jsp handler

<%@taglib uri="/struts-tags" prefix="s"%>
<b>
    following exception occured during the processing
    <s:property value="exception"/>
</b>
<jsp:include page="index.jsp"/>

struts.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>
        <package name="yo" extends="struts-default">
            <action name="divide" class="actions.divide">
                <exception-mapping result="error" exception="Exception"/>
                <result name="success">/result.jsp</result>
                <result name="error">/handler.jsp</result>
            </action>
        </package>
    </struts>
+3
1

:

, , , , -, int, , Integer.parseInt , , ? numberformatexception , ?

Struts 2 , : , < > , , - , , , . INPUT, .

  • Parameters Interceptor . RuntimeException (, NumberFormatException) , devMode - true, Action Errors, . :

    for (Map.Entry<String, Object> entry : acceptableParameters.entrySet()) {
        String name = entry.getKey();
        Object value = entry.getValue();
        try {
            newStack.setParameter(name, value);
        } catch (RuntimeException e) {
            if (devMode) {
                String developerNotification = LocalizedTextUtil.findText(ParametersInterceptor.class, "devmode.notification", ActionContext.getContext().getLocale(), "Developer Notification:\n{0}", new Object[]{
                         "Unexpected Exception caught setting '" + name + "' on '" + action.getClass() + ": " + e.getMessage()
                });
                LOG.error(developerNotification);
                if (action instanceof ValidationAware) {
                    ((ValidationAware) action).addActionMessage(developerNotification);
                }
            }
        }
    }
    
  • Conversion Errors Interceptor , - : Field Error; , , . :

    , ActionContextErrors ( , ValidationAware). , , , , , . , , "abc" int, ( "abc" ) , int (, 0, ).

  • Validation Interceptor ( XML, validate() validateXXX() ), Field Errors , .

  • Workflow Interceptor , Field Errors ( ). , . , INPUT.

, , ( ). struts-default.xml:

<!-- others interceptors here... -->
<interceptor-ref name="params">
    <param name="excludeParams">^dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,^parameters\..*,^action:.*,^method:.*</param>
</interceptor-ref>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
    <param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
    <param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<!-- ... others interceptors here -->

:

, , ...? ?

: String int , Getter ; int, Integer, int , int: 0.

, Conversion Interceptor ( № 2), , Action ( null 0). :

.

, , . , , "abc" , . , , "" , , -, .

...

, . , ... ? (, , ) value:

0 abc:

<s:textfield name = "myIntField" 
            value = "%{getText('format.number',{myIntField})}" />

.

, , abc abc:

<s:textfield name = "myIntField" />

<s:textfield name = "myIntField" 
            value = "%{myIntField}" />

  • , ,
  • (, , , ), , value.

value, , , .

+10

All Articles