Help us understand the problem. What is going on with this article?

Apexでcookieを扱う

ちょっと普通のcookieと違う

apexでcookieを扱う際にはちょっとした注意が必要になります。

スクリーンショット 2019-09-13 19.39.56.png

スクリーンショット 2019-09-13 19.40.41.png

こんな感じでapex__がつきます。

保存の仕方としてはapexで保存する方法と、Javascriptを使う方法があります。

今回はjavascriptで保存し、apex側で呼び出してセットしてみました。

sample.js
function hoge(){
            actfun();
            //入力内容を取得
            var pname = $("[id*=pname]").val();
            var pfamily = $("[id*=pfamily] option:selected").val();
            var pcode = $("[id*=pcode]").val();
            //パスの指定
            var path = "path=/";
            document.cookie = 'apex__conditions.Name='+pname;
            document.cookie = 'apex__conditions.Family=' + pfamily;
            document.cookie = 'apex__conditions.ProductCode=' + pcode;
        }
sample.apex
    public String getNameval() {
        //'apex__'を外したcookie名を指定して取得する
        Cookie Name = ApexPages.currentPage().getCookies().get('conditions.Name');
        return Name == null ? '' : Name.getValue();
    }

こんな感じで取得します。

それ以外は他のcookieとほとんど変わらない挙動かと思います。

注意点はapex__こいつってことですね。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away