2010-03-11 50 views
4

我正在使用Spring 3,並嘗試使用註釋來設置一個簡單的Web應用程序來定義控制器映射。如果不用* .form或* .doSpring 3使用基於註釋的映射進行簡單的無延伸url映射 - 不可能嗎?

胡說所有的網址,這似乎是非常困難的。 web.xml中有一個<security-constraint>,用於保護該根目錄下的所有內容。我想將所有的Spring控制器映射到/ secure/app /。

範例網址是:
/安全/應用/的LandingPage
/安全/應用/編輯/客戶/ {ID}
各自的,我會用適當的JSP/XML /任何處理 。

所以,在web.xml中我有這樣的:

<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> 

而在調度員-servlet.xml中我有這樣的:

<context:component-scan base-package="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; 
    } 
} 

Under/WEB-INF/jsp我有一個「main.jsp」,並設置了一個合適的視圖解析器來指向這個。使用* .form映射調度程序時,我的工作有效,但使用上述代碼無法獲取任何工作。

春啓動時,它似乎映射一切正常:

13:22:36,762 INFO main annotation.DefaultAnnotationHandlerMapping:399 - Mapped URL path [/secure/app/main] onto handler [[email protected]] 

我也注意到了這條線,這看起來很可疑:

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

,並在運行時間的任何企圖查看/安全/ 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 m aps一個URL,然後在第二秒後「忘記」那個映射?到底是怎麼回事?

謝謝。

回答

3

我和你有完全一樣的問題。設置'alwaysUseFullPath'的方法非常簡單。我的conf文件如下:

<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

啊。像往常一樣,發佈問題後發現答案:-)

將RequestMapping註釋更改爲/主修復了問題。關於如何規定所有這些文件,文件不是很清楚。

0

把下面的內容你@Configuration類:

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