ããä¸ãæ们é½ç¥éå¨åºäºSpringçApplicationä¸ï¼éè¦å¨web.xmlä¸å¢å ä¸é¢ç±»ä¼¼çé
置信æ¯ï¼
ãã<listener>
ãã<listener-class>
ããorg.springframework.web.context.ContextLoaderListener
ãã</listener-class>
ãã</listener>
ãã<!-- Spring MVC Servlet -->
ãã<servlet>
ãã<servlet-name>servletName</servlet-name>
ãã<servlet-class>
ããorg.springframework.web.servlet.DispatcherServlet
ãã</servlet-class>
ãã<load-on-startup>1</load-on-startup>
ãã</servlet>
ãã<servlet-mapping>
ãã<servlet-name>servletName</servlet-name>
ãã<url-pattern>/</url-pattern>
ãã</servlet-mapping>
ããæ¤å¤éè¦ç¹å«å¼ºè°çæ¯ <url-pattern>/</url-pattern>使ç¨çæ¯/ï¼èä¸æ¯/*,å¦æ使ç¨/*,é£ä¹è¯·æ±æ¶å¯ä»¥éè¿DispatcherServlet转åå°ç¸åºçActionæè
Controllerä¸çï¼ä½æ¯è¿åçå
容ï¼å¦è¿åçjspè¿ä¼å次被æ¦æªï¼è¿æ ·å¯¼è´404é误ï¼å³è®¿é®ä¸å°jspãæ以å¦æ以ååç°æ»æ¯æ404é误çæ¶åï¼å«å¿äºcheckä¸ä¸ <url-pattern>/</url-pattern>çé
ç½®æ¯å¦æ¯/*.
ããäºãå
¶å®Spring çServletæ¦æªå¨å¹é
è§åï¼å³ <url-pattern>...</url-pattern> ï¼é½å¯ä»¥èªå·±å®ä¹ï¼ä¾ï¼å½æ å°ä¸º@RequestMapping("/user/add")æ¶
ãã1ãæ¦æª*.doã*.htmï¼ ä¾å¦ï¼/user/add.do
ããè¿æ¯æä¼ ç»çæ¹å¼ï¼æç®åä¹æå®ç¨ãä¸ä¼å¯¼è´éææ件ï¼jpg,js,cssï¼è¢«æ¦æªã
ãã2ãæ¦æª/ï¼ä¾å¦ï¼/user/add
ããå¯ä»¥å®ç°ç°å¨å¾æµè¡çRESTé£æ ¼ãå¾å¤äºèç½ç±»åçåºç¨å¾å欢è¿ç§é£æ ¼çURLã
ããå¼ç«¯ï¼ä¼å¯¼è´éææ件ï¼jpg,js,cssï¼è¢«æ¦æªåä¸è½æ£å¸¸æ¾ç¤ºãæ³å®ç°RESTé£æ ¼ï¼äºæ
å°±æ¯éº»ç¦ä¸äºãåé¢æ解å³åæ³è¿ç®ç®åã
ãã3ãæ¦æª/*ï¼è¿æ¯ä¸ä¸ªé误çæ¹å¼ï¼è¯·æ±å¯ä»¥èµ°å°Actionä¸ï¼ä½è½¬å°jspæ¶å次被æ¦æªï¼ä¸è½è®¿é®å°jspã
ããä¸ãå¦ä½è®¿é®å°éæçæ件ï¼å¦jpg,js,css?
ããå¦æä½ çDispatcherServletæ¦æª"*.do"è¿æ ·çæåç¼çURLï¼å°±ä¸åå¨è®¿é®ä¸å°éæèµæºçé®é¢ã
ããå¦æä½ çDispatcherServletæ¦æª"/"ï¼ä¸ºäºå®ç°RESTé£æ ¼ï¼æ¦æªäºææç请æ±ï¼é£ä¹åæ¶å¯¹*.js,*.jpgçéææ件ç访é®ä¹å°±è¢«æ¦æªäºã
ããæ们è¦è§£å³è¿ä¸ªé®é¢ã
ããç®çï¼å¯ä»¥æ£å¸¸è®¿é®éææ件ï¼ä¸å¯ä»¥æ¾ä¸å°éææ件æ¥404ã
ããæ¹æ¡ä¸ï¼æ¿æ´»TomcatçdefaultServletæ¥å¤çéææ件
ãã<servlet-mapping>
ãã<servlet-name>default</servlet-name>
ãã<url-pattern>*.jpg</url-pattern>
ãã</servlet-mapping>
ãã<servlet-mapping>
ãã<servlet-name>default</servlet-name>
ãã<url-pattern>*.js</url-pattern>
ãã</servlet-mapping>
ãã<servlet-mapping>
ãã<servlet-name>default</servlet-name>
ãã<url-pattern>*.css</url-pattern>
ãã</servlet-mapping>
ããç¹ç¹ï¼1. è¦é
ç½®å¤ä¸ªï¼æ¯ç§æ件é
ç½®ä¸ä¸ªã
ãã2. è¦åå¨DispatcherServletçåé¢ï¼ 让 defaultServletå
æ¦æªè¯·æ±ï¼è¿æ ·è¯·æ±å°±ä¸ä¼è¿å
¥Springäºã
ãã3. é«æ§è½ã
ããå¤æ³¨ï¼
ããTomcat, Jetty, JBoss, and GlassFish èªå¸¦çé»è®¤Servletçåå -- "default"
ããGoogle App Engine èªå¸¦ç é»è®¤Servletçåå -- "_ah_default"
ããResin èªå¸¦ç é»è®¤Servletçåå -- "resin-file"
ããWebLogic èªå¸¦ç é»è®¤Servletçåå -- "FileServlet"
ããWebSphere èªå¸¦ç é»è®¤Servletçåå -- "SimpleFileServlet"
ããæ¹æ¡äºï¼ å¨spring3.0.4以åçæ¬æä¾äºmvc:resources ï¼ ä½¿ç¨æ¹æ³ï¼
ãã<!-- 对éæèµæºæ件çè®¿é® -->
ãã<mvc:resources mapping="/images/**" location="/images/" />
ããimages/**æ å°å° ResourceHttpRequestHandlerè¿è¡å¤çï¼locationæå®éæèµæºçä½ç½®.å¯ä»¥æ¯web applicationæ ¹ç®å½ä¸ãjarå
éé¢ï¼è¿æ ·å¯ä»¥æéæèµæºå缩å°jarå
ä¸ãcache-period å¯ä»¥ä½¿å¾éæèµæºè¿è¡web cache
ãã
ããå¦æåºç°ä¸é¢çé误ï¼å¯è½æ¯æ²¡æé
ç½®<mvc:annotation-driven />çåå ã
ããæ¥éWARNING: No mapping found for HTTP request with URI [/mvc/user/findUser/lisi/770] in DispatcherServlet with name 'springMVC'
ãã
ãã使ç¨<mvc:resources/>å
ç´ ,æmappingçURI注åå°SimpleUrlHandlerMappingçurlMapä¸,
ããkey为mappingçURI patternå¼,èvalue为ResourceHttpRequestHandler,
ããè¿æ ·å°±å·§å¦çæ对éæèµæºç访é®ç±HandlerMapping转å°ResourceHttpRequestHandlerå¤ç并è¿å,æ以就æ¯æclasspathç®å½,jarå
å
éæèµæºç访é®.
ããå¦å¤éè¦æ³¨æçä¸ç¹æ¯,ä¸è¦å¯¹SimpleUrlHandlerMapping设置defaultHandler.å 为对static uriçdefaultHandlerå°±æ¯ResourceHttpRequestHandler,
ããå¦åæ æ³å¤çstatic resources request.
ããæ¹æ¡ä¸ ï¼ä½¿ç¨<mvc:default-servlet-handler/>
ãã<mvc:default-servlet-handler/>
ããä¼æ"/**" url,注åå°SimpleUrlHandlerMappingçurlMapä¸,æ对éæèµæºç访é®ç±HandlerMappingè½¬å° org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler å¤ç并è¿å.
ããDefaultServletHttpRequestHandler使ç¨å°±æ¯å个Servlet容å¨èªå·±çé»è®¤Servlet.
ããè¡¥å
说æï¼å¤ä¸ªHandlerMappingçæ§è¡é¡ºåºé®é¢ï¼
ããDefaultAnnotationHandlerMappingçorderå±æ§å¼æ¯ï¼0
ãã< mvc:resources/ > èªå¨æ³¨åç SimpleUrlHandlerMapping çorderå±æ§å¼æ¯ï¼ 2147483646
ãã<mvc:default-servlet-handler/>èªå¨æ³¨å çSimpleUrlHandlerMapping çorderå±æ§å¼æ¯ï¼ 2147483647
ããspringä¼å
æ§è¡orderå¼æ¯è¾å°çãå½è®¿é®ä¸ä¸ªa.jpgå¾çæ件æ¶ï¼å
éè¿ DefaultAnnotationHandlerMapping æ¥æ¾å¤çå¨ï¼ä¸å®æ¯æ¾ä¸å°çï¼å 为æ们没æå«a.jpgçActionãç¶ååæorderå¼ååºæ¾ï¼ç±äºæåä¸ä¸ª SimpleUrlHandlerMapping æ¯å¹é
"/**"çï¼æ以ä¸å®ä¼å¹é
ä¸ï¼å°±å¯ä»¥ååºå¾çã 访é®ä¸ä¸ªå¾çï¼è¿è¦èµ°å±å±å¹é
ãä¸ç¥æ§è½å¦ä½ï¼
ããæåå说æä¸ä¸ï¼æ¹æ¡äºãæ¹æ¡ä¸ å¨è®¿é®éæèµæºæ¶ï¼å¦ææå¹é
ç(è¿ä¼¼)æ»æ¦æªå¨ï¼å°±ä¼èµ°æ¦æªå¨ãå¦æä½ å¨æ¦æªä¸å®ç°æéæ£æ¥ï¼è¦æ³¨æè¿æ»¤è¿äºå¯¹éææ件ç请æ±ã
ããå¦ä½ä½ çDispatcherServletæ¦æª *.doè¿æ ·çURLåç¼ï¼å°±ä¸åä¸è¿°é®é¢äºãè¿æ¯æåç¼æ¹ä¾¿ã
温馨提示:答案为网友推荐,仅供参考