use*_*796 4 java spring spring-mvc
我有我的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>
Run Code Online (Sandbox Code Playgroud)
applicationContext as
<?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>
Run Code Online (Sandbox Code Playgroud)
Dispatcher servlet as
<?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>
Run Code Online (Sandbox Code Playgroud)
控制器在com.mindedges.testapp包中
@Controller
@RequestMapping("/sample")
public class SampleController {
public String sample(Model model){
return "sample.jsp";
}
}
Run Code Online (Sandbox Code Playgroud)
我打的时候http://localhost:8084/TestApp/sample.
警告:在带有名称的DispatcherServlet中,没有找到带有URI [/ TestApp/sample]的HTTP请求的映射
此外,当春天开始时,我没有看到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>
Run Code Online (Sandbox Code Playgroud)
返回没有.jsp文件扩展名的视图名称.视图名称的解析由InternalResourceViewResolver.处理.
public String sample(Model model){
return "sample";
}
Run Code Online (Sandbox Code Playgroud)
您的配置片段:
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
Run Code Online (Sandbox Code Playgroud)
获取视图名称sample并应用适当的前缀(/WEB-INF/jsp/)和后缀(.jsp)以形成用于解析视图的整个视图名称.
还要确保sample.jsp它位于/WEB-INF/jsp/sample.jsp.
此外,在使用@RequestMapping注释时,您必须通过包含在调度程序serlvet的配置中启用这些注释<mvc:annotation-driven/>.
您还必须在调度程序配置中添加组件扫描,以便向调度程序servlet注册控制器.目前,您applicationContext.xml使用以下命令在配置文件中执行组件扫描:
<context:component-scan base-package="com.mindedges" />
Run Code Online (Sandbox Code Playgroud)
但是,由于此组件扫描发生在applicationContext文件中,因此注册的bean(假设它们是您的控制器)将不可用于调度程序serlvet.您必须将此配置代码段放在其中dispatcher-context.xml.
您可以参考我之前关于调度程序配置和上下文配置之间差异的一个答案.
您的dispatcher-context.xml文件中还有一些空格,并且mvc缺少命名空间.我已经纠正了这些问题,并在GitHub Gist中提供了这些问题.
| 归档时间: |
|
| 查看次数: |
38725 次 |
| 最近记录: |