使用form:inputtags时,无论是BeanBean名称的BindingResult还是普通目标对象

时间:2019-06-12 12:41:30

标签: spring spring-mvc

我正在尝试使用Spring MVC表单标签创建一个简单的提交表单, 使用和内部。

当我的表单如下:

  <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html>
<head>

<title>Create Coupon</title>
</head>
<body style="background-color:powderblue;">



<h1> Create Coupon </h1>


<form:form action="company/create" method="POST" modelAttribute="theCoupon">

<form:input path="title"/>
<form:input path="startDate"/>
<form:input path="endDate"/>
<form:input path="amount"/>
<form:input path="message"/>
<form:input path="price"/>
<form:input path="image"/>

<input type="submit" value="submit">
</form:form>


</body>
</html>

我得到了java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'theCoupon' available as request attribute

删除时

 <form:input path="title"/>
<form:input path="startDate"/>
<form:input path="endDate"/>
<form:input path="amount"/>
<form:input path="message"/>
<form:input path="price"/>
<form:input path="image"/>

页面似乎已加载,我不知道这是否是页面中断的真正原因,有什么想法吗?

这是控制器的外观:

package com.example.CouponProjectCore.rest;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.example.CouponProjectCore.customerDAO.CompanyDAO;
import com.example.CouponProjectCore.customerService.CompanyService;
import com.example.CouponProjectCore.customerService.CouponService;
import com.example.CouponProjectCore.entity.Company;
import com.example.CouponProjectCore.entity.Coupon;
import com.example.CouponProjectCore.entity.CouponType;
import com.example.CouponProjectCore.entity.Customer;

@Controller
@RequestMapping("/company")
public class CompanyController  {

    CompanyService companyService;
    CouponService couponService;

    @Autowired
    public CompanyController(CompanyService companyService) {
        this.companyService = companyService;
    }

    @GetMapping("/showAllCompany")
    public String findAll(Model theModel) {
        List<Company> companies = companyService.findAll(); 
        System.out.println(companies);
        theModel.addAttribute("companies", companies);
    return "showAllCompanies";

    }

    @PostMapping("/company")
    public String save(@ModelAttribute Company company,Model theModel) {
        System.out.println
        ("inside save company method");
        System.out.println(company);
        company.setId(0);
        companyService.save(company);
        System.out.println(company);
        theModel.addAttribute("company",company);
        System.out.println(company);
        return "savedCompany";
    }

    @PostMapping("/add")
    public String newCoupon(Model theModel) {

        theModel.addAttribute("theCoupon", new Coupon());

        return "add";
    }

    @PostMapping("/create")
    public String createNewCoupon(@ModelAttribute("theCoupon") Coupon theCoupon) {

        theCoupon.setId(0);
        couponService.save(theCoupon);


        return "savedCoupon";
    }
}

添加随机词,这样我的帖子就不再是代码了

1 个答案:

答案 0 :(得分:0)

到目前为止,我的答案是删除标签并改用html标签,效果很好。