1

注釈を使用してSprings 3.1.1フレームワークでマルチコントローラーを試しています。しかし、うまくいかないので、xmlファイルをさまざまな方法で構成して、注釈のサポートを有効にしようとしました。ここに私のコードがあります

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:mvc="http://www.springframework.org/schema/mvc"
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
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

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

<bean id="userKey" class="controllers.UserController" />
<bean id="newUserKey" class="controllers.UserFormController" />
<bean id="demoKey" class="controllers.demoController" />


<!--
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>
            <prop key="user.htm">userKey</prop>
            <prop key="add_user.htm">newUserKey</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>

コントローラ:

@Controller
public class multi extends MultiActionController {

@RequestMapping("/multi/add")
public ModelAndView add(){
    return new ModelAndView("demo", "message", "Add method called");
}

@RequestMapping("/user/divide.htm")
public ModelAndView divide(HttpServletRequest request, HttpServletResponse response) {
    return new ModelAndView("demo", "message", "divide method called");
}
}

web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.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>
</servlet-mapping>

<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>

私はこれを試しました:

解決策 1:

<mvc:annotation-driven />

解決策 2:

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
4

1 に答える 1

0

手始めに、2 つの戦略を混合していますが、Controllerどちらが@Controller優先されるべきですか? これは注釈で機能しないことを意図しているため、拡張しないことでコントローラーを修正してください。MultiActionController

@Controller
public class multi {

    @RequestMapping("/multi/add")
    public ModelAndView add(){
        return new ModelAndView("demo", "message", "Add method called");
    }

    @RequestMapping("/user/divide.htm")
    public ModelAndView divide(HttpServletRequest request, HttpServletResponse response) {
        return new ModelAndView("demo", "message", "divide method called");
    }
}

ソリューション 2 は、Spring 2.5 ベースの古い@RequestMapping方法を使用しRequestMappingHandlerMappingRequestMappingHandlerAdapterいます<mvc:annotation-driven />

基本的に必要なもの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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

   <!-- Scan only for @Controllers -->
    <context:component-scan base-package="com.web" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller />
    </context:component-scan>

    <mvc:annotation-driven />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

<!-- The index controller. -->
    <mvc:view-controller view-name="index" />

</beans>

@Controllerこれにより、アノテーション (<context:component-scan ... />および@RequestMappingアノテーション( )を処理するために必要なすべてが登録されます。少なくとも<mvc:annotation-driven />Spring 3.x.SimpleUrlHandlerMappingController@Controller

于 2014-03-13T07:22:24.770 に答える