SpringMVC默认首页设置

来源:互联网 发布:百度蜘蛛软件 编辑:程序博客网 时间:2024/05/07 14:04

    使用SpringMVC框架搭建Web,在web.xml中添加

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

发现不能识别首页,看来不是像Web工程一样进行简单的设置。

    SpringMVC使用拦截机制,需要进行Servlet映射,添加一个新的扩展名,总而言之,需要在web.xml中加入以下设置:

<servlet-mapping>    <servlet-name>springmvc</servlet-name>    <url-pattern>/index</url-pattern></servlet-mapping>

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

    根据SpringMVC机制,数据被封装成请求后会去寻找相应的控制器(controller),根据你在applicationContext.xml中的设置,寻找控制器的地址,比如:

<context:component-scan base-package="com.wisu.xitiancheng">    <context:exclude-filter type="regex" expression="com.wisu.xitiancheng.control.*"/></context:component-scan>

    在控制器中添加默认首页控制器,比如:

/*** 默认首页控制器*/@RequestMapping("/index")public String login(HttpServletRequest request, HttpServletResponse response){    return "index";}
//根据返回的值通过视图解析器查找相应的页面返回给用户

0 0
原创粉丝点击