当我将代码放在cadastro.jsp的form:form标记内时:
<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"; }
您必须导入spring形式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类看起来像:
FormBean
public class FormBean { private String name; public FormBean(){} //default constructor //getter and setter for name }