簡體   English   中英

Spring MVC + Hibernate應用

[英]Spring MVC + Hibernate App

我也有一個應用程序(Spring MVC + Hibernate)和AppFuse Framework。

我有兩個實體:具有多對一關系的UserRobot

我需要為Robot表單(robotForm.jsp)的所有者( User )添加一個下拉列表。

Robot實體具有一個User 我讀到我必須為User創建一個自定義Editor UserCustomEditor extends PropertyEditorSupport ),並覆蓋referenceDataRobotFormController添加在initBinder了。

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(); 返回用戶列表。

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

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

我在referenceData方法的ownersMap.put("owners", userManager.getUsers();行中得到了NullPointerException

編輯:

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));
}
}

機器人.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;
    }
}

我看到的唯一的原因是您沒有實例化'userManager'

您可以檢查它是否已經通過(userManager == null) 您的代碼對我來說看起來不錯,沒有考慮邏輯。 我認為您的Spring IoC配置是這里的問題。

您可以發布* .xml文件嗎? 這將有助於解決您的問題。

干杯!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM