项目中遇到要解决web项目访问默认首页地址的需求。网上好多资料
参考这篇文章解决了问题
之前访问主页地址为:http://xxxxxxx/xxxx/xxxx/index.htm 但由于要求,需要直接http:ip:xxxx/,直接访问地址就转到主页(index.htm是配置的controller地址)
<!--web.xml默认配置-->
<welcome-file-list>
<welcome-file>/index.html</welcome-file>
</welcome-file-list>
//controller配置
@RequestMapping({"/index.htm"})
public ModelAndView indexCore(HttpServletRequest request, HttpServletResponse response){
ModelAndView mv = new JModelAndView("/core/index.html", request, response);
return mv;
}
开始想着这不简单嘛,,直接把web.xml的默认主页写为controller访问地址不就行了,所以就改成了这样:
/index.htm 访问后果然报了404。o(╯□╰)o 不然就会当做普通的静态页面文件去访问,所以就不会找到相应的controller。(谁把controller访问地址写成/index.htm的,粗来我不打死你╭∩╮(︶︿︶)╭∩╮)所以修改如下:
<!--web.xml默认配置-->
<welcome-file-list>
<welcome-file>index</welcome-file>
</welcome-file-list>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/index</url-pattern>
</servlet-mapping>
注意
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
需要有两个才行的url-pattern,一个url配置访问地址index 一个只配置 / 为什么需要两个不能直接写还需要研究?
//controller配置
@RequestMapping({"/index"})
public ModelAndView indexCore(HttpServletRequest request, HttpServletResponse response){
ModelAndView mv = new JModelAndView("/core/index.html", request, response);
return mv;
}
然后直接输入http://xxxxxxx/xxxx访问成功。
不过还有一种简单直接粗暴的:
<!--web.xml默认配置-->
<welcome-file-list>
<welcome-file>/</welcome-file>
</welcome-file-list>
//controller配置
@RequestMapping({"/"})
public ModelAndView indexCore(HttpServletRequest request, HttpServletResponse response){
ModelAndView mv = new JModelAndView("/core/index.html", request, response);
return mv;
}
直接都使用\,这样也能达到目的。
实际项目是在weblogic中部署
也需要在项目工程的web-inf 中的weblogic.xml
中配置
<context-root>/</context-root>
如果需要访问的路径为http:ip:port/xxxx
那就把地xxxx配置的其中,直接访问就可以
<context-root>xxxx</context-root>