Background
I've met this problem when I was trying to convert a elliptic PDE problem into the corresponding variational problem in order to apply finite element method.
The PDE is an elliptic PDE with non-zero Dirichlet boundary condition:
Denote
$$
Lu=-\nabla\cdot(a\nabla u)+bu
$$
Then the equation is
$$
\left\{\!\!
\begin{aligned}
&Lu=f,x\in\Omega\\
&u|_{\partial \Omega}=g
\end{aligned}
\right.
$$
When $g\equiv0$, I know the corresponding variational problem is
find $u\in H_0^1(\Omega)$, such that
$$
a(u,v)=(f,v), \forall v\in H_0^1(\Omega)
$$
where
$$
\begin{aligned}
a(u,v)&:=\int_\Omega a\nabla u\cdot\nabla v\,dx+\int_\Omega buv\,dx\qquad \\
(f,v)&:=\int_\Omega fv\,dx,\qquad \forall u,v\in H_0^1(\Omega)
\end{aligned}
$$
(This is actually the weak form of the original PDE.)
Here comes my problem:
For general g,
if I can find a function $w\in H^1$ such that $w|_{\partial\Omega}=g$, by letting $\tilde u=u-w$,
we have
$$
\left\{\!\!
\begin{aligned}
&L\tilde u=\tilde f,x\in\Omega\\
&\tilde u|_{\partial \Omega}=0
\end{aligned}
\right.
$$
whose solution is already known.
So how to find such a $w$?