【问题标题】:How to build a Simple search page using Spring MVC?如何使用 Spring MVC 构建一个简单的搜索页面?
【发布时间】:2019-11-04 08:45:10
【问题描述】:

我已经使用 Spring MVC 创建了 webapp,并且我已经完成了 CRUD 操作,现在卡在搜索页面上。

我已经在jsp和控制器下面编写了代码。

JSP 页面正文

<div align="center">
        <h1>Search Items</h1>


        <form action="search_1" method="get" modelAttribute="search">
        <table>
        <tr>
            <td>Category:</td>
            <td>
          <select  type="text" name="category_id">
            <option value="Book">Book</option>
            <option value="Audio Books">Audio Books</option>
            <option value="Videos">Videos</option>
            <option value="Music">Music</option>
          </select>
             </td>
        </tr>

        <tr>
            <td>Publisher ID:</td>
            <td>
          <select  type="text" name="publisher_id">
            <option value="Harper Collins">Harper Collins</option>
            <option value="Penguins">Penguins</option>
            <option value="Franciscan Media">Franciscan Media</option>
            <option value="Orbis">Orbis</option>
          </select>
             </td>
        </tr>

        <tr>
            <td>Price Range:</td>
            <td>Min: <input type="text" name="price_1"/> Max: 
            <input type="text" name="price_2"/></td>
        </tr>


        <tr>
                <td colspan="2" align="center"><input type="submit" value="search"></td>
            </tr>

        </table>


        </form>


    </div>

控制器

 @RequestMapping(value ="/search_1",method = RequestMethod.GET)
    public ModelAndView search_1(HttpServletRequest request, HttpServletResponse response) {

        String category_id = request.getParameter("category_id");
        String publisher_id = request.getParameter("publisher_id");
        int price = Integer.parseInt(request.getParameter("price"));



        ModelAndView model = new ModelAndView();
        model.setViewName("searchResult");


        return model;
    }

物品豆

package com.jwt.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "items")
public class Items implements Serializable {

    private static final long serialVersionUID = -3465813074586302847L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @Column
    private String ISBN;

    @Column
    private String title;

    @Column
    private String category_id;

    @Column
    private String Author;


    @Column
    private String publisher_id;

    @Column
    private float price;

    @Column
    private int stock;

    @Column
    private int photo_id;



    public int getid() {
        return id;
    }

    public void setid(int id) {
        this.id = id;

    }

    public String getISBN() {
        return ISBN;
    }

    public void setISBN(String ISBN) {
        this.ISBN = ISBN;
    }

    public String gettitle() {
        return title;
    }

    public void settitle(String title) {
        this.title = title;
    }

    public String getcategory_id() {
        return category_id;
    }

    public void setcategory_id( String category_id) {
        this.category_id = category_id;
    }

    public String getAuthor() {
        return Author;
    }

    public void setAuthor(String Author) {
        this.Author = Author;
    }

  public String getpublisher_id() {
        return publisher_id;
    }

    public void setpublisher_id(String publisher_id) {
        this.publisher_id = publisher_id;
    }


     public float getprice() {
        return price;
    }

    public void setprice(float price) {
        this.price = price;
    }

     public int getstock() {
        return stock;
    }

    public void setstock(int stock) {
        this.stock = stock;
    }


     public int getphoto_id() {
        return photo_id;
    }

    public void setphoto_id(int photo_id) {
        this.photo_id = photo_id;
    }
}

必须根据 JSP 页面上的搜索条件进行搜索。结果视图可以在同一页面上。真的没关系,

【问题讨论】:

    标签: rest spring-mvc


    【解决方案1】:

    我不知道你为什么感到困惑,但让我们看看我能不能帮上忙。

    在您的控制器中,您必须正确提取所有标准,然后使用这些标准从您的数据库中检索项目列表。在服务类中创建一个将这些条件作为参数并返回项目列表的方法。在模型中附加该项目并显示在“searchResult.jsp”页面中。

    这是一个粗略的控制器方法,应该可以处理您的搜索

        @RequestMapping(value = "/search_1", method = RequestMethod.GET)
        public ModelAndView search(HttpServletRequest request) {
    
            String categoryId = request.getParameter("category_id");
            String publisherId = request.getParameter("publisher_id");
            int minPrice = Integer.parseInt(request.getParameter("price_1"));
            int maxPrice = Integer.parseInt(request.getParameter("price_2"));
    
            List<Item> items = someService.getItems(categoryId, publisherId, minPrice, maxPrice);
    
            ModelAndView model = new ModelAndView();
            model.addObject("items", items);
            model.setViewName("searchResult");
    
            return model;
        }
    

    【讨论】:

    猜你喜欢
    • 2011-03-28
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    相关资源
    最近更新 更多