gpt4 book ai didi

java - Spring MVC项目用hibernate无法理解如何在jsp中使用

转载 作者:行者123 更新时间:2023-12-01 14:50:46 25 4
gpt4 key购买 nike

我正在学习 Spring MVC,并且我在理解如何在 jsp 中使用我的类时遇到问题,这是我的 Controller :

@Controller
public class BusinessController {
@Autowired
private BusinessService businessService;

@RequestMapping("/index")
public String listContacts(Map<String, Object> map) {

map.put("business", new Business());
map.put("businessList", businessService.listBusiness());

return "business";
}

@RequestMapping("/find/{businessDate}")
public String listContactsByDate(@PathVariable("businessDate") Map<String, Object> map, String businessDate) {

map.put("businessByDate", new Business());
map.put("businessByDateList", businessService.listBusinessByDate(businessDate));

return "businessByDate";
}

@RequestMapping("/")
public String home() {
return "redirect:/index";
}

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addBusiness(@ModelAttribute("business") Business business, BindingResult result) {

businessService.addBusiness(business);

return "redirect:/index";
}

@RequestMapping("/delete/{businessId}")
public String deleteBusiness(@PathVariable("businessId") Integer businessId) {

businessService.removeBusiness(businessId);

return "redirect:/index";
}
}

这是我的jsp:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
<title><spring:message code="label.title" /></title>
</head>
<body>


<h2>
<spring:message code="label.title" />
</h2>

<form:form method="post" action="add" commandName="business">

<table>
<tr>
<td><form:label path="businessDate">
<spring:message code="label.date" />
</form:label></td>
<td><form:input path="businessDate" /></td>
</tr>
<tr>
<td><form:label path="description">
<spring:message code="label.description" />
</form:label></td>
<td><form:input path="description" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit"
value="<spring:message code="label.addbusiness"/>" /></td>
</tr>
</table>
</form:form>

<form:form method="post" action="/find/{$businessDate}"
commandName="businessByDate">
<table>
<tr>
<td><form:label path="businessDate">
<spring:message code="label.date" />
</form:label></td>
<td><form:input path="businessDate" /></td>
</tr>
</table>
<c:if test="${!empty businessByDateList}">
<table class="data">
<tr>
<th><spring:message code="label.date" /></th>

<th><spring:message code="label.description" /></th>
<th>&nbsp;</th>
</tr>
<c:forEach items="${businessByDate}" var="business">
<tr>
<td>${businessByDate.businessDate}</td>
<td>${businessByDate.description}</td>
<td><a href="delete/${businessByDate.id}"><spring:message
code="label.delete" /></a></td>
</tr>
</c:forEach>
</table>
</c:if>

</form:form>

<h3>
<spring:message code="label.businesses" />
</h3>
<c:if test="${!empty businessList}">
<table class="data">
<tr>
<th><spring:message code="label.date" /></th>

<th><spring:message code="label.description" /></th>
<th>&nbsp;</th>
</tr>
<c:forEach items="${businessList}" var="business">
<tr>
<td>${business.businessDate}</td>
<td>${business.description}</td>
<td><a href="delete/${business.id}"><spring:message
code="label.delete" /></a></td>
</tr>
</c:forEach>
</table>
</c:if>
</body>
</html>

而且,这是我的服务实现类:

@Repository
public class BusinessDAOImpl implements BusinessDAO{

@Autowired
private SessionFactory sessionFactory;

public void addBusiness(Business business){
sessionFactory.getCurrentSession().save(business);
}

@SuppressWarnings("unchecked")
public List<Business> listBusinessByDate(String businessDate){
String hql = "from Business B where B.date = :business_date";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
query.setParameter("business_date", businessDate);
return query.list();
}

@SuppressWarnings("unchecked")
public List<Business> listBusiness(){
return sessionFactory.getCurrentSession().createQuery("from Business").list();
}

public void removeBusiness(Integer id){
Business business = (Business) sessionFactory.getCurrentSession().load(
Business.class, id);
if (null != business) {
sessionFactory.getCurrentSession().delete(business);
}
}
}

如果没有jsp的部分,我尝试列出企业的日期,一切正常,我可以添加一个企业,它将立即列在下表中,但如果我添加带有businessByDate的部分,我会得到

Neither BindingResult nor plain target object for bean name 'businessByDate' available as request attribute

我该如何解决这个问题?谢谢

enter code here

最佳答案

这是来自此表单标签:

<form:form method="post" action="/find/{$businessDate}"
commandName="businessByDate">

看起来你只做:

map.put("businessByDate", new Business());

在该表单的操作方法中。您需要在实际加载页面的方法中将该对象添加到 map 中!

关于java - Spring MVC项目用hibernate无法理解如何在jsp中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14883531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com