SPRINGMVC Project configuration Welcome page for index.html

Source: Internet
Author: User

First, the question

Adding the following configuration in Web. XML is invalid

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

Access http://localhost/KingWeixin/No effect

Second, solve the problem 2.1, the problem analysis
1Default page for the default Tomcat container. /index.html is a good way to access static pages (including JSPs) or pages that don't have any parameters. 2. SPIRNG MVC Default Index Controller mode if the Tomcat container does not have a default page configured, how spring MVC will proactively look for/The controller for index, if any, is called, and no 404 page is displayed. @RequestMapping (Value= "/index ") PublicModelandview Index (httpservletrequest request, httpservletresponse response) {return NewModelandview ("index");}3. spirng MVC Configuration root node Access "/"The way this method is more extreme, is to configure a named"/"Controller is called after the URL has been entered. This method is not configured for the previous two methods. @RequestMapping (Value= "/") PublicModelandview Index (HttpServletRequest request, httpservletresponse response) {return NewModelandview ("index");} The level of the three methods:1>>3>>2Because Tomcat has a higher container level than spring, and the above 3-clock configuration is the case, use Tomcat first. Because the controller is configured with a "/", it is matched to the relevant controller without first looking for/Index Controller. Note that even if Web. XML is not added, Tomcat will automatically default to find the index file under the Webroot directory, and if you want to use the following two methods, make sure that there are no index-related files under Webroot. Comprehensive experience, the third method is the most convenient way to use this example: @RequestMapping ("/") PublicModelandview Index (Modelandview modelandview, httpservletrequest request, String openId) {return NewModelandview ("redirect:/tologin.do");} @RequestMapping ("/tologin.do") PublicModelandview tologin (Modelandview modelandview,model Model, httpservletrequest request) {Modelandview.setviewnam E ("Index"); returnModelandview;}

The default HTML in Spring MVC is a static resource, so we can set our static resource mapping by index.html to the page that the project will access by default

2.2:spring setting static resource mappings and directories (remember to put index.html in the HTML directory)

<mvc:resources mapping= "/img/**" location= "/img/"/>
<mvc:resources mapping= "/js/**" location= "/js/"/>
<mvc:resources mapping= "/css/**" location= "/css/"/>
<mvc:resources mapping= "/html/**" location= "/html/"/>
<mvc:resources mapping= "/tinymce/**" location= "/tinymce/"/>
<mvc:resources mapping= "/upload/**" location= "/upload/"/>
<mvc:resources mapping= "/assset/**" location= "/assset/"/>
<mvc:resources mapping= "/data/**" location= "/data/"/>
<mvc:resources mapping= "/images/**" location= "/images/"/>
<mvc:resources mapping= "/media/**" location= "/media/"/>

2.3: Create a controller that jumps to the Index object
/** *  */ PackageCom.king.weixin.controller;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.servlet.ModelAndView;/** * @author[email protected] *@versioncreated: June 13, 2018 PM 10:28:26 * @ClassName Indexcontroller * @Description Spring MVC jump to home page*/@Controller Public classIndexcontroller {@RequestMapping (value="/")      PublicModelandview Gotoindex (httpservletrequest request, httpservletresponse response) {return NewModelandview ("Index"); }}
2.3: Test localhost/Project name-Configure OK

SPRINGMVC Project configuration Welcome page for index.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.