0

由于 spring 可以让您使用自己的 xml 配置 [servlet-name]-servlet.xml 拥有多个调度 servlet。我想知道如何使用 java 注释配置类而不是 xml 文件来做同样的事情?例如:

<servlet>
    <servlet-name>ap</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>


<servlet>
    <servlet-name>em</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>

使用 web.xml:

<servlet-mapping>
    <servlet-name>ap</servlet-name>
    <url-pattern>/ap/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>em</servlet-name>
    <url-pattern>/em/*</url-pattern>
</servlet-mapping>
4

1 回答 1

-2

请参阅下面的配置以使用注释。另外,下面只是我项目的一个工作代码,所以请根据您的需要进行修改。

web.xml

<web-app id="WebApp_ID" version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <display-name>Spring Web MVC Application</display-name>

  <servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>
                  org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
  </servlet-mapping>

</web-app>

mvc-调度程序-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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">


    <context:annotation-config />
    <context:component-scan base-package="com.on.transport" />
    <mvc:annotation-driven />

    <bean
       class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

</beans>

将您的所有课程保留在此包中,因为它将在此包中扫描。(com.on.transport),根据您的需要更改到所有需要的地方。

@Controller
public class AdminEmployeeManipulation {

    @RequestMapping(value="/selectEmployee", method = RequestMethod.GET)
    public ModelAndView employeeManipulation(Principal principal) {
    }

    // Add as many function you need based on @RequestMapping.
}

详细解释参考:http ://www.mkyong.com/spring3/spring-3-mvc-hello-world-example/

于 2013-08-14T14:53:23.363 回答