1. 程式人生 > 其它 >ssm專案設定首頁

ssm專案設定首頁

一、welcome-file-list

1. 如何設定首頁

<welcome-file-list>標籤下可以設定多個首頁<welcome-file>,容器啟動後會在根目錄下依次查詢匹配的物理存在的檔案,返回第一個找到的檔案,沒有找到報404錯誤。

<welcome-file-list>
    <welcome-file>a.jsp</welcome-file>
    <welcome-file>b.html</welcome-file>
    <welcome-file>c.html</welcome-file
> </welcome-file-list>

首頁也可以是WEB-INF目錄下的檔案。

<welcome-file-list>
    <welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
</welcome-file-list>

2. 注意點

首頁的路徑只能是一個實際存在的物理檔案地址,不能將首頁設定成Servlet或Controller的地址,再通過來Servlet或Controller返回一個頁面,例如以下,tomcat會在根目錄下的view資料夾查詢addUser檔案,找不到則報404.

<welcome-file-list>
  <welcome-file>/view/addUser</welcome-file>
</welcome-file-list>

如果確實需要使用控制器來返回頁面,可以通過在頁面中跳轉到控制器路徑來實現。建立首頁檔案home.html,內容如下,URL為跳轉路徑,再將該頁面設定為主頁。

<html> 
<head>
  <meta http-equiv="Refresh" content="0; URL=/view/addUser">
</head>
</html
>

3. 可以不設定首頁,由SpringMVC來處理根路徑應該返回的頁面,注意2中是設定首頁為控制器路徑,此處是不設定首頁,不要混淆。不設定首頁直接去掉welcome-file-list標籤或者

<welcome-file-list>
  <welcome-file></welcome-file>
</welcome-file-list>

對根路徑進行對映

@RequestMapping("/")
public String indexPage() throws Exception {
    return "index"; 
    }

或使用<mvc:view-controller />,有2種方法

1. 重定向
<mvc:view-controller path="/" view-name="redirect:/view/index"/>
即如果當前路徑是/ 則重定向到/view/index
2. view name
<mvc:view-controller path="/" view-name=admin/index"/>

如果當前路徑是/ 則交給相應的檢視解析器直接解析為檢視 。

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"p:order="2">
      <propertyname="viewClass"value="org.springframework.web.servlet.view.JstlView"/>
      <propertyname="contentType" value="text/html"/>
      <property name="prefix"value="/WEB-INF/jsp/"/>
      <property name="suffix"value=".jsp"/>
</bean>

https://blog.csdn.net/xybz1993/article/details/81023955

TRANSLATE with x English
Arabic Hebrew Polish
Bulgarian Hindi Portuguese
Catalan Hmong Daw Romanian
Chinese Simplified Hungarian Russian
Chinese Traditional Indonesian Slovak
Czech Italian Slovenian
Danish Japanese Spanish
Dutch Klingon Swedish
English Korean Thai
Estonian Latvian Turkish
Finnish Lithuanian Ukrainian
French Malay Urdu
German Maltese Vietnamese
Greek Norwegian Welsh
Haitian Creole Persian
TRANSLATE with COPY THE URL BELOW Back EMBED THE SNIPPET BELOW IN YOUR SITE Enable collaborative features and customize widget: Bing Webmaster Portal Back