Re: [Webwork-user] WebWorkVelocityServlet views
Brought to you by:
baldree,
rickardoberg
From: Bill B. <bi...@pr...> - 2002-07-11 06:55:49
|
Hello, See below ... "G.L. Grobe" wrote: > > Still trying to figure this one out. Below is my original action where I > would compare the values of 'view' and return a view mapping pending the > value of view ... > > public class Products extends ActionSupport implements ParameterAware { > private Map params; > ... > public void setParameters(Map map) { this.params = map; } > > public String doExecute() throws Exception { > > if (view.equals("myview.a")) { > return "selectA.success"; > } > else if (view.equals("myview.b")) { > return "selectB.success"; > } > > return "select.error"; > } > > Then I needed to capture the parameter from within the action (coming from > the velocity *.vm template) so I changed the class I extended from and now > have ... The WebWorkVelocityServlet is part of the _view_, not the model or controller so it has nothing to do with the Action interface and as such, the ServletDispatcher will never invoke it as an Action. Instead, it will _forward_ to it just like it would a JSP if you have the mappings defined appropriately. As a result, the ParameterAware interface will have no effect they way you're using it. WebWorkVelocityServlet exposes the HttpServletRequest in the Velocity context as $req (see the createContext method and in VelocityServlet.java the static final REQUEST). So there's no need to extend WebWorkVelocityServlet (for this purpose). The bottom line is there's nothing extra you need to do to access request parameters within a Velocity template--just call $req.getParameter("foo") or whatever method you want to call. > public class Products extends WebWorkVelocityServlet implements > ParameterAware { > private Map params; > ... > public void setParameters(Map map) { this.params = map; } > > public Template handleRequest(Context context) { > HttpServletRequest request = (HttpServletRequest) > context.get(REQUEST); > HttpServletResponse response = (HttpServletResponse) > context.get(RESPONSE); > } > } With the code below, it will throw a NullPointerException if myParam doesn't exist. > As I understand it, I now have the handleRequest() method above so that I > can get a query string parameter from the servlet now and write something > like this in the template page ... > --- *.vm file > #if ( $reqParser.getParameter("myParam").equals("this_string") ) > ## do something > #end Instead, do this which will work whether myParam exists or not ... #if ( $req.getParameter("myParam") == "this_string" ) ## do something #end Another way of doing this using the ParameterAware interface is as follows. Assuming your Products Action class above has getMyParam() and setMyParam(String) methods. The setMyParam method will be called if there's a "myParam" parameter. Then, within the Velocity template forwarded to from that Action you can access it like this: ## assumes the Products Action is mapped to select. #if ( $select.MyParam ) ## same as $select.getMyParam() ## do something #end > The problem is that whereas I had the execute() method from the first ex. > (extended from ActionSupport) to compare views and return a different view > pending on the value of view, I don't know how to do this (return views > pending a query string parameter in the servlet request) w/ the > WebWorkVelocityServlet. If you want to access the existing parameters from the current request to the action, they will also be available to any Velocity template as shown above. > So what I have is a query string coming from the template to the action, and > then I'll return a view mapping w/ query string parameters back to the > browser. If you're trying to create URL's in the Velocity template that pass through the parameters, I'm not sure of the best way to do that. The #url() macro can be used to generate a URL. Parameters can be added and the end result will be properly encoded. > Any help much appreciated. Hope this helps, -Bill |