-1

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

code4j
  • 4,208
  • 5
  • 34
  • 51

1 Answers1

1

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.

Pigueiras
  • 18,778
  • 10
  • 64
  • 87
  • Thanks for your answer, but now I have a problem with the xml form validation. I defined some actions in the same class, they share the same courseBean. Some actions is to retrieve database record but some actions is to get user input. When I request those actions retrieving database record, in that time, the courseBean has not been set. This will violate the validation. So I should refactor my design, make a class to do the actions which retrieve the database record only and to make a class to do the actions which accept the data input and validation? – code4j Dec 11 '12 at 13:35
  • Btw, the questions is focusing on the auto data format convention. Such as `WARNING: Error setting expression 'courseBean.fee' with value '[Ljava.lang.String;@1b40489'` Is that XML validation can prevent this problem? – code4j Dec 11 '12 at 13:47
  • Thank you so much. I have applied the Struts2 tag library with plugin and the XML validation. They make my jsp much more clear than before, but I also want to prevent the deadline of the course is before today. So I should add `validate` method in the Action to do checking? Is that any problems if I use XML validation and `validate` method together?? – code4j Dec 12 '12 at 05:08
  • btw, I ask other question :) http://stackoverflow.com/questions/13833456/should-i-retrieve-database-record-in-struts2-view-layer – code4j Dec 12 '12 at 05:38