Spring 3简单的无扩展url映射与基于注释的映射 – 不可能?
我正在使用Spring 3,并尝试使用注释来设置一个简单的Web应用程序来定义控制器映射。 如果没有使用* .form或* .do对所有url进行操作,这似乎是非常困难的
由于网站的某些部分需要受密码保护,因此这些url都在/安全之下。 web.xml中有一个保护该根目录下的所有内容。 我想将所有Spring控制器映射到/ secure / app /。
示例url将是:
/安全/应用/的LandingPage
/安全/应用/编辑/客户/ {ID}
我会用适当的jsp / xml /其他方法来处理。
所以,在web.xml中我有这个:
dispatcher org.springframework.web.servlet.DispatcherServlet 1 dispatcher /secure/app/*
在despatcher-servlet.xml中我有这个:
在Controller包中,我有一个控制器类:
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; } }
在/ WEB-INF / jsp下我有一个“main.jsp”,并设置了一个合适的视图解析器来指向它。 我使用* .form映射调度程序时有些工作,但是使用上面的代码无法正常工作。
当Spring启动时,它似乎正确地映射了一切:
13:22:36,762 INFO main annotation.DefaultAnnotationHandlerMapping:399 - Mapped URL path [/secure/app/main] onto handler [controller.HomePageController@2a8ab08f]
我也注意到这条线看起来很可疑:
13:25:49,578 DEBUG main servlet.DispatcherServlet:443 - No HandlerMappings found in servlet 'dispatcher': using default
在运行时,任何查看/ secure / app / main的尝试都会在Tomcat中返回404错误,并输出以下日志:
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
那么…… Spring会映射一个URL,然后在一秒钟后“忘记”该映射吗? 到底是怎么回事?
谢谢。
啊。 像往常一样,在发布问题后找到答案:-)
将RequestMapping注释更改为just / main可以解决问题。 关于如何指定所有这些文档并不十分清楚。
在你的@Configuration类中加入这样的东西:
@Bean(autowire = Autowire.BY_TYPE) public AnnotationMethodHandlerAdapter handlerAdapter(){ final AnnotationMethodHandlerAdapter annotationMethodHandlerAdapter = new AnnotationMethodHandlerAdapter(); annotationMethodHandlerAdapter.setAlwaysUseFullPath(true); return annotationMethodHandlerAdapter; }
- 当我使用autowire和组件注释时,不会注入或创建我的spring bean
- JUnit4根据自定义java注释跳过测试
- spring-nullpointerexception-无法在无注释类中访问自动连接的带注释的服务(或dao)
- JSF如何找到使用@ManagedBean注释的bean?
- 如何实现`Jackson AnnotationIntrospector`?
- 当注释具有参数时,CDI拦截器不起作用
- @WebParam的@XmlElement(required = true)不起作用
- 在IntelliJ和Eclipse开发人员都在工作的项目中使用@NotNull
- 如何让@ParametersAreNonnullByDefault工作?