Spring 3 simple extentionless url mappings with an

2020-07-18 04:03发布

问题:

I'm using Spring 3, and trying to set up a simple web-app using annotations to define controller mappings. This seems to be incredibly difficult without peppering all the urls with *.form or *.do

Because part of the site needs to be password protected, these urls are all under /secure. There is a <security-constraint> in the web.xml protecting everything under that root. I want to map all the Spring controllers to /secure/app/.

Example URLs would be:
/secure/app/landingpage
/secure/app/edit/customer/{id}
each of which I would handle with an appropriate jsp/xml/whatever.

So, in web.xml I have this:

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/secure/app/*</url-pattern>
</servlet-mapping>

And in despatcher-servlet.xml I have this:

 <context:component-scan base-package="controller" />

In the Controller package I have a controller class:

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/secure/app/main")
public class HomePageController {
    public HomePageController() { }

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView getPage(HttpServletRequest request)
    {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("main");
        return mav;
    }
}

Under /WEB-INF/jsp I have a "main.jsp", and a suitable view resolver set up to point to this. I had things working when mapping the despatcher using *.form, but can't get anything working using the above code.

When Spring starts up it appears to map everything correctly:

13:22:36,762  INFO main annotation.DefaultAnnotationHandlerMapping:399 - Mapped URL path [/secure/app/main] onto handler [controller.HomePageController@2a8ab08f]

I also noticed this line, which looked suspicious:

13:25:49,578 DEBUG main servlet.DispatcherServlet:443 - No HandlerMappings found in servlet 'dispatcher': using default

And at run time any attempt to view /secure/app/main just returns a 404 error in Tomcat, with this log output:

13:25:53,382 DEBUG http-8080-1 servlet.DispatcherServlet:842 - DispatcherServlet with name 'dispatcher' determining Last-Modified value for [/secure/app/main]    
13:25:53,383 DEBUG http-8080-1 servlet.DispatcherServlet:850 - No handler found in getLastModified    
13:25:53,390 DEBUG http-8080-1 servlet.DispatcherServlet:690 - DispatcherServlet with name 'dispatcher' processing GET request for [/secure/app/main]    
13:25:53,393  WARN http-8080-1 servlet.PageNotFound:962 - No mapping found for HTTP request with URI [/secure/app/main] in DispatcherServlet with name 'dispatcher'    
13:25:53,393 DEBUG http-8080-1 servlet.DispatcherServlet:677 - Successfully completed request

So... Spring maps a URL, and then "forgets" about that mapping a second later? What is going on?

Thanks.

回答1:

I have exactly the same problem as you. The way to set 'alwaysUseFullPath' is pretty straightforward. My conf file is as following:

<bean
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"
    p:order="3" > <!-- a higher value meaning greater in terms of sorting.  -->
    <property name="alwaysUseFullPath" value="true" />
    <property name="interceptors">
        <list>
            <ref local="myInterceptor" />
        </list>
    </property>
</bean>

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="alwaysUseFullPath" value="true" />
</bean>


回答2:

Ah. As usual, found the answer after posting the question :-)

Changing the RequestMapping annotation to just /main fixes the problem. The documentation is not very clear on how all this is specified.



回答3:

Put something like this in your @Configuration class:

@Bean(autowire = Autowire.BY_TYPE)
public AnnotationMethodHandlerAdapter handlerAdapter(){
final AnnotationMethodHandlerAdapter annotationMethodHandlerAdapter = new AnnotationMethodHandlerAdapter();
annotationMethodHandlerAdapter.setAlwaysUseFullPath(true);
return annotationMethodHandlerAdapter;
}