1

我有这个 servlet 映射

<servlet-mapping>
        <servlet-name>devicesWeb</servlet-name>
        <url-pattern>*.do</url-pattern>
        <url-pattern>/device-catalogue</url-pattern>
        <url-pattern>/device-catalogue/</url-pattern>                
        <url-pattern>/device-catalogue/*</url-pattern>
        <url-pattern>/search/search.do</url-pattern>
</servlet-mapping>

使用这两种方法:

   @RequestMapping(value = "/device-catalogue", method = RequestMethod.GET)
        private String initForm(@ModelAttribute("searchForm") final SearchForm searchForm, 
                                 BindingResult result, HttpServletRequest request, Model model, Locale locale) {

            sessionHelper.checkSessionAttributes(request,  locale);
            return SEARCH_VIEW;
        }


@RequestMapping(value = { "/search/showProductDetails.do",  "/device-catalogue/{id}" },  method = { RequestMethod.GET, RequestMethod.POST })
    private String showProductDetails(@ModelAttribute("searchForm") final SearchForm searchForm, 
                              HttpServletRequest request, Model model, Locale locale) {

        StringTokenizer st = new StringTokenizer(StringEscapeUtils.escapeHtml(searchForm.getItemId()),"=");

        if (st.countTokens()>1) {

            String awardId=st.nextToken();
            String id=st.nextToken();

            Item item = deviceService.getItemByAwardId  (Long.parseLong(id), awardId);

            normalizeWebsiteURL (item);

            orderCountriesAvailability(item.getCountriesAvailability(), locale);

            model.addAttribute("item", encodeItemForHTML(item));    
        }
        return PRODUCT_DETAIL_VIEW;
    }

此 URL 工作正常:

http://127.0.0.1:7001/eDevices/device-catalogue

但不是这个(我有一个 404)!

http://127.0.0.1:7001/eDevices/device-catalogue/16720

如果我将它添加到 web.xml 它可以工作

<url-pattern>/product-catalogue/16720</url-pattern>
4

1 回答 1

1

不要写<url-pattern>每个网址。使用spring mvc( )的前端控制器DispatcherServlet负责处理所有的应用请求。

为了做到这一点,web.xml你需要类似的东西:

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

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

除此之外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:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

   <context:component-scan base-package="com.test" /> <!-- specify your base package instead of "com.test" -->

   <mvc:annotation-driven />

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

   <mvc:resources mapping="/resources/**" location="WEB-INF/resources/" /> <!-- Not necessary but it's nice to address resources(css, images, etc) like this -->

</beans>
于 2016-04-19T16:32:06.097 回答