Java 我应该使用会话来实现搜索框吗?
我有一个带有以下字段的搜索表单Java 我应该使用会话来实现搜索框吗?,java,session,struts2,Java,Session,Struts2,我有一个带有以下字段的搜索表单 price, status, owner 当用户填写所有字段时,请求将发送到后端,以显示具有指定价格、状态和所有者的产品的类别列表。用户可以单击每个类别以查看其产品列表 为了实现它,我使用以下方法来检索类别,并将搜索字段price、status、owner放入会话中,以便在选择类别时在搜索的下一页中可用 参数的值可能很长,我更喜欢将它们设置为便于将结果添加到书签中 public String retrieveCategories(){ //...
price, status, owner
当用户填写所有字段时,请求将发送到后端,以显示具有指定价格、状态和所有者的产品的类别列表。用户可以单击每个类别以查看其产品列表
为了实现它,我使用以下方法来检索类别,并将搜索字段price、status、owner放入会话中,以便在选择类别时在搜索的下一页中可用
参数的值可能很长,我更喜欢将它们设置为便于将结果添加到书签中
public String retrieveCategories(){
//... here I retrieve categories which their products are matched
//with the search fields
Map session = ActionContext.getContext().getSession();
session.put("Items", this.items);
return "SUCCESS";
}
显示所有类别后,用户可以单击每个类别查看其产品。
类别的名称将发送到后端,因此我将从会话中检索值,以搜索所选类别具有相同规格的产品
public String retrieveProductsOfSelectedCategory(){
Map session = ActionContext.getContext().getSession();
this.items = (Items) session.get("Items");
//... here I retrieve products of the selected category based on values retrieved
// from session
}
我想知道如果不是的话,实施它是否是一个好的做法。你的建议是什么?不,我不会用这个课程来实施它。使用该会话有几个不便之处: 它使您的应用程序具有状态,这使得集群变得更加困难:来自给定客户端的每个请求都必须转到同一个服务器,或者会话必须是持久的或复制的。您在会话中投入的越多,在内存、存储和带宽方面的成本就越高 由于使用硬编码的会话密钥存储搜索条件和项目,因此不能在同一搜索页面上打开多个选项卡:它们将共享相同的条件和项目。 清理会话很复杂:何时进行?如果您在许多用例上应用此模式,您将在会话中得到大量无用的数据。会话应针对生存期为整个会话的数据进行起诉。不用于将特定页的数据传递到下一页。 干净的溶液没有那么硬。只需使用隐藏字段或请求参数将数据从一页传递到另一页: 第一页:
<input type="text" name="price" />
<input type="text" name="owner" />
第二页
Please choose a category:
<ul>
<li><a href="/products?name=foo&price=12&category=first"></a></li>
<li><a href="/products?name=foo&price=12&category=second"></a></li>
<li><a href="/products?name=foo&price=12&category=third"></a></li>
</ul>
然后,映射到/products的操作可以从请求参数中检索名称、价格和类别,并显示结果。会话中没有存储任何内容。您应该实现类似的功能。您可以更改get方法等等,但这是一种非常简单的方法
import java.util.*;
/**
* Created by Czipperz on 3/28/2014.
*/
public class Search {
public static class Item {
private String price;
private String status;
private String owner;
public Item(String price, String status, String owner) {
this.price = price;
this.status = status;
this.owner = owner;
}
public String getPrice() {
return price;
}
public String getStatus() {
return status;
}
public String getOwner() {
return owner;
}
}
public static class Category {
private String name;
private List<Item> items;
public Category(String name, List<Item> items) {
this.name = name;
this.items = items;
}
public String getName() {
return name;
}
public List<Item> getItems() {
return items;
}
}
private List<Category> categories = new ArrayList<Category>();
//Only compatable with Java 8
public List<Item> getItems(Category selectedCategory, String price, String status, String owner) {
//The result array
ArrayList<Item> result = new ArrayList<Item>();
//Starts the stream of items (see http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html)
selectedCategory.getItems().stream()
//Filters out all that don't have the price
.filter((i) -> i.getPrice().equalsIgnoreCase(price))
//Filters out those that don't have the status
.filter((i) -> i.getStatus().equalsIgnoreCase(status))
//Filters out those that don't have the owner.
.filter((i) -> i.getOwner().equalsIgnoreCase(owner))
//Has each added to the result List
.forEach((i) -> result.add(i));
//Returns the resulting list of correct items.
return result;
/*
Java 7 Version:
*/
/*
for(Item i : items) {
if(i.getPrice().equalsIgnoreCase(price)) {
if(i.getStatus().equalsIgnoreCase(status)) {
if(i.getOwner().equalsIgnoreCase(owner)) {
result.add(i);
}
}
}
}
*/
}
public List<Category> getCategories() {
return categories;
}
public void addCategory(Category category) {
this.categories.add(category);
}
}
您建议您希望用户为页面添加书签。你需要一些更持久的东西。您可以使用本地存储进行此操作,如果本地存储不可用,则恢复为cookie。如果他们想打开两个窗口,并在每个窗口中执行不同的搜索,该怎么办?这可以不用使用会话变量来完成。您是如何提出请求的,我的意思是,这是一个ajax请求,还是您在搜索和选择类别时重新加载页面?谢谢您的回答,但我想知道如果搜索的总长度超过256,会发生什么,这会限制应用程序吗?据我所知,GET请求的具体限制为256个字符。因此,可能无法正确发送较长的请求。实际限制远大于256。看见但如果你真的很担心,那就用帖子而不是GET。您需要JavaScript使链接发送POST请求而不是GET请求。但我想无论是价格、所有者还是状态,长度都不会超过几个字符。但问题是,参数的值可能太长,我更喜欢将它们设置为便于将结果添加到书签中。你有什么建议?我能说什么?你知道这是有限度的。您怀疑参数可能超过了限制。你不能对限制做任何事。你不想使用没有限制的方法。这是一个不可解的方程式。价格、状态、所有者和类别ID怎么可能超过2K?因为类别ID可能不止一个类别ID