【问题标题】:How to save select multiple in database如何在数据库中保存选择多个
【发布时间】:2016-10-13 12:32:46
【问题描述】:

这是我第一次在这里发帖。我有一个关于如何将来自“html select multiple”的数据保存到数据库中的问题。我正在使用 dto(data transfer object) 将表单中的输入值传递给控制器​​。

这是我的 HTML 代码:

<form:form id="user-form" action="/admin/users/add-user/add-success" commandName="addUser" method="POST">
    <fieldset>
        <legend class="text-center">
            <label class="text font-md">Profile Information</label>
        </legend>             

        <div class="form-group">
            <label class="control-label">Select Property <span class="important">*</span></label>
            <select class="form-control custom-scroll" path="propertyList.propertyId" name="propertyId" id="propertyId" multiple>
                <c:forEach var="property" items="${property}">
                    <option value="${property.id}" data-value="${property.prtCompany.id}"><c:out value="${property.propertyName}"/></option>
                </c:forEach>
            </select>
            <div class="note">
                <strong>Note:</strong> hold down the ctrl/cmd button to select multiple options.
            </div>
        </div>
        <div class="form-actions">
            <div class="row">
                <div class="col-sm-12">
                    <button class="btn btn-primary" type="submit">
                        Submit
                    </button>
                </div>
            </div>
        </div>
    </fieldset>
</form:form>

这是我的控制器的代码:

@RequestMapping(value = "/admin/users/add-user/add-success", method = RequestMethod.POST)
public ModelAndView saveAddUser(ModelMap mvc, @ModelAttribute("addUser") UserProfileDto userProfileDto, HttpServletResponse response) throws IOException, ServerException {

    ModelAndView mv = new ModelAndView("user-pages/user");

    userProfileDto.setAuditId(auditService.logAuditCreate().getId());
    userProfileDto = userProfileService.createUserProfile(userProfileDto);

    return mv;
}

这是我的 ServiceImpl:

public UserProfileDto createUserProfile(UserProfileDto userProfileDto) {
    UserProfile userProfile = new UserProfile();
    userProfile = convertDtoToEntity(userProfile, userProfileDto);
    save(userProfile);
    return convertEntityToDto(userProfile);
}

protected UserProfile convertDtoToEntity(UserProfile userProfile, UserProfileDto dto) {

List<Property> propertyList = listUsersProperty(dto.getPropertyList());

userProfile.setProperty(propertyList);

return userProfile;
}

protected List<Property> listUsersProperty(List<PropertyDto> propertyDtoList) {
List<Property> propertyList = null;
for (PropertyDto propertyDto : propertyDtoList) {
    propertyList.add(entityManager.find(Property.class, propertyDto.getId()));
}
return propertyList;
}

这是错误:

19:37:37,543 ERROR [io.undertow.request] (default task-40) UT005023: Exception handling request to /admin/users/add-user/add-success: org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.ejb.EJBException: java.lang.NullPointerException
Caused by: java.lang.NullPointerException
at com.altypaynet.crm.service.user.UserProfileServiceImpl.listUsersProperty(UserProfileServiceImpl.java:249)
at com.altypaynet.crm.service.user.UserProfileServiceImpl.convertDtoToEntity(UserProfileServiceImpl.java:261)
at com.altypaynet.crm.service.user.UserProfileServiceImpl.createUserProfile(UserProfileServiceImpl.java:136)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.jboss.as.ee.component.ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptor.java:52)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:437)
at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.doMethodInterception(Jsr299BindingsInterceptor.java:82)
at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.processInvocation(Jsr299BindingsInterceptor.java:93)
at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:63)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.as.ejb3.component.invocationmetrics.ExecutionTimeInterceptor.processInvocation(ExecutionTimeInterceptor.java:43)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.as.jpa.interceptor.SBInvocationInterceptor.processInvocation(SBInvocationInterceptor.java:47)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:437)
at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:64)
at org.jboss.as.weld.ejb.EjbRequestScopeActivationInterceptor.processInvocation(EjbRequestScopeActivationInterceptor.java:83)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.as.ee.concurrent.ConcurrentContextInterceptor.processInvocation(ConcurrentContextInterceptor.java:45)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.invocation.InitialInterceptor.processInvocation(InitialInterceptor.java:21)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
at org.jboss.as.ee.component.interceptors.ComponentDispatcherInterceptor.processInvocation(ComponentDispatcherInterceptor.java:52)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.as.ejb3.component.pool.PooledInstanceInterceptor.processInvocation(PooledInstanceInterceptor.java:51)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:275)
... 91 more

