简体   繁体   English

Spring MVC + Hibernate应用

[英]Spring MVC + Hibernate App

I have an app (Spring MVC + Hibernate) and AppFuse Framework too. 我也有一个应用程序(Spring MVC + Hibernate)和AppFuse Framework。

I have two entities: User and Robot with a Many-To-One relationship. 我有两个实体:具有多对一关系的UserRobot

I need to add a drop down list to for owner ( User ) to the Robot form (robotForm.jsp). 我需要为Robot表单(robotForm.jsp)的所有者( User )添加一个下拉列表。

The Robot entity has a User . Robot实体具有一个User I read that I must create a custom Editor for User . 我读到我必须为User创建一个自定义Editor ( UserCustomEditor extends PropertyEditorSupport ) and override referenceData in the RobotFormController add in the initBinder too. UserCustomEditor extends PropertyEditorSupport ),并覆盖referenceDataRobotFormController添加在initBinder了。

RobotFormController 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). UserEditor(也许这是我的错误)。

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 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. 我在referenceData方法的ownersMap.put("owners", userManager.getUsers();行中得到了NullPointerException

Edit: 编辑:

UserManagerImpl 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 机器人.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. 我看到的唯一的原因是您没有实例化'userManager'

You can check if it has been through (userManager == null) . 您可以检查它是否已经通过(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. 我认为您的Spring IoC配置是这里的问题。

Can you post your *.xml files. 您可以发布* .xml文件吗? This would help out solve your problems. 这将有助于解决您的问题。

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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