Rest 如何使用SpringMVC构建一个简单的搜索页面?
我已经使用SpringMVC创建了webapp,并且我已经完成了CRUD操作,现在仍然停留在搜索页面上 我已经在jsp和控制器下面编写了代码 JSP页面正文Rest 如何使用SpringMVC构建一个简单的搜索页面?,rest,spring-mvc,Rest,Spring Mvc,我已经使用SpringMVC创建了webapp,并且我已经完成了CRUD操作,现在仍然停留在搜索页面上 我已经在jsp和控制器下面编写了代码 JSP页面正文 搜索项目 类别: 书 有声读物 视频 音乐 发布者ID: 哈珀柯林斯 企鹅 方济会传媒 奥比斯 价格范围: 最小值:最大值: 控制器 @RequestMapping(value=“/search_1”,method=RequestMethod.GET) 公共模型和视图搜索_1(HttpServletRequest请求,HttpServl
搜索项目
类别:
书
有声读物
视频
音乐
发布者ID:
哈珀柯林斯
企鹅
方济会传媒
奥比斯
价格范围:
最小值:最大值:
控制器
@RequestMapping(value=“/search_1”,method=RequestMethod.GET)
公共模型和视图搜索_1(HttpServletRequest请求,HttpServletResponse响应){
字符串category_id=request.getParameter(“category_id”);
字符串publisher_id=request.getParameter(“publisher_id”);
int price=Integer.parseInt(request.getParameter(“price”);
ModelAndView模型=新的ModelAndView();
model.setViewName(“搜索结果”);
收益模型;
}
项目bean
package com.jwt.model;
导入java.io.Serializable;
导入javax.persistence.Column;
导入javax.persistence.Entity;
导入javax.persistence.GeneratedValue;
导入javax.persistence.GenerationType;
导入javax.persistence.Id;
导入javax.persistence.Table;
@实体
@表(name=“items”)
公共类项实现可序列化{
私有静态最终长serialVersionUID=-3465813074586302847L;
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私有int-id;
@纵队
专用字符串ISBN;
@纵队
私有字符串标题;
@纵队
私有字符串类别_id;
@纵队
私有字符串作者;
@纵队
私有字符串发布者\u id;
@纵队
私人浮动价格;
@纵队
私人股票;
@纵队
私人int photo_id;
公共int getid(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共字符串getISBN(){
返回ISBN;
}
公共无效集合ISBN(字符串ISBN){
这是ISBN=ISBN;
}
公共字符串gettitle(){
返回标题;
}
公共无效集合标题(字符串标题){
this.title=标题;
}
公共字符串getcategory_id(){
退货类别标识;
}
公共无效集合类别id(字符串类别id){
this.category\u id=category\u id;
}
公共字符串getAuthor(){
返回作者;
}
公共void setAuthor(字符串编写器){
this.Author=作者;
}
公共字符串getpublisher_id(){
返回发布者id;
}
public void setpublisher\u id(字符串publisher\u id){
this.publisher\u id=publisher\u id;
}
公开发行价格(){
退货价格;
}
公共定价(浮动价格){
这个价格=价格;
}
public int getstock(){
退货;
}
公共无效集合库存(内部库存){
这个。股票=股票;
}
public int getphoto_id(){
返回照片/身份证;
}
公共无效setphoto_id(int photo_id){
this.photo\u id=photo\u id;
}
}
必须按照JSP页面上的搜索条件进行搜索。结果视图可以位于同一页上。这真的没关系,我不知道你为什么感到困惑,但让我们看看我是否能帮上忙 在控制器中,必须正确提取所有条件,然后使用这些条件从数据库中检索项目列表。在服务类中创建一个方法,该方法将这些条件作为参数并返回项目列表。在模型中附加该项并显示在“searchResult.jsp”页面中 下面是一个粗略的控制器方法,可以处理您的搜索
@RequestMapping(value=“/search_1”,method=RequestMethod.GET)
公共模型和视图搜索(HttpServletRequest){
String categoryId=request.getParameter(“category_id”);
字符串publisherId=request.getParameter(“publisher_id”);
int minPrice=Integer.parseInt(request.getParameter(“price_1”);
intmaxprice=Integer.parseInt(request.getParameter(“price_2”));
List items=someService.getItems(categoryId、publisherId、minPrice、maxPrice);
ModelAndView模型=新的ModelAndView();
model.addObject(“items”,items);
model.setViewName(“搜索结果”);
收益模型;
}
我尝试过,但仍然面临错误。请你检查一下这个好吗-