当我将代码放在表单中:form tag on view cadastro.jsp:
<div class="form-group">
<label for="nome" class="col-sm-2 control-label"> name Project: </ label>
<div class="col-sm-10">
<input for="nome" class="form-control" />
<form:errors path="nome" />
</div>
</div>
其作品
但是当我把
<div class="form-group">
<label for="nome" class="col-sm-2 control-label"> Nome do Projeto: </ label>
<div class="col-sm-10">
<form:input cssClass="form-control" path="nome" />
<form:errors path="nome" />
</div>
</div>
无效,因为表单:输入标记
错误:
HTTP状态500 - 在第34行处理JSP页面/WEB-INF/view/cadastro.jsp时发生异常
ProjetroController
@RequestMapping(value = "/novoProjeto", method = RequestMethod.POST)
public String adicionarProjeto(@Valid @ModelAttribute("projeto") Projeto projeto, BindingResult result) {
if(result.hasErrors()) {
return("cadastro");
}
projeto.setStatus("NOVO");
this.pc.salvar(projeto);
return "redirect:/listar";
}
答案 0 :(得分:3)
我认为您需要在表单周围添加<form:form>
标记,如下所示:
<form:form>
<div class="form-group">
<label for="nome" class="col-sm-2 control-label"> Nome do Projeto: </ label>
<div class="col-sm-10">
<form:input cssClass="form-control" path="nome" />
<form:errors path="nome" />
</div>
</div>
</form:form>
在Spring官方文档中,它声明:
此库中的所有其他标记都是 form 标记的嵌套标记。
答案 1 :(得分:1)
你必须导入spring form taglib才能使用spring形式的elemnet,如:
将其添加到您的jsp顶部
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
然后,您可以使用:
<form:form id="myForm" method="post" action="/someAction" modelAttribute="formBean">
<form:label path="name"/>
<form:input path="name"/>
<form:form>
您已在控制器中添加了modelAttribute / command对象,如:
@RequestMapping(value="/someUrl", method=RequestMethod.GET)
public String showForm(Model model){
model.addAttribute("formBean", new FormBean());
return "someViewName";
}
和FormBean
类看起来像:
public class FormBean {
private String name;
public FormBean(){} //default constructor
//getter and setter for name
}