SpringMVC默认欢迎页面的问题

1.默认tomcat容器的默认页面。

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

这种方式适合访问静态的页面(也包括JSP)或者说是没有任何参数的页面。

2.SpingMVC配置控制器的代码

  1. web.xml配置
    <!-- 配置Spring MVC DispatcherServlet -->
    <servlet>
        <servlet-name>MVC DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 初始化参数 -->
        <init-param>
            <!-- 加载SpringMVC的xml到 spring的上下文容器中 -->
            <param-name>contextConfigLocation</param-name>
            <!-- 下面这个参数是指定springmvc的配置文件所在 -->
            <param-value>classpath:springmvc-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- 配置DispatcherServlet所需要拦截的 url -->
    <servlet-mapping>  
      <servlet-name>MVC DispatcherServlet</servlet-name>  
      <url-pattern>/index</url-pattern>  
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>MVC DispatcherServlet</servlet-name>
        <!-- <url-pattern>/</url-pattern> 这样配置的话,所有页面都会进入拦截器 这个在springmvc 的配置文件里还有对其进行再次配置 -->
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

<!-- 欢迎页 -->
    <welcome-file-list>
        <welcome-file>index</welcome-file>
    </welcome-file-list>

2.控制器中添加默认首页控制器

    @RequestMapping("/index")
    public String homepage(Model model){
        model.addAttribute("indexMsg", "hello,this is index.jsp form homepage/index.do");
        System.out.println("this is homepage!!!");
        return "index";
    }

3. 默认页跳转

1.web.xml配置

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

2.index.jsp页面跳转到Controller

<jsp:forward page="/index"></jsp:forward>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

songhuageini

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值