1

I am using struts2 data model to accept request parameters, if user does not type any value in input element, the corresponding property in model class is set to "", but I want it to be null, not "". here is my code:

class User{
    private String userName;
    private String userId;

    //setter & getter omitted
}

class UserAction extends ActionSupport{
      private User user;
      //setter & getter omitted

 }

in UserAction class, I maybe need do some business validation or something else, I just use statements lik if(user.getUserId() !== null), now this condition returns true even if user doesn't type anything in <input type="text" name="user.userId" />. maybe I can write like this:if(user.getUserId() != null && !"".equals(user.getUserId())), it is a little tiring and there are many places like this in my code, I want a convenient way to achieve this, does filter can do this? if can is there any exsiting filter or interceptor ? Thanks.

1 Answer 1

1

Why not choosing a more abstract, object oriented, framework-independent way to do this ?

class User{
    private String userName;
    private String userId;
    //setter & getter omitted

    public boolean isEmpty() {
       return (userId==null || "".equals(userId));
    }
}

/* somewhere in your Java code: */
if (!user.isEmpty()) { 

<!-- somewhere in your JSP code: -->    
<s:if test="!user.empty()" >

Alternatively you could alter the getters to return null even if the value is empty String, (or vice versa) but usually this forcing is not good.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.