我的 web.xml 为
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
应用上下文为
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
>
<context:component-scan base-package="com.mindedges" />
</beans>
调度器 servlet 作为
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</beans>
com.mindedges.testapp 包中的 Controller
@Controller
@RequestMapping("/sample")
public class SampleController {
public String sample(Model model){
return "sample.jsp";
}
}
当我点击 http://localhost:8084/TestApp/sample
时。
WARNING: No mapping found for HTTP request with URI [/TestApp/sample] in DispatcherServlet with name
此外,当 spring 启动时,我没有看到 Testapp/sample 加载的处理程序
我想我的组件扫描不工作。为什么会这样?任何其他原因
编辑: 调度程序上下文是
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans "
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
">
<context:component-scan base-package="com.mindedges" />
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</beans>
最佳答案
返回不带.jsp
的 View 名称文件扩展名。 View 名称的解析由 InternalResourceViewResolver
处理.
public String sample(Model model){
return "sample";
}
您的配置片段:
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
采用 View 名称 sample
并应用适当的前缀 ( /WEB-INF/jsp/
) 和后缀 ( .jsp
) 来形成用于 View 解析的整个 View 名称。
还要确保 sample.jsp
位于/WEB-INF/jsp/sample.jsp
.
同样在使用 @RequestMapping
时annotation 您必须通过包含 <mvc:annotation-driven/>
在调度程序 serlvet 的配置中启用这些注释.
您还必须在调度程序配置中添加组件扫描,以便 Controller 注册到调度程序 servlet。当前,您在 applicationContext.xml
内执行组件扫描配置文件使用:
<context:component-scan base-package="com.mindedges" />
但是,由于此组件扫描发生在 applicationContext
内文件注册的 bean(假设它们是您的 Controller )将不可用于调度程序 serlvet。您必须将此配置片段放在 dispatcher-context.xml
中.
你们很多想咨询我的一个previous answers关于调度程序配置和上下文配置之间的差异。
在您的 dispatcher-context.xml 文件中也有一些空白和一个 mvc
namespace 丢失。我已更正这些问题并在此 GitHub Gist 中提供它们.
关于java - 警告 : No mapping found for HTTP request with URI [ ] in DispatcherServlet with name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21797916/