<servlet> <servlet-name>springmvc-servlet-rest</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc-servlet-rest</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>多個servlet可以共存URL模板模式映射@RequestMapping(value="/ viewItems/{id}"):{×××}占位符,請求的URL可以是“/viewItems/1”或“/viewItems/2”,通過在方法中使用@PathVariable獲取{×××}中的×××變量。@PathVariable用于將請求URL中的模板變量映射到功能處理方法的參數(shù)上。@RequestMapping("/viewUsers/{id}") public @ResponseBody viewUsers(@PathVariable("id") String id,Model model) throws Exception{ //方法中使用@PathVariable獲取id的值,使用model傳回頁面 //調(diào)用 service查詢用戶信息 UserCustom userCustom = userService.findUserById(id); return userCustom;}注意:如果RequestMapping中表示為"/viewUsers/{id}",id和形參名稱一致,@PathVariable不用指定名稱。靜態(tài)資源訪問<mvc:resources>如果在DispatcherServlet中設(shè)置url-pattern為 /則必須對靜態(tài)資源進(jìn)行訪問處理。spring mvc 的<mvc:resources mapping="" location="">實(shí)現(xiàn)對靜態(tài)資源進(jìn)行映射訪問。如下是對js、CSS、img等文件訪問配置:<!-- 靜態(tài)資源解析 --><mvc:resources location="/js/" mapping="/js/**" /><mvc:resources location="/css/" mapping="/css/**" /><mvc:resources location="/img/" mapping="/img/**" />
新聞熱點(diǎn)
疑難解答