更新时间:2022-02-21 17:48:53
问题在于servlet-mapping的url模式。
the problem is in url pattern of servlet-mapping.
<url-pattern>/DispatcherServlet</url-pattern>
假设我们的控制器是
@Controller
public class HomeController {
@RequestMapping("/home")
public String home(){
return "home";
}
}
当我们在浏览器上点击某个网址时。调度程序servlet将尝试映射此URL。
when we hit some URL on our browser. the dispatcher servlet will try to map this url.
我们的serlvet的url模式目前是 / Dispatcher
,这意味着资源来自 {contextpath} / Dispatcher
the url pattern of our serlvet currently is /Dispatcher
which means resources are served from {contextpath}/Dispatcher
但是当我们请求 http:// localhost:8080 / home $ c时$ c>我们实际上要求
/
中的资源不可用。
所以要么我们需要说调度员servlet从 /
服务
but when we request http://localhost:8080/home
we are actually asking resources from /
which is not available.
so either we need to say dispatcher servlet to serve from /
by doing
<url-pattern>/</url-pattern>
我们通过 / Dispatcher / * 来自/ Dispatcher服务code>
例如
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID"
version="3.1">
<display-name>springsecuritydemo</display-name>
<servlet>
<description></description>
<display-name>offers</display-name>
<servlet-name>offers</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>offers</servlet-name>
<url-pattern>/Dispatcher/*</url-pattern>
</servlet-mapping>
</web-app>
并请求 http:// localhost:8080 / Dispatcher / home
或只需 /
就可以请求
http://localhost:8080/home