【spring】搭建springmvc

  1. 在web.xml中添加前端控制其<servlet>
    <servlet>
            <!-- 读取WEB-INF下springmvc-serlvet.xml文件 -->
    	<servlet-name>springmvc</servlet-name>
    	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    	<load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
    	<servlet-name>springmvc</servlet-name>
    	<url-pattern>/</url-pattern>
    </servlet-mapping>


  2.  在springmvc-servlet.xml中配置部件扫描,将@Componet,@Controller,@Service,@Repository等注解的类转换为Bean
    <context:component-scan />
     

  3. 在springmvc-servlet.xml中配置注解驱动,将请求参数绑定到控制器的方法参数上
    <mvc:annotation-driven/> 


  4. 在springmvc-servlet.xml中配置处理对静态资源的请求
    <mvc:resources mapping="/resources/**" location="/resources/" />
     

  5. 配置处理器映射类(HandlerMapping)来明确地将请求分发给哪个控制器(Controller)
    <!-- 将客户端的请求映射给使用@RequestMapping注解的控制器和控制器方法 -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
            <property name="useSuffixPatternMatch" value="false" />
    </bean>
    
     

  6. 配置文件上传解析器
    <bean id="multipartResolver"
    		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

     
  7. 配置视图解析器,这里使用综合视图解析器
    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    	<property name="mediaTypes">
    		<map>
    			<entry key="html" value="text/html"/>
    			<entry key="json" value="application/json"/>
    		</map>
    	</property>
    	<property name="viewResolvers">
    		<list>
    			<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    				<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    				<property name="prefix" value="/page/"/>
    				<property name="suffix" value=".jsp"/>
    			</bean>
    		</list>
    	</property>
    	<property name="defaultViews">
    		<list>
    			<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
    				<property name="prefixJson" value="false"/>
    			</bean>
    		</list>
    	</property>
    </bean>

     
  8. ---待续

你可能感兴趣的:(springMVC)