【发布时间】:2015-07-17 14:06:07
【问题描述】:
我是 java 新手,正在尝试使用 spring-mvc 和 hibernate 编写应用程序。
我收到了错误 “原因:java.lang.IllegalStateException:Bean 名称‘command’的 BindingResult 和普通目标对象都不能用作请求属性” 当我尝试使用数据库中的元素填充下拉列表时,在 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>
【问题讨论】:
-
将您的 JSP 代码发布到您要填充列表的位置。
-
您好,我无法添加 JSP。我怎么做。它的说代码格式不正确
-
我终于成功发布了 JSP
标签: java spring hibernate spring-mvc