我有jsp文件并尝试从两个文本字段插入值。我使用<form:input path="name" />
进行输入。但是在运行时它没有显示文本字段(图片1)。
<form:form method="POST" commandName="student">
<table>
<tr>
<td>Enter your name:</td>
<td><form:input path="name" /></td>
<td><form:errors path="name" cssStyle="color: #ff0000;"/></td>
</tr>
<tr>
<td>Enter your last name:</td>
<td><form:input path="lastName" /></td>
<td><form:errors path="lastName" cssStyle="color: #ff0000;"/></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
<tr>
</table>
</form:form>
学生管理员
@Controller
@RequestMapping("/student.htm")
public class StudentController {
@Autowired
@Qualifier("studentValidator")
private Validator validator;
@InitBinder
private void initBinder(WebDataBinder binder) {
binder.setValidator(validator);
}
@RequestMapping(method = RequestMethod.GET)
public String initForm(Model model){
Student student = new Student();
model.addAttribute("student", student);
return "student";
}
@RequestMapping(method = RequestMethod.POST)
public String submitForm(
Model model, @Validated Student student, BindingResult result) {
String returnVal = "success";
if(result.hasErrors()) {
returnVal = "student";
} else {
model.addAttribute("student", student);
}
return returnVal;
}
}
图片1
答案 0 :(得分:0)
包含taglib并从Controller发送commandName(student)以获取表单
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<form:form method="POST" modelAttribute="student">
<table>
<tr>
<td>Enter your name:</td>
<td><form:input path="name" /></td>
<td><form:errors path="name" cssStyle="color: #ff0000;"/></td>
</tr>
<tr>
<td>Enter your last name:</td>
<td><form:input path="lastName" /></td>
<td><form:errors path="lastName" cssStyle="color: #ff0000;"/></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
<tr>
</table>
</form:form>
和 @Controller
@RequestMapping(value="/")//your mapped path
public String indexPage(Model model){
model.addAttribute("student",new Student());
return "index";//your view Name
}
@PostMapping("/")
public String register(@Valid @ModelAttribute("student") Student student,BindingResult result){
// your logic
}
答案 1 :(得分:0)
确保在页面中包含此标记:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>