接上一篇:web.xmlhtml
Spring-servlet.xmlweb
掃描指定的包中的類上的註解,經常使用的註解有:spring
@Controller 聲明Action組件
@Service 聲明Service組件 @Service("myMovieLister")
@Repository 聲明Dao組件
@Component 泛指組件, 當很差歸類時.
@RequestMapping("/menu") 請求映射
@Resource 用於注入,( j2ee提供的 ) 默認按名稱裝配,@Resource(name="beanName")
@Autowired 用於注入,(srping提供的) 默認按類型裝配
@Transactional( rollbackFor={Exception.class}) 事務管理
@ResponseBody
@Scope("prototype") 設定bean的做用域spring-mvc
1 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 2 <property name="prefix" value="/WEB-INF/views/"></property> 3 <property name="suffix" value=".jsp"></property> 4 <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> 5 </bean>
1 <mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources>
匹配URL /resources/** 的URL被當作靜態資源,由Spring讀出到內存中再響應http。mvc
或直接使用默認的Servlet來響應靜態文件。app
<mvc:default-servlet-handler/>
示例:jsp
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemaLocation=" 8 http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans.xsd 10 http://www.springframework.org/schema/context 11 http://www.springframework.org/schema/context/spring-context.xsd 12 http://www.springframework.org/schema/mvc 13 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 14 <context:component-scan base-package="com.spring.mvc.controller"/> 15 <mvc:annotation-driven/> 16 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 17 <property name="prefix" value="/WEB-INF/views/"></property> 18 <property name="suffix" value=".jsp"></property> 19 </bean> 20 <!--添加訪問靜態資源的功能 --> 21 <mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources> 22 </beans>