每当你使用javascript或jsf返回页面而不将其存储到数据库中时,如何在jsp页面中维护复选框状态

时间:2022-05-04 21:18:03

i want my all check boxes checked whenever i come back from other pages, i want to maintain their states across pages using javascript.

我希望每当我从其他页面回来时检查所有复选框,我想使用javascript在页面中维护它们的状态。

3 个解决方案

#1


0  

I think you are asking how to store state for an individual session between requests. In this case, that state is checkbox values.

我想你问的是如何在请求之间存储单个会话的状态。在这种情况下,该状态是复选框值。

You have a choice to make first: do you want to store the data on the client (in the browser) or on your server?

您可以选择先制作:您想将数据存储在客户端(在浏览器中)还是存储在服务器上?

Server Side

You can store this state on the server side with or without a "database" depending on how pedantic you want to be about the term.

您可以在服务器端存储此状态,有或没有“数据库”,具体取决于您希望了解该术语的迂腐程度。

If what you want is to avoid configuring an SQL RDBMS, you might find that the built-in storage options from most Java Servlet containers will work. In Tomcat, you can just use your Session objects as normal, but configure a "File Based Store" instead of a "JDBC Based Store." This will store session data to disk in files. Alternatively you can use StandardManager which uses in-memory storage, but does not persist session state across restarts.

如果您想要避免配置SQL RDBMS,您可能会发现大多数Java Servlet容器中的内置存储选项都可以使用。在Tomcat中,您可以正常使用Session对象,但可以配置“基于文件的存储”而不是“基于JDBC的存储”。这会将会话数据存储到磁盘中的文件中。或者,您可以使用使用内存存储的StandardManager,但不会在重新启动时保持会话状态。

Put simply, these will create a Java Map for each JSESSIONID issued by your server, and then keep the maps in memory, on disk, or in a JDBC database. For more information see: https://tomcat.apache.org/tomcat-7.0-doc/config/manager.html

简而言之,这些将为服务器发出的每个JSESSIONID创建一个Java Map,然后将映射保存在内存,磁盘或JDBC数据库中。有关更多信息,请参阅:https://tomcat.apache.org/tomcat-7.0-doc/config/manager.html

Client Side

Here you have a few options as well. The driving factor is what level of browser you wish to support. If you can tolerate restricting your users to those who use a browser with HTML5 web storage and JavaScript enabled, things are pretty easy. If not, you can accomplish the same thing with a cookie.

这里有几个选项。驱动因素是您希望支持的浏览器级别。如果您可以容忍将用户限制为使用带有HTML5网络存储和启用JavaScript的浏览器的用户,那么事情就非常简单。如果没有,你可以用cookie完成同样的事情。

The big downside to client-side storage is trust. Users (or software on their computer) can modify client-side storage. This goes for cookies, localStorage, and sessionStorage. Many developers forget this and introduce security vulnerabilities because of it. If this is for a real production web application, you'll want to wrap your state in an authenticator.

客户端存储的一大缺点是信任。用户(或其计算机上的软件)可以修改客户端存储。这适用于cookie,localStorage和sessionStorage。许多开发人员忘记了这一点并因此引入了安全漏洞。如果这是一个真正的生产Web应用程序,您将希望将您的状态包装在身份验证器中。

Here's a the first in a three article series on a way to convince your servlet container to put session state into cookies in a way that is transparent to your servlets. It is missing authentication, but you can add it by following guidance such as this bit from Rob Winch.

这是关于说服servlet容器以对servlet透明的方式将会话状态放入cookie的方式的三篇系列文章中的第一篇。它缺少身份验证,但您可以按照Rob Winch的这一位指导添加它。

Now What?

Ok. You've decided to use client- or server-side storage for your checkbox values. Now what?

好。您决定使用客户端或服务器端存储来获取复选框值。怎么办?

A simple (usually wrong) option is to store the checkbox input names and values in a map:

一个简单的(通常是错误的)选项是将复选框输入名称和值存储在地图中:

{"boxFoo": true,"BarBox":false}

The reason this is usually wrong is that it fails to distinguish which form your user was visiting. It means that if you apply this strategy to more than one form on your site, you'll have to worry about name collisions.

这通常是错误的原因是它无法区分您的用户正在访问哪种形式。这意味着,如果您将此策略应用于网站上的多个表单,则必须担心名称冲突。

The next evolution is to have a structure keyed by form name and then field name. This would be a map like the following:

