我创建了一个页面,使用弹簧启动&百里香叶从数据库搜索学生。在我的SearchStudent.html
页面中,有3个字段作为搜索参数(firstName,lastName,city)。我的要求是,即使没有输入参数(全部搜索)或基于参数,搜索也应该可以工作。 '搜索全部'条件正在工作,但不确定如何在搜索条件中传递一些或全部参数时如何更改控制器以正常工作。如何在springboot中将参数值传递给控制器
SearchController
@Controller
@ComponentScan
public class SearchController {
@Autowired
protected StudentRepository repository;
@RequestMapping(value = {"/","/search"}, method = RequestMethod.GET)
public String search(Model model) {
model.addAttribute("student", new Student());
model.addAttribute("allStudents", (ArrayList<Student>)repository.findAll());
return "SearchStudent";
}
SearchStudent.html
<div class="panel-body">
<form th:object="${student}" th:action="@{/search}" action="#" method="get">
<input type="text" th:field="*{firstName}" class="form-control" placeholder="First Name" />
<div style="clear: both; display: block; height: 10px;"></div>
<input type="text" th:field="*{lastName}" class="form-control" placeholder="Last Name" />
<div style="clear: both; display: block; height: 10px;"></div>
<input type="text" th:field="*{city}" class="form-control" placeholder="City" />
<div style="clear: both; display: block; height: 10px;"></div>
<input type="submit" class="btn btn-danger pull-right" value="Search">
<input type="submit" class="btn btn-success pull-right" value="Clear">
</form>
</div>
我用你的例子findAll但得到错误------------有一个意外的错误(type = Method Not Allowed,status = 405)。 请求方法'GET'不支持----------------如果我删除method = RequestMethod.POST它的作品,但没有记录显示。 – Muhammad
我想我的编辑上面应该帮助..请检查它:) :) –
我也尝试过,但没有运气:( – Muhammad