2016-04-29 48 views
0

我可以使用EL在我的彈簧控制器上使用類似@RequestMapping("${pageContext.request.contextPath}")的東西來指定我的應用程序上下文路徑嗎?其實,我得到一個404錯誤。所以我的問題是如何映射控制器上下文根?我可以在控制器上的@RequestMapping中指定應用程序路徑嗎?

我相信,當應用程序是不是單獨的服務器上@RequestMapping("/")不能做的工作,和URL看起來像: http://localhost:8080/myapp/mypage

@Controller 
@RequestMapping("${pageContext.request.contextPath}") 
public class MainController { 

    @RequestMapping("/") 
    public ModelAndView index() { 

    } 
} 

PS:我已經添加了應用程序路徑前綴爲每一個URL鏈接我的JSP,如<form class="form-inline" role="form" action="${pageContext.request.contextPath}/search" method="post">。現在,我需要爲我的Spring控制器製作相同的技巧,使其能夠在......將應用程序部署爲ROOT應用程序或作爲「該服務器上的另一個應用程序」時運行。

+0

你想添加上下文路徑爲每一個網址的前綴? –

+0

這沒有任何意義。映射已經相對於上下文根。 Spring的DispatcherServlet映射到'/'。 Servlet容器已經解決了應用程序的Servlet應該被調用。 –

回答

1

您可以在application.properties配置文件中添加根上下文路徑。該根上下文路徑將成爲所有控制器路徑的前綴路徑。

application.properties

server.contextPath =/MyApp的/我的空間

控制器

@RequestMapping( 「/測試」)

結果:

http://localhost:8080/myapp/mypage/test

相關問題