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 に答える