Spring控制器请求映射资源中的点的问题 [英] Spring controller request mapping issue for dot in resource

查看:91
本文介绍了Spring控制器请求映射资源中的点的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用spring 3.2.2 jar并且有一个带有映射/ validate的控制器。如果我在浏览器url中调用/validate.123,则执行该映射方法,这不应该是。我的代码或春天问题不是问题吗?

I am using spring 3.2.2 jars and have a controller with mapping "/validate". If i invoke "/validate.123" in browser url, this mapping method get executed which shouldn't be. Is't a problem in my code or spring issue?

@Controller
@SessionAttributes
public class ValidationController {

    @RequestMapping(value = "/validate", method = { RequestMethod.POST,
            RequestMethod.GET })
    public ModelAndView validatCheck(@ModelAttribute("po") PO po){
    }

web.xml

 <web-app xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd">
    <display-name>My 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>/</url-pattern>
    </servlet-mapping>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>
    <listener>
        <listener-class>
             org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/WEB-INF/pages/error.jsp</location>
    </error-page>
    <welcome-file-list>
        <welcome-file>home</welcome-file>
    </welcome-file-list>
</web-app>

mvc-dispatcher-servlet.xml

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

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


    <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <!--Don't add suffix or prefix like you do with .jsp files-->
        <property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/>
          <property name="order" value="0"/>        
    </bean>

    <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer" >
        <property name="definitions">
            <value>/WEB-INF/tiles.xml</value>
        </property>
    </bean>

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



    <mvc:annotation-driven />
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
    <mvc:resources mapping="/resources/**" location="/resources/" />


</beans>


推荐答案

正如您在部署描述符中所看到的(网络) .xml)你写过这样的东西

As you can see in your deployment-descriptor(web.xml) you have written something like this

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

显示您的调度员 - servlet将响应哪种URL模式

Which display to which URL pattern your dispatcher-servlet will respond

此处< url-pattern> /< / url-pattern> 传达dispatcher-servlet将响应任何网址格式,如果你想限制dispatcher-servlet响应特定的URL您可以定义Url模式,如下所示

here <url-pattern>/</url-pattern> convey that dispatcher-servlet will respond to any URL pattern ,If you want to restrict dispatcher-servlet to be respond to specific URL you can define Url pattern somthing like below

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

这篇关于Spring控制器请求映射资源中的点的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