springmvc啟動后跳轉到控制器而不是index.jsp默認進入controller的配置辦法


需要在web.xml中進行配置

welcome-file

為一個並不存在的路徑,但前邊帶一個斜杠,看起來像是一個路徑的樣子

建議使用這個方法,這樣不容易亂

    <!--先進入這個控制器-->
    <welcome-file-list>
        <welcome-file>/homepage</welcome-file>
    </welcome-file-list>  

然后對用的,在某個控制器里編寫這個方法

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hs.Controller;

import com.hs.model.UserModel;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author C
 */
@Controller

public class HomepageCtrlr {

    @RequestMapping("/")//匹配路徑/
    public ModelAndView homepage(HttpSession session, UserModel usermodel) throws Exception {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("view/front/homepage");
        return mav;
    }
}

方法2

welcome-file

前邊不寫斜杠

    <!--先進入這個控制器-->
    <welcome-file-list>
        <welcome-file>tohomepagectrlr</welcome-file>
    </welcome-file-list>  

 

然后就會進入路徑為 / 的控制器

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hs.Controller;

import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author C
 */
@Controller

public class MainpageCtr {
/**
 * 網站打開后,進入這個控制器,而不是進入jsp頁面
 * @param request
 * @return 
 */
    @RequestMapping("/")//匹配路徑/
    public ModelAndView tohomepagectrlr(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView();      
        mav.setViewName("view/front/homepage");
        return mav;
    }
}

 


免責聲明!

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



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