web.xml
<?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> <!-- <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> --> <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>/DispatcherServlet</url-pattern> </servlet-mapping> </web-app>
offer-sevlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns:context = "http://www.springframework.org/schema/context" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.spring.security.web"></context:component-scan> <mvc:annotation-driven></mvc:annotation-driven> <bean name="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsps/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
这是怎么了 我无法访问home.jsp。我实际上是在观看3.0Spring的教程,并且已经完全按照视频显示了。有人可以在这里指出我的错误吗?
问题出在servlet映射的url模式中。
<url-pattern>/DispatcherServlet</url-pattern>
假设我们的控制器是
@Controller public class HomeController { @RequestMapping("/home") public String home(){ return "home"; } }
当我们在浏览器中点击某些URL时。调度程序servlet将尝试映射此URL。
当前我们Serlvet的网址模式是/Dispatcher指从{contextpath}/Dispatcher
/Dispatcher
{contextpath}/Dispatcher
但是当我们请求时,我们http://localhost:8080/home实际上是在询问/哪些资源不可用。所以要么我们需要说调度程序servlet就可以/通过
http://localhost:8080/home
/
<url-pattern>/</url-pattern>
我们通过/ dispatcher /Dispatcher/*
/Dispatcher/*
例如
<?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/Dispatcher/home