SpringMVC默认访问首页配置 及web.xml配 weblogic配置

项目中遇到要解决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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值