0. Question: How to modify the welcome page of the project in mvc, or it is called the root path
One thing is almost finished, and there is only one problem. It should be a small problem. Is the welcome page of the mvc project. How can I change it.
This project is created through mvn. The prototype of the entire project is spring_mvc_jpa_deom_archetype. The framework is built early to see the results. If the project name is mvn_mvc, the effect is as follows:
Access root path
Http: // localhost: 8080/mvn_mvc/
Automatically jump to the following path
Http: /localhost: 8080/mvn_mvc/spring/
The subsequent accesses are all prefixed with "spring ".
Http: /localhost: 8080/mvn_mvc/spring/person/list
Obviously, I don't want to carry the "spring" prefix, rename the name or kill it, and directly go /.
The problem is how to modify the default path!
1. Default Process
First, let's look at the default root path process:
No welcome page information is configured in web. xml! In fact, this is equivalent to the following Configuration:
//-If the welcome page is not specified, the default value is the following configuration. This should be set on different Web servers, but most of them are like this-
<Welcome-file-list>
<Welcome-file> index.html </welcome-file>
<Welcome-file> index.htm </welcome-file>
<Welcome-file> index. jsp </welcome-file>
</Welcome-file-list>
In the project directory, there is an index.html file that jumps:
<Html>
<Head>
<Metahttp-equiv = "Refresh" content = "0; URL = spring/">
</Head>
</Html>
The full url to jump to is equivalent to http: // localhost: 8080/mvn_mvc/spring /.. Why is it because the following url configuration is performed in web. xml:
<Servlet>
<Servlet-name> appServlet </servlet-name>
<Servlet-class> org. springframework. web. servlet. DispatcherServlet </servlet-class>
<Init-param>
<Param-name> contextConfigLocation </param-name>
<Param-value>/WEB-INF/spring/app/servlet-context.xml </param-value>
</Init-param>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
<Servlet-mapping>
<Servlet-name> appServlet </servlet-name>
<Url-pattern>/spring/* </url-pattern>
</Servlet-mapping>
In this case, http: // localhost: 8080/mvn_mvc/spring/Corresponds to/In the MVC path route according to this rule /. That is, HomeController.
@ Controller
PublicclassHomeController
{
@ RequestMapping (value = "/", method = RequestMethod. GET)
PublicString home (Model model)
{
Return "home ";
}
}
Find the corresponding home. jsp.
The entire process is like this. Why is this spring path set by default. The main purpose is to improve the url Processing efficiency of spring. All branches under spring/are handled by spring, and other branches can be handled by web servers.
If everything is handled by spring, we need to intercept. Well, it will certainly be used by a lot of static resources or other dynamic JSPs. It will be intercepted by spring first and then processed as another one. Yes, yes, but the efficiency will feel a step more.
However, when planning a url, we may shorten it as much as possible to facilitate user input. At the same time, we will not consider the efficiency of spring when planning a url, that is, url design first. At this time, there is usually no specific path of spring, that is, spring will plan the url. That is, to intercept.
2. How to directly intercept the root path
Let's talk about the process directly:
The following must be added to web. xml:
<Welcome-file-list>
<Welcome-file> </welcome-file>
</Welcome-file-list>
At this point, the web server knows that the root path should not be handled by the web server, but by the program itself. In this case, index.html does not work.
Then, the root path is intercepted by HomeCtroller because the ing to "/" is configured.
Alternatively, you can simplify the process by adding the following content to the servlet configuration file:
<Mvc: view-controllerpath = "/" view-name = "index"/>
If both are available. There is only one function. Generally, scanning takes effect first. Who will first scan, is to see who is in front of the comparison with <mvc: annotation-driven/>.
3. Why did you go to his uncle?
I finally realized that Zhuge Liang was really easy to do afterwards. I forgot the pig in advance!
This process is very simple, but it was a waste of a weekend to solve it before we figured it out. Remember to search for it on the Internet, or conduct research on the source code breakpoint, or refer to the example for configuration reference. I have no clue until this morning. No rule is found, and the interception of the root path is not implemented. Then I went out for a meal on time at noon. After dinner, I came back and played with my computer. I found that the project was running and everything was done as needed. It was not normal.
Is there a cache when mvn tomcat: run is running !!! Therefore, each time you modify the configuration file, it does not take effect immediately. I remember using mvn tomcat: run, mvn jetty: run, or mavent debugging in eclipse. It was not normal! This time, it is directly released to Tomcat6. You can directly change the configuration file, restart, and view it each time. It's all done.
In this conclusion, the first sentence is that the master of maven is suffering.
However, some other things are worth using for reference:
- Maven jetty: During run, modifying web. xml does not know whether the cache will be affected, and cannot take effect immediately. Refresh the browser multiple times.
- You can check the project startup log, and each url mapped to which controller has output.
- <Mvc: annotation-driven/> it is recommended that you do not call it multiple times, call it once, scan all-inclusive once, and you will find a scan ing record appears in the log
- What is the difference between path interception and. Reference: callback.
Forgive him!