Scalate and Spring MVC without ServletConfig

143 views
Skip to first unread message

Matthew Farwell

unread,
Jan 31, 2013, 2:56:12 AM1/31/13
to sca...@googlegroups.com
This is fairly long, but there are questions and possible pull requests inside.

I was following the spring MVC example in 1.6.1, and in the web.xml the spring configuration is an init-param to the DispatcherServlet. This works fine.

However, one of the "standard" ways of configuring spring in a webapp is to use a listener:

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

And this doesn't work. templateEngine in ScalateViewResolver is never initialised because setServletConfig() is never called (because there is no config for the DispatcherServlet). A solution is to use initServletContext(), which does get called:

public class MyResolver extends ScalateViewResolver {
  protected void initServletContext(final ServletContext servletContext) {
    super.initServletContext(servletContext);
    
    super.setServletConfig(new ServletConfig() {
      public String getServletName() { return "unknown"; }
      public ServletContext getServletContext() { return servletContext; }
      public Enumeration<String> getInitParameterNames() { return null; }
      public String getInitParameter(String arg0) { return null; }
    });
  }
}

And this seems to work. So, question 1: does this seem a reasonable approach? What are the advantages / disadvantages of this?

I know that I can't specify parameters to ServletTemplateEngine such as boot.class, compiler.classpath.prefix, compiler.classpath.suffix, but I could always pass them in manually, use context parameters in that case.

Question 2: should I do a PR to add initServletContext into ServletViewResolver which does pretty much the same as the above. I could add context params for the boot.class etc is necessary. I'd need to avoid initialising twice of course.

Question 3: I spent a day or so trying to work this out. Do you want me to do a PR with a second example for spring mvc (in scala or java)?
Also, it took me a while to figure out how to set up the model so that I could pass attributes to and use in the template, which I think is important for people to know. So the PR would include this, and probably form submission as well. I'm using scaml.

Thanks.

Matthew Farwell.

Matthew Farwell

unread,
Feb 8, 2013, 8:42:38 AM2/8/13
to sca...@googlegroups.com
I'm sending this question again because I didn't receive any answer the last time. Does anyone have any responses to the questions below? I'd like to do a pull request, but I want to be sure that I'm going in the right direction.

Thanks.

Matthew Farwell.

2013/1/31 Matthew Farwell <matthew...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "Scalate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalate+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Ross A. Baker

unread,
Feb 9, 2013, 1:42:32 AM2/9/13
to sca...@googlegroups.com
A ServletContext can cleanly implement most of the Config trait needed
by the ServletTemplateEngine constructor. `getName` requires an
arbitrary dummy value, but the rest falls right into place. That
conversion would be a win if there is anywhere else where we have to
configure from the ServletContext.

https://github.com/scalate/scalate/blob/scala_2.10/scalate-core/src/main/scala/org/fusesource/scalate/servlet/Config.scala

I would return an empty Enumeration for getInitParameterNames rather than null.
> --
> You received this message because you are subscribed to the Google Groups
> "Scalate" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to scalate+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
Ross A. Baker
ba...@alumni.indiana.edu
Indianapolis, IN, USA

Matthew Farwell

unread,
Feb 19, 2013, 5:28:10 PM2/19/13
to sca...@googlegroups.com
Thanks for the reply.

There is now a pull request https://github.com/scalate/scalate/pull/49. If anybody would like to, they can comment on it.

Thanks.

Matthew Farwell.

2013/2/9 Ross A. Baker <ba...@alumni.indiana.edu>
Reply all
Reply to author
Forward
0 new messages