Spring MVC Url-Pattern DispatchServlet

时间:2013-08-21 09:25:24

标签: java spring servlets spring-mvc url-pattern

我认为我的设置崩溃了,因为我的映射不再起作用了,我不知道为什么。这是我的web.xml,applicationContext.xml payment-servlet.xml和payment.beans.xml。

**web.xml**

     <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

    <!-- Add Support for Spring -->
    <!-- Default applicationContext location: /WEB-INF/applicationContext.xml -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- exposes the request to the current thread -->
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

     <!-- springapp payment servlet -->
            <servlet>
                <servlet-name>payment</servlet-name>
                <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                    <init-param>
                        <param-name>contextConfigLocation</param-name>
                        <!--  <param-value>classpath:/spring/servlet/payment-servlet.xml</param-value> -->
                        <param-value>file:**/webapp/META-INF/spring/servlet/payment-servlet.xml</param-value>
                    </init-param>
                <load-on-startup>1</load-on-startup>
            </servlet>
            <servlet-mapping>
                 <servlet-name>payment</servlet-name>
                  <url-pattern>/payment/*</url-pattern> 
                  <url-pattern>/paymentExternalData</url-pattern>
                  <url-pattern>/paymentInternalData</url-pattern> 
            </servlet-mapping>

            <!-- Welcome files -->
            <welcome-file-list>
                 <welcome-file>payment.jsp</welcome-file>
                 <welcome-file>payment.html</welcome-file>
            </welcome-file-list>
        </web-app>

**applicationContext.xml**

    <context:annotation-config />

         <!--  payment servlet 
        <import resource="classpath:/spring/payment.beans.xml"/> -->
        <import resource="file:**/webapp/META-INF/spring/payment.beans.xml"/>

        <!-- Auto scan the components -->
        <context:component-scan 
            base-package="com.app.payment.model.PaymentUser" />

**payment-servlet**

    <!-- Auto scan the components -->
    <context:component-scan base-package="at.dt_i.primesign.payment" />

    <!-- Payment controller --> 
    <bean class="at.dt_i.primesign.payment.controller.PaymentController">
    </bean> 

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

    <!-- PropertyPlaceholderConfigurer 
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" depends-on="configuration">
        <property name="properties" ref="configuration" />
    </bean> -->
    <bean 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>/WEB-INF/configuration.properties</value>
        </property>
    </bean>

**payment.beans.xml**

    <context:annotation-config />
    <tx:annotation-driven /> 

    <bean id="paymentDao" class="com.app.payment.model.PaymentDAOImpl" />
    <bean id="paymentService" class="com.app.payment.PaymentServiceImpl" />

    <bean id="dataSource" 
             class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${database.driverClassName}" />
        <property name="url" value="${database.url}" />
        <property name="username" value="${database.username}" />
        <property name="password" value="${database.password}" />
    </bean>


    <bean id="paymentTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="paymentEntityManagerFactory" />
    </bean>

    <!-- -->
    <bean id="paymentJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="showSql" value="true" />
        <property name="generateDdl" value="${paymentJpaVendorAdapter.generateDdl}" />
        <property name="databasePlatform" value="${paymentJpaVendorAdapter.databasePlatform}" />
    </bean> 

    <bean id="paymentEntityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="profileDataSource" />
        <property name="jpaVendorAdapter" ref="paymentJpaVendorAdapter" />
        <property name="persistenceUnitName" value="payment" />
    </bean>

我的第一个问题:我的结构是正确的还是有更好的解决方案。 主要目标是使用控制器方法/ paymentInternalData和/ paymentExternalData。但我认为dispatchServlet加载了不同的东西,因为映射不起作用它只显示欢迎页面。但不是2个子页面。

我知道这主要是代码,但我不确定要发布什么,所以我发布了所有内容。希望任何人都可以提供帮助。

1 个答案:

答案 0 :(得分:1)

我认为您的servlet的Url模式是正确的:

             <servlet-mapping>
             <servlet-name>payment</servlet-name>
              <url-pattern>/payment/*</url-pattern> 
              <url-pattern>/paymentExternalData</url-pattern>
              <url-pattern>/paymentInternalData</url-pattern> 
            </servlet-mapping>

但是

的文件:** / web应用/ META-INF /弹簧/ servlet的/支付-servlet.xml中

无法加载payment-servlet.xml文件。 如果你的META-INF在webapp目录下,那么你可以这样做:

                <init-param>
                    <param-name>contextConfigLocation</param-name>
                    <!--  <param-value>classpath:/spring/servlet/payment-servlet.xml</param-value> -->
                    <param-value>/META-INF/spring/servlet/payment-servlet.xml</param-value>
                </init-param>

或 删除init-param块并在web.xml / WEB-INF /目录下移动payment-servlet.xml,其中存在web.xml。