0

I have some content that needs dynamic loading in my Controller and some content that I want to deliver in a static manner.

Currently I have to configure the path of the controller manually in dispatcher-servlet.xml, e.G. the @RequestMapping isn't working. If I remove the <mvc:resources mapping="/static/**" location="/static/" /> line from my dispatcher-servlet.xml the annotation is properly read again.

I've tried to change the context url for dispatcher servlet in web.xml and place the static pages outside of springs control, but that didn't work either, so I reverted it.

My goal is to remove the urlMapping bean from dispatcher-servlet.xml and have that done by the annotations. As far as I understood that should also get rid of the AbstractController and the handleRequestInternal parts.

What confuses me is that I can use only the annotations when I leave out the static resources line.

Here are the relevant exerts of my current state:

dispatcher-servlet.xml

<context:component-scan base-package="some.pkg.containing.controllers" />
<mvc:resources mapping="/static/**" location="/static/" />
<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
  p:prefix="/WEB-INF/jsp/"
  p:suffix=".jsp" />

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <property name="mappings">
    <props>
      <prop key="/dashboard/*">dashboardController</prop>
    </props>
  </property>
</bean>

web.xml

<servlet>
  <servlet-name>dispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>dispatcher</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

Controller in package:

@Controller
@RequestMapping("/dashboard")
public class DashboardController extends AbstractController {
  public ModelAndView getSite() {
    ModelAndView retVal = new ModelAndView("dashboard_base");
    retVal.addObject("url", "/dashboard");
    return retVal;
  }
  @Override
  protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    return getSite();
  }
}
Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72

1 Answers1

0

You must tell the mvc part of spring that it should honor your annotations as well. So if you add this line to your dispatcher-servlet.xml it will work:

<mvc:annotation-driven />
Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72