如何将弹簧控制器 URL 配置为没有扩展名



我的控制器目前被映射为类似 http://example.com/fix.go 的东西,当然,我认为这是愚蠢的,想要更好的东西,比如 http://example.com/fix 或 http://example.com/mmm/fix 没有扩展。 但是,当我尝试配置它时,我无法让它工作。 我显然错过了对整个映射的理解的关键部分。 我正在使用Spring 3.x,tomcat和控制器的注释。

我的网站.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app
    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">
    <servlet>
        <servlet-name>BooBoo</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>BooBoo</servlet-name>
        <url-pattern>*.*</url-pattern>   <!-- was *.go when it worked -->
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

BooBoo-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"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        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">
    <context:component-scan base-package="com.foofoo.booboo"/>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

我的一个控制器配置如下:

@Controller
public class BangBangController extends BaseController {
            // Used to be fix.go when it worked
    @RequestMapping( value="fix", method=RequestMethod.GET )
    public ModelAndView choose(
            HttpSession session,
            @RequestParam( value="fixId", required=false, defaultValue="-1" ) Integer fixId,
            @RequestParam( value="forkId", required=false, defaultValue="-1" ) Integer forkId 
    )
    throws Exception { ... }
}

我尝试将 web.xml 中的映射更改为/mmm/*,期望像 http://example.com/mmm/fix 这样的 URL 可以工作,但这也没有奏效。 当我在浏览器中输入我认为正确的 URL 时,我收到"缺少资源"错误。

我在这里搞什么? 我缺乏什么关键的理解? 我尝试过在工作中的另一个项目上使无扩展的东西工作,但也无法让它去那里。 我显然错过了一些东西。

问题是您的调度程序 servlet 匹配了其中带有"."的任何内容。

将其更改为

 <url-pattern>/*</url-pattern> 

但是,这也很糟糕,因为您将无法提供静态内容。

你真正应该做的是:

 <url-pattern>/webapp/*</url-pattern> 

其中"webapp"是一些前缀。 您的所有网址都需要以它为前缀,但这允许您仍然提供静态内容。

答案是我没有指定我正在使用Spring MVC的东西,从而弄乱了spring-servlet.xml文件。 我感谢克里斯在诊断问题方面的帮助。 我需要确认我尝试过的一些事情(但由于这个错误而失败)是正确的。

我添加了

xmlns:mvc="http://www.springframework.org/schema/mvc"

<mvc:annotation-driven />

到那个文件,繁荣,它开始工作。 让我挠头的是,"没有它它是如何工作的? 因为它适用于 *.go 模式。

以下是完整的结果文件:

<?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/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        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">
    <context:component-scan base-package="com.barbar.foofoo"/>
    <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven />
    <!--
    Declare View Resolver: when view 'view_name' is called (from the Controller), 
    the file '/jsp/view_name.jsp' will be used.
    -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

相关内容

最新更新