简体   繁体   English

在网站上存储用户首选项的最佳方法?(不仅是字符串输入)

[英]Best way to store user preferences on a website?(Not just string entries)

Lets say I want to store user preferences, but they aren't just string and integer preferences. 可以说我想存储用户首选项,但不仅仅是字符串和整数首选项。 Some of them are say java objects such as Color, JFreeChart chart settings, etc. What is the best way to do that? 其中有些是说Java对象,例如Color,JFreeChart图表设置等。做到这一点的最佳方法是什么? Would YAML be an option and just serialize that data into the application? 是否可以选择YAML并将其序列化到应用程序中? I want to avoid having to write translation code converting strings into java objects. 我想避免不得不编写将字符串转换为Java对象的翻译代码。 Thanks. 谢谢。

An option is to use XStream . 一种选择是使用XStream

It's easy to use, needs no external config* and converts Java objects to/from XML or JSON. 它易于使用,不需要外部配置*,并且可以将Java对象与XML或JSON相互转换。

*If you want to make the serialized preferences more portable or readable, you will need a couple of annotations. *如果您想使序列化的首选项更具可移植性或可读性,则需要几个注释。 But if the objective is just to serialize/deserialize Java objects into/from a roughly human-readable format, you don't even need annotations. 但是,如果目标只是将Java对象序列化/反序列化为大致人类可读的格式,那么您甚至不需要注释。

You can use some standard object-to-string mappings like XML or JSON. 您可以使用一些标准的对象到字符串的映射,例如XML或JSON。 That way you just use a 3rd party library to do the conversion (no need to write your own) 这样,您仅需使用第3方库即可进行转换(无需自己编写)

Where do you want to store your preferences? 您想在哪里存储您的偏好? If they will go to a DB, use a ORM. 如果他们要去数据库,请使用ORM。 If serializing them to a file is good enough, you can use XStream as biziclop suggests, and it can serialize to JSON and XML 如果将它们序列化为文件就足够了,则可以按照biziclop的建议使用XStream,它可以序列化为JSON和XML

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android上的用户个人资料之间共享首选项的最佳方法 - Best way to share preferences between user profiles on Android 如何使用和存储用户首选项 - How to use and store User Preferences 表示对象中优先级/首选项的最佳方法是什么? - What is the best way to represent priorities/preferences in objects? 在共享首选项中存储char [] []的最快方法 - Fastest way to store char[][] in shared preferences 将ArrayList转换为字符串以存储在共享首选项中 - Converting ArrayList to String to store in Shared Preferences Play Framework 2存储用户密码哈希的最佳方法 - Play Framework 2 best way to store password hash of user 在mysql和java中存储Google用户ID值的最佳方法 - best way to store Google user-ID value in mysql and java 内存有效的方式来存储大列表 <Map<String,String> &gt;许多地图条目相同的地方 - Memory-efficient way to store large List<Map<String,String>> where many map entries are identical java-存储字符串数组的唯一列表的最佳方法 - java - best way to store unique list of string array 在 MYSQL 数据库中存储 base64 字符串的最佳方法是什么? - What is the best way to store base64 string in MYSQL database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM