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

 

转载于:https://www.cnblogs.com/tomcuper/p/9492983.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值