下一个演变是使用表单名称和字段名称键入结构。这将是如下地图:

{ "formA": {"boxFoo": true,"BarBox":false},
  "formQ": {"checkAlpha":true,"BetaCheck":false } }

This works, but will have annoying behavior when your users use multiple tabs. You can make that behavior more predictable for your users by using per-tab identifiers -- at the expense of space in your session object -- or by using AJAX to keep the fields in sync -- which has its own perils. Or you can do what most people do an just assume that the last submitted form overwrites the state from all previous ones, tabs be damned. That's much simpler to code, but more annoying to users.

这可行,但当用户使用多个标签时会产生烦人的行为。您可以通过使用每个选项卡标识符(以会话对象中的空间为代价)或使用AJAX使字段保持同步(这有其自身的危险),使您的用户更可预测该行为。或者你可以做大多数人做的事情,只是假设最后提交的表单覆盖了以前所有表单的状态,标签被诅咒。这对代码来说简单得多,但对用户来说更烦人。

#2


0  

You have to bind the value with a backing bean. As long as the backing bean is having the appropriate scope it will be retained on the page when you navigate to it.

您必须使用辅助bean绑定该值。只要辅助bean具有适当的范围,当您导航到它时,它将保留在页面上。

#3


0  

I can propose some ways :

我可以提出一些方法:

  • send http params (in hidden field) with check boxes flags which must stay checked in each new page requested by your application . You can factorize it with a function but it stays cumbersome to do.
  • 发送带有复选框标志的http参数(在隐藏字段中),这些标记必须在应用程序请求的每个新页面中保持检查状态。您可以使用函数对其进行分解,但这样做很麻烦。

  • store the check boxes marker flag in the http session. If the check boxes must stay checked in all the life of your user, it may be a suitable solution. Use may use a backing bean session for it as you use JSF. Nevertheless, store the minimum of information in it.
  • 将复选框标记标志存储在http会话中。如果必须在用户的整个生命周期内检查复选框,则可能是合适的解决方案。当您使用JSF时,使用可以使用支持bean会话。尽管如此,请将最少的信息存储在其中。

  • store the information in a shared applicative cache to retrieve it. In this way, you stay stateless and you have not the drawback of the session if you use clustering in your servers.
  • 将信息存储在共享的应用程序缓存中以检索它。这样,如果在服务器中使用群集,则保持无状态并且没有会话的缺点。

There is maybe better as alternative.

可能有更好的选择。

#1


0  

I think you are asking how to store state for an individual session between requests. In this case, that state is checkbox values.

我想你问的是如何在请求之间存储单个会话的状态。在这种情况下,该状态是复选框值。

You have a choice to make first: do you want to store the data on the client (in the browser) or on your server?

您可以选择先制作:您想将数据存储在客户端(在浏览器中)还是存储在服务器上?

Server Side

You can store this state on the server side with or without a "database" depending on how pedantic you want to be about the term.

您可以在服务器端存储此状态,有或没有“数据库”,具体取决于您希望了解该术语的迂腐程度。

If what you want is to avoid configuring an SQL RDBMS, you might find that the built-in storage options from most Java Servlet containers will work. In Tomcat, you can just use your Session objects as normal, but configure a "File Based Store" instead of a "JDBC Based Store." This will store session data to disk in files. Alternatively you can use StandardManager which uses in-memory storage, but does not persist session state across restarts.

如果您想要避免配置SQL RDBMS,您可能会发现大多数Java Servlet容器中的内置存储选项都可以使用。在Tomcat中,您可以正常使用Session对象,但可以配置“基于文件的存储”而不是“基于JDBC的存储”。这会将会话数据存储到磁盘中的文件中。或者,您可以使用使用内存存储的StandardManager,但不会在重新启动时保持会话状态。

Put simply, these will create a Java Map for each JSESSIONID issued by your server, and then keep the maps in memory, on disk, or in a JDBC database. For more information see: https://tomcat.apache.org/tomcat-7.0-doc/config/manager.html

简而言之,这些将为服务器发出的每个JSESSIONID创建一个Java Map,然后将映射保存在内存,磁盘或JDBC数据库中。有关更多信息,请参阅:https://tomcat.apache.org/tomcat-7.0-doc/config/manager.html

Client Side

Here you have a few options as well. The driving factor is what level of browser you wish to support. If you can tolerate restricting your users to those who use a browser with HTML5 web storage and JavaScript enabled, things are pretty easy. If not, you can accomplish the same thing with a cookie.

