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;
    }
}
复制代码

 

posted @   不打鱼光晒网  阅读(987)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· Open-Sora 2.0 重磅开源!
历史上的今天:
2019-08-22 mysql不创建表 <property name="hbm2ddl.auto">update</property> 无效
2019-08-22 fastjson循环引用 问题@ManyToOne @OneToOne返回数据中"$ref"问题
2019-08-22 org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider]
2019-08-22 mysql
点击右上角即可分享
微信分享提示