Question

I am new to Struts.I provide a model courseBean to the views to capture information.

It works perfectly if I input correct information with the correct form. Otherwise, if I input with the wrong format, it will throw exception.

Say, I entered abcd in the courseBean.startDate, it will throw exception because abcd cannot be changed into Date format.

Is there any ways to prevent those exception? Or Should I make a new Bean Class and set all the attributes of that class to be String?

            <!--Title-->
            <div class="control-group">
                <label class="control-label" for="inputEmail">Title:</label>
                <div class="controls">
                    <input type="text" name="courseBean.title" placeholder="Title" value="<s:property value="courseBean.title" />"/>
                </div>
            </div>

         <!--Start Date-->
        <div class="control-group">
            <label class="control-label" for="inputPassword">Start Date:</label>
            <div class="controls">
                <input type="text" readonly="readonly" name="courseBean.startdate" placeholder="Start Date" value="<s:property value="courseBean.startdate" />" />
            </div>
        </div>

This questions focus on the auto data format convention of the input to the Bean.

Such as WARNING: Error setting expression 'courseBean.fee' with value '[Ljava.lang.String;@1b40489' when I input "" in the textfield which map to the courseBean.fee

Était-ce utile?

La solution

First, you have to validate the data that the user entered in your form. This can be done in many ways, with Annotations (section Validation Annotations), with XML or with a simple validate() method in your action. You can search on the Internet lots of examples of how to validate data with Struts2 if the official documentation isn't enough.

Then I want to add something related with your jsp. Struts2 has built-in tags that render lots of things and help you to communicate the action with jsps. If we are talking about forms, we have for example <s:form> that can help you, for example, not to hardcode the url of the action in an HTML form or <s:textfield> that can be useful for render validation errors.

If the problem is that you want to use the Twitter Bootstrap notation for build your page, there is a plugin that render the HTML ready to use that CSS Framework.

EDIT

WARNING: Error setting expression 'courseBean.fee' 
with value '[Ljava.lang.String;@1b40489'

The problem with your aproach is that you are trying to assign a String [] to a field that probably isn't a String [] (maybe it's a Date). You have to take the String and parse it to convert it to a Date object. Also, if you don't know how <s:textfield> and <s:form> work in Struts2, you should take a look at this tutorial. It could be helpful for you.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top