Organizational Research By

Surprising Reserch Topic

struts2 input result how does it work how are conversion validation errors h


struts2 input result how does it work how are conversion validation errors h  using -'java,validation,struts2,casting,type-conversion'

Main Question

The work flow should be like this: if an String is entered other than a number, first it should pass through a exception interceptor, and when passing through param interceptor, while converting to int type, it wont be able to do it using Integer.parseInt and an exception would occur; shouldn't that exception (that is NumberFormatException) be pushed into Value Stack ? Why does it not show NumberFormatException and show the result even though result should not be printed instead ?

Side Question

Whenever I add an alphabet in the form, it changed to zero...? Why so ?

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>


handler jsp

<%@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>

    
asked Sep 24, 2015 by amit.gupta
0 votes
6 views



Related Hot Questions



Government Jobs Opening


...