这几天我正在使用 spring mvc 进行一些测试,但有一个问题。对于要测试的基本应用程序,我选择本教程: http://loianegroner.com/2010/09/extjs-spring-mvc-3-and-hibernate-3-5-crud-datagrid-example/ 我已下载该示例和一切都按预期工作。问题是当我尝试添加另一个控制器时。看来我添加的文件没有被扫描。
我的 web.xml 文件:
<servlet>
<servlet-name>extjs-crud-grid-spring-hibernate</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/app-config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>extjs-crud-grid-spring-hibernate</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
/WEB-INF/spring/app-config.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan base-package="com.loiane" />
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />
<!-- misc -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- Configures Hibernate - …Run Code Online (Sandbox Code Playgroud) 我对适配器类没有什么疑问.我知道适配器类的目标是什么.何时应该使用.我怀疑是关于班级建设.我已经检查了一些教程,并且所有人都说我应该将"Adaptee"类作为依赖项传递给我的"Adapter".例如
Class SampleAdapter implements MyInterface
{
private AdapteeClass mInstance;
public SampleAdapter(AdapteeClass instance)
{
mInstance=instance;
}
}
Run Code Online (Sandbox Code Playgroud)
此示例是从维基百科复制的.正如您所见,AdapteeClass作为依赖项传递给我的对象.问题是为什么?如果我正在改变对象的界面很明显我将使用"新"界面,我不需要"旧"界面.为什么我需要在我的适配器外创建"旧"类的实例.有人可能会说我应该使用依赖注入,所以我可以传递任何我想要的,但这是适配器 - 我需要更改具体类的接口.就个人而言,我认为代码贝娄更好.
Class SampleAdapter implements MyInterface
{
private AdapteeClass mInstance;
public SampleAdapter()
{
mInstance= new AdapteeClass();
}
}
Run Code Online (Sandbox Code Playgroud)
你有什么意见?