当前位置: 首页 > 面试题库 >

Spring Controller请求资源中点的映射问题

景阳平
2023-03-14
问题内容

我正在使用Spring 3.2.2 jar,并具有一个映射“ / validate”的控制器。如果我在浏览器URL中调用“
/validate.123”,则将执行此映射方法,而不应该这样做。我的代码或spring问题不是问题吗?

@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

<?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>

问题答案:

如您在Deployment-descriptor(web.xml)中所看到的,您已经编写了如下内容

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

您的调度程序servlet将响应哪个URL模式的显示

在这里<url-pattern>/</url-pattern>传达出dispatcher-
servlet将响应任何URL模式,如果您想限制dispatcher-servlet响应特定URL,则可以定义Url模式,如下所示

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


 类似资料:
  • 我在一个spring mvc应用程序中工作(我在spring mvc中是新的)。

  • 我在Spring MVC中测试了一些请求映射,在我的应用程序中遇到了一个奇怪的情况。我决定创建一个简单的cenario,以便您能够理解我的问题。我将首先向您展示我的项目(源代码)的详细信息,然后再开始我的问题。 我的项目中有以下目录结构: 我的Tomcat部署描述符: DispatcherServlet的我的应用程序上下文: 第1页的我的控制器类。jsp: 我的page1.jsp: 我可以访问第1

  • Web 容器需要本章描述的映射技术去映射客户端请求到 Servlet(该规范2.5以前的版本,使用这些映射技术是作为一个建议而不是要求,允许servlet 容器各有其不同的策略用于映射客户端请求到 servlet)。

  • 很抱歉再次问这种问题,但我无法通过查看其他线程和Spring doc来解决我的问题。 我正在使用maven的3.1.0.RELEASE,并尝试使用注释和java配置。 以下是我的web.xml: 这是我的档案web-application-config.xml. 我有两个类。第一个配置视图解析器 第二个定义我的控制器: 根据我的配置,我想一切都应该指向我的home()函数。然而,事实并非如此,以下

  • 我在将css、js、图像映射到我的JSP时遇到了一个问题:我的项目如下所示: src/main/webapp/WEB-INF/jsp/index.jsp

  • 在发布帖子时,我在网络控制台上看到了上述错误。 源地图错误:请求失败,状态404资源URL:超文本传输协议://{mysite}/js/app.js源地图URL:bootstrap.js.map 我的资源/js/app.js是这样的 还有这个网页。js看起来像这样 我的部分工作输出 我的错误是这样的

  • 主要内容:WebJars 映射,默认静态资源映射在 Web 应用中会涉及到大量的静态资源,例如 JS、CSS 和 HTML 等。我们知道,Spring MVC 导入静态资源文件时,需要配置静态资源的映射;但在 SpringBoot 中则不再需要进行此项配置,因为 SpringBoot 已经默认完成了这一工作。 Spring Boot 默认为我们提供了 3 种静态资源映射规则: WebJars 映射 默认资源映射 静态首页(欢迎页)映射 WebJ

  • 我有以下servlet映射- 我的图像链接在html是"/图像/文件夹/ImageName.jpg"-这些图像给我一个404,而如果更改链接到"/图像/ImageName.jpg"和移动图像直接下的图像文件夹,它得到我的图像。 我是否需要以任何方式修改我的servlet映射以考虑层次结构?