这是我在JSP文件中的表单:
<p class="lead">Check out the books in my store...</p>
<form:form action="${pageContext.request.contextPath}/bookListByCategory">
<select path="category">
<option value="Programming">Programming</option>
<option value="Fiction">Fiction</option>
<option value="Biography">Biography</option>
<option value="Travel">Travel</option>
</select>
<input type="submit" name="submit" value="submit" />
</form:form>
表单将发送到方法:
@RequestMapping(value = "/bookListByCategory/{category}")
public String getBooksByCategory(@PathVariable String category, Model model) {
List<Book> bookListByCategory = bookService.getBookListByCategory(category);
model.addAttribute("books", bookListByCategory);
model.addAttribute("category", category);
return "bookList";
}
出于某种原因,它无法正常工作,我可以在浏览器中看到以下内容:
当我点击&#34;提交&#34;它将显示404:
我可以看到所选选项的值不会发送到Controller,而Controller是处理请求所必需的。有什么想法吗?
答案 0 :(得分:2)
现在的工作表格:
<form action="${pageContext.request.contextPath}/bookListByCategory">
<select name="category" onchange="this.form.submit();">
<option value="all">-- category --</option>
<option value="Programming">Programming</option>
<option value="Fiction">Fiction</option>
<option value="Biography">Biography</option>
<option value="Travel">Travel</option>
</select>
</form>
控制器中还有两种方法:
@RequestMapping(value = "/bookListByCategory/{category}")
public String getBooksByCategory(@PathVariable String category, Model model) {
List<Book> bookListByCategory = bookService.getBookListByCategory(category);
model.addAttribute("books", bookListByCategory);
model.addAttribute("category", category);
return "bookList";
}
@RequestMapping(value = "/bookListByCategory")
public String getBooksByCategoryForm(@ModelAttribute("category") String category,
Model model) {
if (category.equalsIgnoreCase("all")) {
return "redirect:/bookList/";
} else {
return "redirect:/bookListByCategory/" + category;
}
}
一切正常,并产生所需的输出。
答案 1 :(得分:0)
我将举例说明如何向mvc控制器发送选项。
所以在jsp中说你有这个 -
请注意,我在此使用了<form:select/>
而不是select
。
<form:form action="bookListByCategory" method="get">
<form:select path="category">
<form:option value="NONE"> --SELECT--</form:option>
<form:options items="${listOfCategories}"></form:options>
</form:select>
...
</form:form>
现在使用RequestParam
@RequestMapping(value = "/bookListByCategory", method = RequestMethod.GET)
public String getBooksByCategory(@RequestParam String category, Model model)
throws Exception {
//rest of code ....
}