这里有几个选项。驱动因素是您希望支持的浏览器级别。如果您可以容忍将用户限制为使用带有HTML5网络存储和启用JavaScript的浏览器的用户,那么事情就非常简单。如果没有,你可以用cookie完成同样的事情。

The big downside to client-side storage is trust. Users (or software on their computer) can modify client-side storage. This goes for cookies, localStorage, and sessionStorage. Many developers forget this and introduce security vulnerabilities because of it. If this is for a real production web application, you'll want to wrap your state in an authenticator.

客户端存储的一大缺点是信任。用户(或其计算机上的软件)可以修改客户端存储。这适用于cookie,localStorage和sessionStorage。许多开发人员忘记了这一点并因此引入了安全漏洞。如果这是一个真正的生产Web应用程序,您将希望将您的状态包装在身份验证器中。

Here's a the first in a three article series on a way to convince your servlet container to put session state into cookies in a way that is transparent to your servlets. It is missing authentication, but you can add it by following guidance such as this bit from Rob Winch.

这是关于说服servlet容器以对servlet透明的方式将会话状态放入cookie的方式的三篇系列文章中的第一篇。它缺少身份验证,但您可以按照Rob Winch的这一位指导添加它。

Now What?

Ok. You've decided to use client- or server-side storage for your checkbox values. Now what?

好。您决定使用客户端或服务器端存储来获取复选框值。怎么办?

A simple (usually wrong) option is to store the checkbox input names and values in a map:

一个简单的(通常是错误的)选项是将复选框输入名称和值存储在地图中:

{"boxFoo": true,"BarBox":false}

The reason this is usually wrong is that it fails to distinguish which form your user was visiting. It means that if you apply this strategy to more than one form on your site, you'll have to worry about name collisions.

这通常是错误的原因是它无法区分您的用户正在访问哪种形式。这意味着,如果您将此策略应用于网站上的多个表单,则必须担心名称冲突。

The next evolution is to have a structure keyed by form name and then field name. This would be a map like the following:

下一个演变是使用表单名称和字段名称键入结构。这将是如下地图:

{ "formA": {"boxFoo": true,"BarBox":false},
  "formQ": {"checkAlpha":true,"BetaCheck":false } }

This works, but will have annoying behavior when your users use multiple tabs. You can make that behavior more predictable for your users by using per-tab identifiers -- at the expense of space in your session object -- or by using AJAX to keep the fields in sync -- which has its own perils. Or you can do what most people do an just assume that the last submitted form overwrites the state from all previous ones, tabs be damned. That's much simpler to code, but more annoying to users.

这可行,但当用户使用多个标签时会产生烦人的行为。您可以通过使用每个选项卡标识符(以会话对象中的空间为代价)或使用AJAX使字段保持同步(这有其自身的危险),使您的用户更可预测该行为。或者你可以做大多数人做的事情,只是假设最后提交的表单覆盖了以前所有表单的状态,标签被诅咒。这对代码来说简单得多,但对用户来说更烦人。

#2


0  

You have to bind the value with a backing bean. As long as the backing bean is having the appropriate scope it will be retained on the page when you navigate to it.

您必须使用辅助bean绑定该值。只要辅助bean具有适当的范围,当您导航到它时,它将保留在页面上。

#3


0  

I can propose some ways :

我可以提出一些方法:

  • send http params (in hidden field) with check boxes flags which must stay checked in each new page requested by your application . You can factorize it with a function but it stays cumbersome to do.
  • 发送带有复选框标志的http参数(在隐藏字段中),这些标记必须在应用程序请求的每个新页面中保持检查状态。您可以使用函数对其进行分解,但这样做很麻烦。

  • store the check boxes marker flag in the http session. If the check boxes must stay checked in all the life of your user, it may be a suitable solution. Use may use a backing bean session for it as you use JSF. Nevertheless, store the minimum of information in it.
  • 将复选框标记标志存储在http会话中。如果必须在用户的整个生命周期内检查复选框,则可能是合适的解决方案。当您使用JSF时,使用可以使用支持bean会话。尽管如此,请将最少的信息存储在其中。

  • store the information in a shared applicative cache to retrieve it. In this way, you stay stateless and you have not the drawback of the session if you use clustering in your servers.
  • 将信息存储在共享的应用程序缓存中以检索它。这样,如果在服务器中使用群集,则保持无状态并且没有会话的缺点。

There is maybe better as alternative.

可能有更好的选择。