一直使用spring mvc来做web开发,发现在访问域名,例如http://www.domain.com/时,却不得不在首页加一个index.jsp来跳转到实际的controller。今天特地上网搜索解决方法,原来蛮简单的!
首先在web.xml加入以下代码:
<welcome-file-list>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
然后在DispatcherServlet加入对index.htm的拦截
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/index.htm</url-pattern>
</servlet-mapping>
然后在DispatcherServlet-servlet.xml加入以下代码
<bean id="simpleUrlHandlerMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/index.htm">indexController</prop>
</props>
</property>
</bean>
最后在IndexController加入RequestMapping
@RequestMapping("/index.htm")
public String index() {
return "hello";
}
本文介绍如何配置SpringMVC使访问域名时能够直接映射到控制器,避免使用index.jsp进行跳转。通过在web.xml中设置欢迎文件列表,并在DispatcherServlet中添加对首页的映射。
2617

被折叠的 条评论
为什么被折叠?



