springMVC訪問根路徑問題


當web.xml沒有配置歡迎頁:如下

<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>

此情況下,web服務器會有缺省的配置如下:可以修改

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

假設我們的項目域名是www.**.com,此時默認會在項目目錄下找index.html...等默認的歡迎頁.項目路徑下沒有這些頁面,會報404錯誤.

如何設置SpringMVC 對根路徑進行攔截?

1.在web.xml中加入

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

此時,web服務器就知道,根路徑不要web服務器來處理,而是由程序自身來處理。

然后在spring的配置文件中加入<mvc:view-controller path="/" view-name="forward:/index"/> :表示當訪問主頁時自動轉發到index控制器 

<mvc:view-controller path="/" view-name="login/login"/>  :表示跳轉到login.jsp頁面

或者可以定一個這樣的類:

@Controller
public class mainController {
    
    @RequestMapping("{param}")
    public String main(@PathVariable("param") String param) {
        System.out.println("進入Maincontroller");
        return param;
    }
    
    //當訪問url為:http://127.0.0.1:8080/HRMN/ 直接跳轉到登錄頁面
    @RequestMapping("/")
    public String login() {
        return "login";
    }
    
}

 

 

 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM