Dies ist meine erste Buchung hier. Ich habe eine Frage darüber, wie man die Daten von "html select multiple" in die Datenbank speichert. Ich verwende dto (Datenübertragungsobjekt), um den Wert der Eingaben in meinem Formular an den Controller zu übergeben.So speichern Sie wählen mehrere in der Datenbank
Das ist mein HTML-Code ist:
<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>
Dies ist der Code meiner Controller ist:
@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;
}
Das ist mein ServiceImpl ist:
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;
}
Dies ist der Fehler:
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
Und die 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;
}
}
Ich versuche schon darüber gesucht, aber leider gibt es niemanden, dass mein Problem zu lösen. Ich hoffe du kannst mir helfen Jungs. Vielen Dank.
Hinweis: Vielleicht fragen einige von Ihnen, was ist die Beziehung der Eigenschaft zu userprofile, ich entferne einige der Code, die in den Codes enthalten sind, ich poste nur die Codes, die auf den Fehler bezogen ist.
BIG HINWEIS: ENTSCHULDIGUNG FÜR MEIN SCHLECHTES ENGLISCH. ICH HABE MEIN BESTES GEGEBEN.
Sie initialisieren nie Ihre "propertyList" auf etwas anderes als null (Liste propertyList = null;). So offensichtlich erhalten Sie eine NPE beim Versuch, propertyList.add (...) –
Mögliche Duplikat von [Was ist eine NullPointerException, und wie repariere ich es?] (Http://stackoverflow.com/questions/218384/) what-is-a-nullpointerexception-and-how-do-i-fix-it) –
@ 911DidBush Liste propertyList = null; für (PropertyDto propertyDto: propertyDtoList) { propertyList.add (entityManager.find (Property.class, propertyDto.getId())); } Rückgabe propertyList; Nachdem es für jeden geht, sollte es nicht null zurückgeben. –
Sytor