1. 程式人生 > >java web設定預設首頁方法

java web設定預設首頁方法

本文部分來自:https://blog.csdn.net/caiwenfeng_for_23/article/details/45486233

引文:在構建專案後,我們在使用Tomcat啟動後設置預設首頁方法有以下幾種:

1.靜態頁面

在tomcat安裝目錄下,/conf/web.xml 中,新增以下內容:

  1. <welcome-file-list>

  2. <welcome-file>/WEB-INF/views/index.jsp</welcome-file>

  3. </welcome-file-list>

2. 動態頁面

同樣是在web.xml中servlet中加入相關配置:

其中:

url-pattern部分即為訪問時配置的預設主頁。

3.spring-MVC

(1)如果在tomcat容器中沒有找到配置的預設頁面,就會去spring-mvc中去尋找/index的controller,如果有則會去進行呼叫,否則就會顯示404頁面:

  1. @RequestMapping(value=”/index”)

  2. public ModelAndView index(HttpServletRequest request, HttpServletResponse response){

  3. return new ModelAndView(“index”);

  4. }

(2)spring-mvc配置根節點訪問“/”方式

  1. @RequestMapping(value=”/”)

  2. public ModelAndView index(HttpServletRequest request, HttpServletResponse response){

  3. return new ModelAndView(“index”);

  4. }

以上三種方法級別高低依次為: 1 - > 2 -> 3 (2)-- >3(1)