还有 DTO,

UserProfileDto:

public class UserProfileDto extends UserBaseDto {

public UserProfileDto() {
}

private List<PropertyDto> propertyList;

public List<PropertyDto> getPropertyList() {
    return propertyList;
}

public void setPropertyList(List<PropertyDto> propertyList) {
    this.propertyList = propertyList;
}
}

PropertyDto:

public class PropertyDto extends CompanyDto {

public PropertyDto() {
}

private String propertyId;

private String propertyNo;

private String propertyName;

private String propertyType;

private Integer propertyUnits;

private Integer propertyFloors;

public String getPropertyId() {
    return propertyId;
}

public void setPropertyId(String propertyId) {
    this.propertyId = propertyId;
}

public String getPropertyNo() {
    return propertyNo;
}

public void setPropertyNo(String propertyNo) {
    this.propertyNo = propertyNo;
}

public String getPropertyName() {
    return propertyName;
}

public void setPropertyName(String propertyName) {
    this.propertyName = propertyName;
}

public String getPropertyType() {
    return propertyType;
}

public void setPropertyType(String propertyType) {
    this.propertyType = propertyType;
}

public Integer getPropertyUnits() {
    return propertyUnits;
}

public void setPropertyUnits(Integer propertyUnits) {
    this.propertyUnits = propertyUnits;
}

public Integer getPropertyFloors() {
    return propertyFloors;
}

public void setPropertyFloors(Integer propertyFloors) {
    this.propertyFloors = propertyFloors;
}
}

我已经尝试搜索这个,但不幸的是没有人能解决我的问题。我希望你们能帮助我。谢谢。

注意:也许你们中的一些人可能会问该属性与用户配置文件的关系是什么,我删除了代码中包含的一些代码,我只是发布了与错误相关的代码。

大注意:对不起我的英语不好。我已经尽力了。

【问题讨论】:

  • 你永远不会将你的“propertyList”初始化为空以外的任何东西(List propertyList = null;)。所以很明显你会在尝试调用 propertyList.add(...) 时得到一个 NPE
  • @911DidBush List propertyList = null; for (PropertyDto propertyDto : propertyDtoList) { propertyList.add(entityManager.find(Property.class, propertyDto.getId())); } 返回属性列表;在它去 for each 之后,它不应该返回 null。
  • 请阅读我链接的问题。只要 propertyList 设置为 null,就不能调用 propertyList.add。
  • 我也试过使用 List propertyList = new ArrayList();,你还有什么要推荐的吗?无论如何,感谢您注意到我的问题。

标签: java html multi-select dto


【解决方案1】:

而不是 UserProfileDto bean 将 PropertyDto 传递给控制器​​。

PropertyDto.getProperId() 将为您提供以逗号分隔的所有选定值。 您得到空指针,因为 dto.getPropertyList() 不包含任何值。

【讨论】:

  • 感谢您的回答,但我不能这样做,正如我在问题的最后一部分中所说的那样,其中包含其他代码,我只是将其删除,因为我收到了错误仅在多选中
  • 换句话说,我还有其他输入需要在 UserProfileDto 中绑定,我只是将其删除我提供的代码。
  • 那么您至少需要在您的 UserProfileDto 中添加私有 String propertyId 以及 getter setter。
  • 我不是私有字符串propertyIid,而是通过初始化来调用PropertyDto列表。
  • 这一行:private List propertyList;
猜你喜欢
  • 2016-04-08
  • 1970-01-01
  • 1970-01-01
  • 2011-06-27
  • 2014-06-03
  • 2013-01-06
  • 2019-01-19
  • 2016-04-28
  • 1970-01-01
相关资源
最近更新 更多