簡體   English   中英

帶有Hibernate的Spring MVC項目可以了解如何在JSP中使用

[英]Spring MVC project with hibernate can' understand how to use in jsp

我正在研究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

那是從這個表單標簽出來的:

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

看來您只能這樣做:

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

在此窗體的操作方法中。 您需要通過實際加載頁面的方法將該對象添加到地圖中!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM