简体   繁体   中英

Error populating dropdown list from database - spring mvc - hibernate

I am new to java and am trying to write an application using spring-mvc and hibernate.

I am getting the error "Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute" in a JSP file when I try to populate a dropdown list with elements from a database

@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>
 <%@ 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>}

Set the modelAttribute property on your form to the correct value.

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

In your case it should be "mtMetal".

Always name your model attributes when adding them to your model, so that you know what to set in your form.

@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;
}


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM