从数据库填充下拉列表时出错 - spring mvc - hibernate

时间:2015-07-17 14:06:08

标签: java spring hibernate spring-mvc

我是java的新手,我正在尝试使用spring-mvc和hibernate编写应用程序。

我收到错误  "由以下引起:java.lang.IllegalStateException:BindingResult和bean名称'命令'都没有普通目标对象。可用作请求属性" 当我尝试使用数据库中的元素填充下拉列表时,在JSP文件中

@Controller

public class MetalController {


    @Autowired
    MtMetalService mtMetalService;

    @Autowired 
    MtFormulaService mtFormulaService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView homePage() {

        ModelAndView model =new ModelAndView("homepage");

        return model;
    }

    @RequestMapping(value = "/inputmetal.html",  method = RequestMethod.GET)
    public ModelAndView metalInputForm() {
        MtFormula mtFormula = new MtFormula();
        List<MtFormula> formulalist = mtFormulaService.getAll(mtFormula);

        ModelAndView model =new ModelAndView("metalinput");
        model.addObject(formulalist);

        for (MtFormula x: formulalist) {

            System.out.println(x.getFormulaCode());
        }
        return model;
    }

    @RequestMapping(value = "/metalviewinput.html",  method = RequestMethod.GET)
    public ModelAndView metalViewForm() {

        ModelAndView model =new ModelAndView("metalviewinput");

        return model;
    }

    @RequestMapping(value="/createmetal.html", method = RequestMethod.POST)
    public ModelAndView createMetal(@ModelAttribute("mtMetal") MtMetal mtMetal) {

        mtMetalService.persist(mtMetal);
        ModelAndView model =new ModelAndView("redirect:/inputmetal.html?success=true");

        return model;

    }


    @RequestMapping(value="/viewmetal.html", method={ RequestMethod.GET, RequestMethod.POST})
    public ModelAndView McMetalView(
        @RequestParam("MetalCode") String metalCode) {

        MtMetal mtMetal = new MtMetal();
        mtMetal = mtMetalService.getOne(mtMetal, metalCode);
        ModelAndView model =new ModelAndView("metalview");
        model.addObject("msg", "Metal input form");
        model.addObject(mtMetal);

        return model;
    }

    @RequestMapping(value = "/metallist.html", method={ RequestMethod.GET})                   
    public ModelAndView metalListView() {

        MtMetal mtMetal = new MtMetal();
        List<MtMetal> mtMetalList = mtMetalService.getAll(mtMetal);
        ModelAndView model = new ModelAndView("metallistview");
        model.addObject(mtMetalList);

        return  model;
    }
}


 <%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 <%@ taglib prefix="form" uri="http://www.springframework.org ags/form"%>
 <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>


 <form:form  method="post" action="/labcontrol/createmetal.html"> 

 <c:if test="${param.success eq true}"></c:if> Metal save - Successful
 <table border=1>

        <tr><th>Metal details entry form</th></tr>
        <tr><td>Metal Code1              </td><td> <input type=text      
        name="metalCode"/></td></tr>
        <tr><td>Metal Description        </td><td> <input type=text   
        name="metalDesc"/></td></tr>
        <tr><td>Metal Unit of measure    </td><td> <input type=text 
        name="metalUnit"/></td></tr>    


        <tr><td>Loss Formula             </td><td><form:select 
        path="formulalist">
                                                        <form:option 
        value="-" label="--Please Select"/>
                                                        <form:options 
        items="${formulalist}" itemValue="formulaCode" 
        itemLabel="formula"/>
                                            </form:select> </td></tr>

        <tr><td>Treatment recovery       </td><td> <input type=number 
        name="treatmentRecovery"/></td></tr>
        <tr><td>Salable mass             </td><td> <input type=number 
        name="saleableMass"/></td></tr>
        <tr><td>Pricing unit             </td><td> <input type=number 
        name="pricePerUnit"/></td></tr>
        <tr><td>Gross value              </td><td> <input type=number 
        name="grossValue"/></td></tr>
      <tr><td><input type="submit" value="Save record"></td></tr>   
      </table>
      </form:form>

3 个答案:

答案 0 :(得分:0)

 <%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 <%@ taglib prefix="form" uri="http://www.springframework.org ags/form"%>
 <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>


 <form:form  method="post" action="/labcontrol/createmetal.html"> 

 <c:if test="${param.success eq true}"></c:if> Metal save - Successful
 <table border=1>

        <tr><th>Metal details entry form</th></tr>
        <tr><td>Metal Code1              </td><td> <input type=text      
        name="metalCode"/></td></tr>
        <tr><td>Metal Description        </td><td> <input type=text   
        name="metalDesc"/></td></tr>
        <tr><td>Metal Unit of measure    </td><td> <input type=text 
        name="metalUnit"/></td></tr>    


        <tr><td>Loss Formula             </td><td><form:select 
        path="formulalist">
                                                        <form:option 
        value="-" label="--Please Select"/>
                                                        <form:options 
        items="${formulalist}" itemValue="formulaCode" 
        itemLabel="formula"/>
                                            </form:select> </td></tr>

        <tr><td>Treatment recovery       </td><td> <input type=number 
        name="treatmentRecovery"/></td></tr>
        <tr><td>Salable mass             </td><td> <input type=number 
        name="saleableMass"/></td></tr>
        <tr><td>Pricing unit             </td><td> <input type=number 
        name="pricePerUnit"/></td></tr>
        <tr><td>Gross value              </td><td> <input type=number 
        name="grossValue"/></td></tr>
      <tr><td><input type="submit" value="Save record"></td></tr>   
      </table>
      </form:form>}

答案 1 :(得分:0)

将表单上的modelAttribute属性设置为正确的值。

<form:form modelAttribute="" > ...

在你的情况下它应该是“mtMetal”。

在将模型属性添加到模型时,请始终为模型属性命名,以便您知道在表单中设置的内容。

答案 2 :(得分:0)

@Entity
public class MtMetal {
@Id
@GeneratedValue
private int metalId;
private String metalCode;
private String metalDesc;
private String metalUnit;
private double treatmentRecovery; 
private double saleableMass;
private double pricePerUnit;
private double grossValue;

@OneToOne
private MtFormula lossFormula;


public MtFormula getLossFormula() {
    return lossFormula;
}
public void setLossFormula(MtFormula lossFormula) {
    this.lossFormula = lossFormula;
}
public double getTreatmentRecovery() {
    return treatmentRecovery;
}
public void setTreatmentRecovery(double treatmentRecovery) {
    this.treatmentRecovery = treatmentRecovery;
}
public double getSaleableMass() {
    return saleableMass;
}
public void setSaleableMass(double saleableMass) {
    this.saleableMass = saleableMass;
}
public double getPricePerUnit() {
    return pricePerUnit;
}
public void setPricePerUnit(double pricePerUnit) {
    this.pricePerUnit = pricePerUnit;
}
public double getGrossValue() {
    return grossValue;
}
public void setGrossValue(double grossValue) {
    this.grossValue = grossValue;
}
public String getMetalUnit() {
    return metalUnit;
}
public void setMetalUnit(String metalUnit) {
    this.metalUnit = metalUnit;
}

public int getMetalId() {
    return metalId;
}
public void setMetalId(int metalId) {
    this.metalId = metalId;
}
public String getMetalCode() {
    return metalCode;
}
public void setMetalCode(String metalCode) {
    this.metalCode = metalCode;
}
public String getMetalDesc() {
    return metalDesc;
}
public void setMetalDesc(String metalDesc) {
    this.metalDesc = metalDesc;
}


}


@Entity
public class MtFormula {

@Id
@GeneratedValue
private int formulaId;
private String formulaCode;
private String formulaDesc;
private double formulaRate;

public int getFormulaId() {
    return formulaId;
}
public void setFormulaId(int formulaId) {
    this.formulaId = formulaId;
}
public String getFormulaCode() {
    return formulaCode;
}
public void setFormulaCode(String formulaCode) {
    this.formulaCode = formulaCode;
}
public String getFormulaDesc() {
    return formulaDesc;
}
public void setFormulaDesc(String formulaDesc) {
    this.formulaDesc = formulaDesc;
}
public double getFormulaRate() {
    return formulaRate;
}
public void setFormulaRate(double formulaRate) {
    this.formulaRate = formulaRate;
}


}