springservlet映射/url模式



我有这个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-目录

但不是这个(我得了404分)!

http://127.0.0.1:7001/eDevices/device-目录/16720

如果我把它添加到web.xml中,它就可以工作

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

不要为每个url编写<url-pattern>。使用springmvc(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>

相关内容

  • 没有找到相关文章

最新更新