简体   繁体   中英

Spring MVC + Hibernate App

I have an app (Spring MVC + Hibernate) and AppFuse Framework too.

I have two entities: User and Robot with a Many-To-One relationship.

I need to add a drop down list to for owner ( User ) to the Robot form (robotForm.jsp).

The Robot entity has a User . I read that I must create a custom Editor for User . ( UserCustomEditor extends PropertyEditorSupport ) and override referenceData in the RobotFormController add in the initBinder too.

RobotFormController

protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
  SimpleDateFormat dateFormat = new SimpleDateFormat(getText("date.format"));
  dateFormat.setLenient(false);
  binder.registerCustomEditor(Date.class, null, 
  new CustomDateEditor(dateFormat, true));
  binder.registerCustomEditor(Long.class, null,
        new CustomNumberEditor(Long.class, null, true));
  binder.registerCustomEditor(User.class, new UserEditor(userManager));
}

// ...
protected Map referenceData(HtppServletRequest request) throws Exception {
  Map ownersMap = new HashMap();
  ownersMap.put("owners", userManager.getUsers();
  return ownersMap;
}

userManager.getUsers(); return a List of Users.

UserEditor (maybe here is my error).

public class UserEditor extends PropertyEditorSupport {

  private final UserManager userManager;
  protected final transient Log log = LogFactory.getLog(getClass());

  public UserEditor(UserManager userManager) throws IllegalArgumentException {
    this.userManager = userManager;
  }

  @Override
  public void setAsText(String text) throws IllegalArgumentException {
   if (text != null && text.length() > 0) {
     try {
          User user = userManager.getUser(new String (text));
                    super.setValue(user);
         } catch (NumberFormatException ex) {
                    throw new IllegalArgumentException();
         }
      } else {
            super.setValue(null);
            }
     }

  @Override
  public String getAsText() {
    User user = (User) super.getValue();
    return  (user != null ? (user.getId()+"").toString(): "");
  }

} 

robotForm.jsp

<form:select path="owner" itemValue="id" itemLabel="name" items="${owners}"
</form:select>  

I get a NullPointerException in the ownersMap.put("owners", userManager.getUsers(); line of the referenceData method.

Edit:

UserManagerImpl

@Service(value = "userManager")
public class UserManagerImpl implements UserManager {
@Autowired
UserDao dao;

public void setUserDao(UserDao dao) {
    this.dao = dao;
}

public List getUsers() {
    return dao.getUsers();
}

public User getUser(String userId) {
    return dao.getUser(Long.valueOf(userId));
}

public void saveUser(User user) {
    dao.saveUser(user);
}

public void removeUser(String userId) {
    dao.removeUser(Long.valueOf(userId));
}
}

Robot.java

public class Robot extends BaseObject {
    private static final long serialVersionUID = -1932852212232780150L;
    private Long id;
    private String name;
    private Date birthday;
    private User owner;

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public User getOwner() {
        return owner;
    }
    public void setOwner(User owner) {
        this.owner = owner;
    }
}

The only thing I am seeing why you are having a null in there is that 'userManager' has not been instantiated.

You can check if it has been through (userManager == null) . Your code looks good to me, not looking into the logic. I think your configuration of Spring's IoC is the problem here.

Can you post your *.xml files. This would help out solve your problems.

Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM