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