我创建了一个Web应用程序。在一个jsp页面中,我有一个复选框,文本框和两个提交按钮。一个提交按钮,只是从DB中检索值并填充在文本框中。另一个提交按钮将检索到的值更新到数据库中的表中。如果我检查复选框并在从数据库中检索后提交值,则应将其存储到数据库中,并且存储正常。然后,如果我取消选中该复选框并提交到数据库。复选框值未更新。反之亦然,如果我只是更新文本框并提交,则值0在数据库中更新,但在再次检查并更新到DB后,该值不会更新。我已经用过,弹簧和休眠了。启发我在哪里做错了。
模型1 的 CloneSafe.java
package com.consulting.data;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import com.consulting.annotation.Setting;
@Entity
@Table(name = "clonesafe_settings")
public class CloneSafe {
@Id
@GeneratedValue
@Column(name = "PK1")
private int pk1;
@Basic
private String cloud_safe_id;
@Basic
private Boolean is_clone_safe;
public int getPk1() {
return pk1;
}
public void setPk1(int pk1) {
this.pk1 = pk1;
}
@Setting(key = "cloud_safe_id")
public String getCloud_safe_id() {
return cloud_safe_id;
}
public void setCloud_safe_id(String cloud_safe_id) {
this.cloud_safe_id = cloud_safe_id;
}
@Setting(key = "is_clone_safe")
public Boolean getIs_clone_safe() {
return is_clone_safe;
}
public void setIs_clone_safe(Boolean is_clone_safe) {
this.is_clone_safe = is_clone_safe;
}
}
模型2 RegistryCloneSettings.java
package com.consulting.data;
public class RegistryCloneDetails {
private String cloneCloudState;
private String cloneSafeId;
public String getCloneCloudState() {
return cloneCloudState;
}
public void setCloneCloudState(String cloneCloudState) {
this.cloneCloudState = cloneCloudState;
}
public String getCloneSafeId() {
return cloneSafeId;
}
public void setCloneSafeId(String cloneSafeId) {
this.cloneSafeId = cloneSafeId;
}
}
表格详情 的 CloneSafe.jsp
<form:form modelAttribute="cloneSafe" method="POST" action="cloneSafe">
<div>
<form:checkbox path="is_clone_safe" />
</div>
<div>
<form:input path="cloud_safe_id" />
<input type="submit" value="Update" name="action" />
<input title="Submit" cancelUrl="bookextract">
</form:form>
Dao Classes 的 CloneSafeDao.java
package com.consulting.dao;
import java.sql.Connection;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.consulting.data.CloneSafe;
import com.consulting.data.RegistryCloneDetails;
import com.consulting.exception.ApplicationException;
import com.consulting.exception.SystemException;
import com.consulting.settings.service.RegistryService;
@Repository("clonesafeDao")
public class CloneSafeDao {
private static final Logger log = LoggerFactory.getLogger(CloneSafeDao.class);
@Autowired
private SessionFactory sessionFactory;
@Autowired
private RegistryService registryService;
/**
* @param s
*/
public void save(CloneSafe s) {
Session session = sessionFactory.openSession();
session.beginTransaction();
session.persist(s);
session.getTransaction().commit();
session.close();
}
/**
* @return
*/
public CloneSafe load() {
Session session = sessionFactory.openSession();
session.beginTransaction();
Query query = session.createQuery("from CloneSafe");
// expecting only one row
List<CloneSafe> cloneSafe = query.list();
session.getTransaction().commit();
session.close();
if (cloneSafe == null || cloneSafe.size() == 0) {
log.debug("CloneSafe null, creating new");
return new CloneSafe();
}
// expecting only one row
return cloneSafe.get(0);
}
/**
* @return
*/
public List<CloneSafe> loadList() {
Session session = sessionFactory.openSession();
session.beginTransaction();
Query query = session.createQuery("from CloneSafe");
// expecting only one row
List<CloneSafe> cloneSafe = query.list();
session.getTransaction().commit();
session.close();
return cloneSafe;
}
/**
* @param cloneSafe
* @return
* @throws SystemException
* @throws ApplicationException
*/
public int update(CloneSafe cloneSafe) throws SystemException, ApplicationException {
Session session = sessionFactory.openSession();
session.beginTransaction();
List<CloneSafe> safe = loadList();
for (CloneSafe g : safe) {
log.debug("Cloud Safe ? : " + g.getIs_clone_safe());
}
if (safe.size() >= 1) {
int pk1 = safe.get(0).getPk1();
log.debug("PK1 : " + pk1);
Query query = session
.createQuery("update CloneSafe set cloud_safe_id = " + ":cloud_safe_id where pk1 =:pk1");
query.setString("cloud_safe_id", cloneSafe.getCloud_safe_id());
query.setInteger("pk1", pk1);
int result = query.executeUpdate();
return result;
} else if (safe.size() == 0) {
save(cloneSafe);
return 1;
}
session.close();
return 0;
}
/**
* Load the System Registry Values
* @return RegistryCloneDetails
*/
public RegistryCloneDetails loadCloneRegistryValues() {
Session session = sessionFactory.openSession();
session.beginTransaction();
try {
RegistryCloneDetails cloneDetails = new RegistryCloneDetails();
String cloudSiteID = registryService.getSystemValue("cloud_site_id", null);
cloneDetails.setCloneSafeId(cloudSiteID);
return cloneDetails;
} catch (Exception e) {
//add exception logic
}
finally {
session.close();
}
return null;
}
}
控制器 的 CloneSafeController.java
package com.consulting.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.service.internal.SessionFactoryServiceRegistryImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.consulting.dao.CloneSafeDao;
import com.consulting.data.CloneSafe;
import com.consulting.data.RegistryCloneDetails;
import com.consulting.exception.ApplicationException;
import com.consulting.exception.SystemException;
import com.consulting.service.impl.SystemRegistryServiceImpl;
import com.consulting.settings.service.RegistryService;
@Controller
@RequestMapping("/admin")
public class CloneSafeController {
private static final Logger _LOG = LoggerFactory.getLogger(CloneSafeController.class);
@Autowired
private CloneSafeDao cloneSafeDao;
@Qualifier("Handle")
private String Handle;
@Autowired
@Qualifier("Vendor")
private String Vendor;
/**
* @param request
* @param response
* @param cloneSafe
* @param result
* @return
*/
@RequestMapping(value = { "cloneSafe" }, method = RequestMethod.GET)
public ModelAndView getSettingsPage(HttpServletRequest request, HttpServletResponse response,
@ModelAttribute("cloneSafe") CloneSafe cloneSafe, BindingResult result) {
ModelAndView mav = new ModelAndView();
cloneSafe = cloneSafeDao.load();
mav.setViewName("cloneSafe");
mav = new ModelAndView("cloneSafe", "cloneSafe", cloneSafe);
return mav;
}
/**
* @param request
* @param response
* @param cloneSafe
* @param result
* @return
*/
@RequestMapping(value = { "cloneSafe" }, method = RequestMethod.POST)
public ModelAndView saveCloneSafeSettings(HttpServletRequest request, HttpServletResponse response,
@ModelAttribute("cloneSafe") CloneSafe cloneSafe, BindingResult result) {
ModelAndView mav = new ModelAndView();
try {
int i =cloneSafeDao.update(cloneSafe);
} catch (Exception e){
//Add Logic
_LOG.debug("Errored....");
}
mav.setViewName("cloneSafe");
mav = new ModelAndView("cloneSafe", "cloneSafe", cloneSafe);
return mav;
}
/**
* Load the System Registry Values
* @param request
* @param response
* @param cloneSafe
* @param result
* @param model
* @return
* @throws SystemException
* @throws ApplicationException
*/
@RequestMapping(value = { "cloneSafe" }, params = "action", method = RequestMethod.POST)
public ModelAndView loadCloneSafeDetails(HttpServletRequest request,
HttpServletResponse response,@ModelAttribute("cloneSafe") CloneSafe cloneSafe,
BindingResult result, ModelMap model){
ModelAndView mav = new ModelAndView();
RegistryCloneDetails cloneDetails= cloneSafeDao.loadCloneRegistryValues();
if(null!=cloneDetails)
{
if(null!=cloneDetails.getCloneSafeId())
{
cloneSafe.setCloud_safe_id(cloneDetails.getCloneSafeId());
}
}
mav.setViewName("cloneSafe");
mav = new ModelAndView("cloneSafe", "cloneSafe", cloneSafe);
return mav;
}
}
我犯了什么错误,为什么复选框值在第一次更新后没有更新。我需要更改代码的地方。提前致谢。
答案 0 :(得分:0)
我在hibernate更新查询中犯了一个错误。在我纠正之后,我没有遇到任何问题。
query = session.createQuery("update CloneSafe set cloud_safe_id = " + ":cloud_safe_id,is_clone_safe = " + ":is_clone_safe where pk1 =:pk1");
query.setString("cloud_safe_id", cloneSafe.getCloud_safe_id());
query.setInteger("pk1", pk1);
query.setBoolean("is_clone_safe", cloneSafe.getIs_clone_safe());
session.getTransaction().commit();
我在DAO中添加了上面的代码,一切正常。