用保存的 cookie 填写表格

2023-09-24Java开发问题
0

本文介绍了用保存的 cookie 填写表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个像这样保存我的 cookie 的操作类:

I have an action class that saves my cookies like this:

public String execute() {

    // Save to cookie
      Cookie name = new Cookie("name", userInfo.getName() );
      name.setMaxAge(60*60*24*365); // Make the cookie last a year!
      servletResponse.addCookie(name);
}

如果我提交表单,我可以在浏览器上看到已创建并保存的 cookie.当用户提交时,他们会被重定向到一个新页面,该页面包含他们刚刚创建的所有存储信息.

If I submit my form, I can see the cookies on the browser that has been created and saved. When the user submits, they get redirected to a new page, a page with all the stored information that they just created.

我希望用户能够返回提交页面并查看他们刚刚提交的表单中的所有信息.是否可以使用 Struts2 执行此操作,使用保存的 Cookie 并获取表单以填写旧数据?

I want the user to be able to go back to the submit page and see all the information in the forms that they just submitted. Is it possible to do this with Struts2, by using the saved Cookies and get the form to fill in with the old data?

这是我的表格:

<s:textfield
        label="Name"
        name="name"
        key="name" 
        tooltip="Enter your Name here"/>

推荐答案

要发送 cookie,您可以使用 cookie-provider 拦截器.它允许您通过实现 CookieProvider 在操作中填充 cookie.要将此拦截器应用于操作配置,您可以覆盖拦截器配置

To send cookie you can use a cookie-provider interceptor. It allows you to populate cookies in the action via implementing CookieProvider. To apply this interceptor to the action configuration you can override the interceptors config

<action ... >
  <interceptor-ref name="defaultStack"/>
  <interceptor-ref name="cookieProvider"/>
  ...
</action> 

CookieProvider有方法实现,

public class MyAction extends ActionSupport implements CookieProvider {

    @Override
    public Set<Cookie> getCookies(){
      Set<Cookie> cookies = new HashSet<>();
      Cookie name = new Cookie("name", userInfo.getName() );
      name.setMaxAge(60*60*24*365); // Make the cookie last a year!
      name.setPath("/"); //Make it at root.
      cookies.add(name);
      return cookies;
    }

}

在表格中

<s:set var="name">${cookie["name"].value}</s:set>
<s:textfield
        label="Name"
        name="name"
        value="%{#name}"
        tooltip="Enter your Name here"/>

这篇关于用保存的 cookie 填写表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13