我想通过使用SpringMVC,Hibernate从数据库.Am获取值来加载下拉列表。当从数据库中获取列表时,显示表名为id。 它看起来像This " DeviceType [idDT = 1,Devices =台式计算机]" 而不仅仅是(台式电脑)
型号:
@Entity
@Table(name = "Customers")
public class Customers implements Serializable
@NotEmpty
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "DevCustomers",
joinColumns = {@JoinColumn(name = "ID_CUSTOMERS") },
inverseJoinColumns = {@JoinColumn(name = "IDDEV_TYPE") })
private Set<DeviceType> deviceType = new HashSet<DeviceType>();
public Set<DeviceType> getDeviceType() {
return deviceType;
}
public void setDeviceType(Set<DeviceType> deviceType) {
this.deviceType = deviceType;
**************************DeviceType************************************
@Entity
@Table(name = "DeviceType")
public class DeviceType implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer idDT;
@Column(name = "DEVICES", length=20, unique=true, nullable=false)
private String Devices = DeviceTypes.Desktopcomputer.getDeviceTypes();
public Integer getIdDT() {
return idDT;
}
public void setIdDT(Integer idDT) {
this.idDT = idDT;
}
public String getDevices() {
return Devices;
}
public void setDevices(String devices) {
Devices = devices;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((idDT == null) ? 0 : idDT.hashCode());
result = prime * result + ((Devices == null) ? 0 : Devices.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof DeviceType))
return false;
DeviceType other = (DeviceType) obj;
if (idDT == null) {
if (other.idDT != null)
return false;
} else if (!idDT.equals(other.idDT))
return false;
if (Devices == null) {
if (other.Devices != null)
return false;
} else if (!Devices.equals(other.Devices))
return false;
return true;
}
@Override
public String toString() {
return "DeviceType [idDT=" + idDT + ", Devices=" + Devices + "]";
}
}
************************************************************************
public enum DeviceTypes implements Serializable {
Desktopcomputer("Desktop computer"),
laptop("laptop"),
Miscellaneous("Miscellaneous"),
Notebook("Notebook"),
Server("Server"),
Smartphone("Smartphone"),
Tablet("Tablet");
String deviceTypes;
private DeviceTypes(String deviceTypes){
this.deviceTypes = deviceTypes;
}
public String getDeviceTypes(){
return deviceTypes;
}
}
DAO:
public interface DeviceTypeDao {
List<DeviceType>findAllCustomers();
DeviceType findByRType(String deviceType);
DeviceType findByRId(int idDT);
}
******************************implements**************************************
@Repository("deviceTypeDao")
public class DeviceTypeDaoImpl extends AbstractDao<Integer, DeviceType> implements DeviceTypeDao {
static final Logger logger = LoggerFactory.getLogger(DeviceTypeDaoImpl.class);
@SuppressWarnings("unchecked")
public List<DeviceType> findAllCustomers() {
Criteria crit = createEntityCriteria();
crit.addOrder(Order.asc("Devices"));
return (List<DeviceType>)crit.list();
}
@Override
public DeviceType findByRType(String devices) {
Criteria crit = createEntityCriteria();
crit.add(Restrictions.eq("Devices", devices));
return (DeviceType) crit.uniqueResult();
}
@Override
public DeviceType findByRId(int idDT) {
return getByKey(idDT);
}
}
Services:
public interface DeviceTypeService {
List<DeviceType>findAllCustomers();
DeviceType findByRType(String deviceType);
DeviceType findByRId(int idDT);
}
*******************implements*****************************
public class DeviceTypeServiceImpl implements DeviceTypeService {
@Autowired
DeviceTypeDao deviceDao ;
@Override
public List<DeviceType> findAllCustomers() {
return deviceDao.findAllCustomers();
}
@Override
public DeviceType findByRType(String deviceType) {
return deviceDao.findByRType(deviceType);
}
@Override
public DeviceType findByRId(int idDT) {
return deviceDao.findByRId(idDT);
}
}
控制器:
/**
* This method will provide the medium to add a new user.
*/
@RequestMapping(value = { "/newcustomer" }, method = RequestMethod.GET)
public String newcustomer(ModelMap model) {
Customers customers = new Customers();
model.addAttribute("customers", customers);
model.addAttribute("edit", false);
model.addAttribute("loggedinuser", getPrincipal());
return "newcustomer";
}
@RequestMapping(value = { "/newcustomer" }, method = RequestMethod.POST)
public String saveCustomers(@Valid Customers customers, BindingResult result, ModelMap model) {
if (result.hasErrors()) {
return "newcustomer";
}
if (!customersService.isCustomersPhoneUnique(customers.getIdCustomers(), customers.getCustomer_MPhone())) {
FieldError CustomerPhoneError = new FieldError("customers", "CustomerPhone", messageSource
.getMessage("non.unique.CustomerPhone", new String[] {customers.getCustomer_MPhone() }, Locale.getDefault()));
result.addError(CustomerPhoneError);
return "newcustomer";
}
customersService.saveCustomers(customers);
model.addAttribute("Thank you ", "Customer " + customers.getCustomer_Name() + " "+ " registered successfully");
model.addAttribute("loggedinuser", getPrincipal());
return "checkinsuccess";
}
/**
* This method will provide device type list to views
*/
@ModelAttribute("device")
public List<DeviceType>initializeDeives(){
return deviceTypeService.findAllCustomers();
}
查看:
<div class="control-group ">
<label class="control-label" for="device">Asset/DeviceType</label>
<div class="controls">
<form:select path="" id="device" class="form-control span4" >
<form:option value="" >Select Device</form:option>
<form:options path="deviceType" items="${device}" />
</form:select>
<div class="has-error">
<form:errors path="deviceType" class="help-inline" />
</div>
</div>
</div>
我无法找到问题所在。我的代码出了什么问题?
答案 0 :(得分:0)
您将返回list
DeviceType
id
本身包含属性DEVICES
和 <div class="control-group ">
<label class="control-label" for="device">Asset/DeviceType</label>
<div class="controls">
<form:select path="" id="device" class="form-control span4" >
<form:option value="" >Select Device</form:option>
<form:options path="deviceType" items="${device.DEVICES}" />
</form:select>
<div class="has-error">
<form:errors path="deviceType" class="help-inline" />
</div>
</div>
</div>
,请尝试以下操作,
{
"What should I do in the event of an accident?": "Call 911 for emergency assistance, especially in the event of any injury. After calling 911, you should Remain at the scene of the accidentTake photos of the accident scene and vehicles involvedExchange insurance and contact information with anyone involved or witnesses to the accident "
}
答案 1 :(得分:0)
这是解决方案。 我将这些参数添加到选项标签itemLabel =“”itemValue =“”
<div class="control-group ">
<label class="control-label" for="device">Asset/Device Type</label>
<div class="controls">
<form:select path="" id="device" class="form-control span3">
<form:option value="">Select Device</form:option>
<form:options path="deviceTypes" items="${devices} "itemLabel="Devices" itemValue="idDT" />
</form:select>
<div class="has-error">
<form:errors path="deviceTypes" class="help-inline" />
</div>
</div>
</div>