<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中的模板變量映射到功能處理方法的參數上。@RequestMapping("/viewUsers/{id}") public @ResponseBody viewUsers(@PathVariable("id") String id,Model model) throws Exception{ //方法中使用@PathVariable獲取id的值,使用model傳回頁面 //調用 service查詢用戶信息 UserCustom userCustom = userService.findUserById(id); return userCustom;}注意:如果RequestMapping中表示為"/viewUsers/{id}",id和形參名稱一致,@PathVariable不用指定名稱。靜態資源訪問<mvc:resources>如果在DispatcherServlet中設置url-pattern為 /則必須對靜態資源進行訪問處理。spring mvc 的<mvc:resources mapping="" location="">實現對靜態資源進行映射訪問。如下是對js、CSS、img等文件訪問配置:<!-- 靜態資源解析 --><mvc:resources location="/js/" mapping="/js/**" /><mvc:resources location="/css/" mapping="/css/**" /><mvc:resources location="/img/" mapping="/img/**" />
|
新聞熱點
疑難解答