0

我正在学习 Spring MVC,但在理解如何在 jsp 中使用我的类时遇到了问题,这是我的控制器:

@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
4

1 回答 1

1

这是来自这个表单标签:

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

看起来你只做:

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

在方法中就是这个表单的动作。您需要在实际加载页面的方法中将该对象添加到地图中!

于 2013-02-14T20:46:30.633 回答