spring servlet 映射/url 模式
spring servlet-mapping / url-pattern
我有这个 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>
不要为每个 url 写一个 <url-pattern>
。使用springmvc(DispatcherServlet
)的front controller负责处理所有的应用请求
为此,在您的 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>
我有这个 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>
不要为每个 url 写一个 <url-pattern>
。使用springmvc(DispatcherServlet
)的front controller负责处理所有的应用请求
为此,在您的 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>