Let $\mathscr{H}$ be a Hilbert space. If $\{e_{\alpha}\}_{\alpha \in I}$ is a Hilbert (orthonormal) basis, one can write every element $\psi \in \mathscr{H}$ as: \begin{eqnarray} \psi = \sum_{\alpha \in I}\langle e_{\alpha}, \psi\rangle e_{\alpha}. \tag{1}\label{1} \end{eqnarray} In quantum mechanics, one finds a more general form of this expression as follows. If $|x\rangle$ denotes an eigenvector of the position operator $\hat{x}$, then we use the set $\{|x\rangle\}_{x\in \mathbb{R}^{3}}$ as a basis to write: \begin{eqnarray} |\psi\rangle = \int dx\psi(x) |x\rangle \tag{2}\label{2} \end{eqnarray} where $\psi(x) := \langle x| \psi\rangle$. The simple generalization from (\ref{1}) to (\ref{2}) is non-rigorous, and although it works just fine, it has to be better explained in terms of mathematical rigor. However, I'm aware that expression (\ref{2}) can be obtained in rigorous mathematical terms by using either the spectral theorem for unbounded linear operators on Hilbert spaces (first developted by J. von Neumann) or by using the so-called Gelfand triple or Rigged Hilbert spaces.
Question: Can someone explain how to obtain expression (\ref{2}) rigorously using either one of the above approaches (spectral theorem or Rigged Hilbert spaces)?