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

时间:2011-04-04 09:59:38

标签: google-app-engine spring-mvc controller javabeans applicationcontext

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

// 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控制器

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时这样:

<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已在/logingoogleControllerClassNamehandlerMapping注册。我在Google App Engine上使用Spring 3.0.2。

会感激一些帮助。

2 个答案:

答案 0 :(得分:1)

我能够解决这个问题,我从应用程序上下文文件中删除了ControllerClassNameHandlerMappingSimpleUrlHandlerMapping bean。

关心你们

答案 1 :(得分:0)

该错误告诉您在applicationContext中不需要<bean id="GoogleLoginService" class="org.opevel.web.LoginGoogleController" />。您也不需要<bean id="urlMapping">

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