问题
I have a JSP and in that JSP I have a link which looks something like
http://mydomain.com/verify.do?email=emailid&id=123
Now when users clicks that link, it goes to my velocityview servlet, now my question is how do I pull out those parameters in velocity servlet and put it into the context so that I can send it to my template.
I tried getparameter,#set(#request.getAttribute(email) etc.But for some reason I dont see the value getting into the context or template.
I know there is velocity parameter tool out there,but I am not able to find as how to install and use it.So appreciate if someone can point me to right direction.
回答1:
The solution is the Velocity context. In your backing Java class or Velocity servlet, read in the parameters and put them in the context:
context.put("email", request.getParameter("email"));
context.put("id", request.getParameter("id"));
Then in your Velocity template you can reference them as $email
and $id
.
回答2:
In the accepted answer you have to explicitly unwind all the arguments in your controller. So if you have 5 fields, you have 5 of the above statements.
I would suggest:
context.put("request", request);
Then in your template something like this:
<p>Is the Jump Run Heading Good? <b>$request.getParameter("heading")</b></p>
Maybe you could write a little macro to avoid that last little bit of ugliness. You write a little helper function to loop through the request parameters names with getParameterNames() and call getParameter() on each.
来源:https://stackoverflow.com/questions/13704870/how-do-i-pass-get-request-parameters-to-my-velocity-template-to-send-mails