Bean 使用 @Controller 初始化,但 @RequestMapping 不会被调用

发布于 2024-10-30 03:30:13 字数 5145 浏览 1 评论 0原文

下面是我对新的基于 Spring 3 注解的控制器的设置:

//dispatcher-servlet.xml //web.xml

<?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"
       xmlns:security="http://www.springframework.org/schema/security"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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/security   http://www.springframework.org/schema/security/spring-security-3.0.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

<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" />

 <!-- Enables plain controllers -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="order" value="1" />
        <property name="ignoreAcceptHeader" value="true" />
        <property name="mediaTypes">
            <map>
                <entry key="xml" value="application/xml" />
                <entry key="json" value="application/json" />
            </map>
        </property>
    </bean>

<!-- Entity Property binding for webBindingInitializer -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="webBindingInitializer">
                <bean class="org.opevel.web.BindingInitializer" />
        </property>
    </bean>

<context:component-scan base-package="org.opevel.web"/>

</beans>

//

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <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>*.htm</url-pattern>
        <url-pattern>/auth</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
</session-config>

</web-app>

Spring 控制器

package org.opevel.web;

@Controller
public class LoginGoogleController {

private static final Logger log = Logger.getLogger(LoginGoogleController.class.getName());

public LoginGoogleController() {
    log.info("constructing LoginGoogleController");
}

@RequestMapping(value="/auth", method=RequestMethod.GET)
public String doGet(HttpServletRequest request, HttpServletResponse response) throws Exception {

         return "redirect:index";

      }

    }

当我导航到 /auth 时,我得到一个 404。当我尝试在像这样的 applicationContext

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
            <prop key="/auth">GoogleLoginService</prop>
            </props>
        </property>

<bean id="GoogleLoginService" class="org.opevel.web.LoginGoogleController" />

我收到一个 BeanException ,表明该 bean 已通过 ControllerClassNamehandlerMapping/logingoogle 注册。我在 Google App Engine 上使用 Spring 3.0.2。

将不胜感激一些帮助。

Below is my setup for the new Spring 3 annotation based controller:

// dispatcher-servlet.xml

<?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"
       xmlns:security="http://www.springframework.org/schema/security"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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/security   http://www.springframework.org/schema/security/spring-security-3.0.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

<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" />

 <!-- Enables plain controllers -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="order" value="1" />
        <property name="ignoreAcceptHeader" value="true" />
        <property name="mediaTypes">
            <map>
                <entry key="xml" value="application/xml" />
                <entry key="json" value="application/json" />
            </map>
        </property>
    </bean>

<!-- Entity Property binding for webBindingInitializer -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="webBindingInitializer">
                <bean class="org.opevel.web.BindingInitializer" />
        </property>
    </bean>

<context:component-scan base-package="org.opevel.web"/>

</beans>

// web.xml

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <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>*.htm</url-pattern>
        <url-pattern>/auth</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
</session-config>

</web-app>

// Spring controller

package org.opevel.web;

@Controller
public class LoginGoogleController {

private static final Logger log = Logger.getLogger(LoginGoogleController.class.getName());

public LoginGoogleController() {
    log.info("constructing LoginGoogleController");
}

@RequestMapping(value="/auth", method=RequestMethod.GET)
public String doGet(HttpServletRequest request, HttpServletResponse response) throws Exception {

         return "redirect:index";

      }

    }

When I navigate to /auth, I get a 404. When I try to register the bean in the applicationContext like this:

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
            <prop key="/auth">GoogleLoginService</prop>
            </props>
        </property>

<bean id="GoogleLoginService" class="org.opevel.web.LoginGoogleController" />

I get a BeanException stating that the bean is already registered at /logingoogle through ControllerClassNamehandlerMapping. I am using Spring 3.0.2 on Google App Engine.

Will appreciate some help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇俏 2024-11-06 03:30:13

我可以通过从应用程序上下文文件中删除 ControllerClassNameHandlerMappingSimpleUrlHandlerMapping bean 来解决此问题。

问候你们

I was able to fix this my removing both the ControllerClassNameHandlerMapping and SimpleUrlHandlerMapping beans from the application context file.

Regards y'all

栖竹 2024-11-06 03:30:13

该错误告诉您,您的 applicationContext 中不需要 。另外,您也不需要

@Controller 为您创建一个 bean,@RequestMapping 创建 URL 映射。

That error is telling you that you don't need <bean id="GoogleLoginService" class="org.opevel.web.LoginGoogleController" /> in your applicationContext. Also you don't need <bean id="urlMapping">

@Controller creates a bean for you and @RequestMapping creates the URL mapping.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文