0

我一直在尝试开发一个使用 Spring 3(.1.1) MVC 的小型测试应用程序,它支持多个 servlet 来处理不同的 URL 模式。

我创建了另一个 bean 以自动装配到第一个 servlet 中,因此我最初将 DAO 的 放入 app-servlet.xml 文件中,因为我希望它仅限于该 servlet。Bu† 当我这样做时,我收到一个关于无法自动装配 bean 的错误。如果我将 dhe 语句移动到 root-context.xml 文件中,那么它就起作用了。

因为我在 web.xml 中有 2 个 DispatcherServlet,所以我将每个的 url-pattern 设置为“/”。虽然我仍然在 app-servlet.xml 文件中有这些语句,但我将第二个 servlet 的 url-pattern 更改为“/add”(也尝试了“/fred”),突然一切都按预期工作了(第二个 servlet 使用 /addperson 调用- 在 servlet 的 RequestMapping 中指定)。

当我将 DAO 的代码放入 root-context.xml 时,我能够将 url-pattern 放回“/”。


所以在测试这个设置时,我有以下 web.xml:

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->

<servlet>
    <servlet-name>app</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/app-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet>
    <servlet-name>person</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/person-servlet.xml</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>app</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>person</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

app-servlet.xml:

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<context:annotation-config />
<context:component-scan base-package="com.hh.builditup.controller" />

和人-servlet.xml:

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<context:component-scan base-package="com.hh.builditup.controller" /> 

根上下文.xml:

<!-- Root Context: defines shared resources visible to all other web components -->
<context:annotation-config />
<context:component-scan base-package="com.hh.builditup.dao" />

'app' servlet 看起来像:@Controller public class HomeController {

private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

@Autowired
private MyDAO myDAO;
/**
 * Simply selects the home view to render by returning its name.
 */
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {

最后是'person' servlet:

@Controller
public class PersonController {

private static final Logger logger = LoggerFactory.getLogger(PersonController.class);

/**
 * Simply selects the home view to render by returning its name.
 */
@RequestMapping(value = "/addperson", method = RequestMethod.GET)

DAO bean 看起来像:

@Component("myDAO")
public class MyDAO {

public MyDAO()
{
    System.out.println("created MyDAO object");
}

}

有人可以解释会产生这些结果的 url-pattern 和 RequestMapping 的交互吗?当一个 bean 被限制在一个 servlet 而不是很常见时,为什么必须修改 url 模式?另外为什么更改为 url-pattern 对用于测试 servlet 的 URL 没有影响?

提前感谢您的任何指导。

4

0 回答 0