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