SpringMVC一:环境搭建

springmvc环境搭建,只涉及到springmvc,未做其他整合

1、spring的jar包

2、在web.xml中添加如下:

     2.1 DispatcherServlet 

<!-- 配置spring mvc核心servlet -->  
	    <servlet>
	        <servlet-name>example</servlet-name>
	        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	        <load-on-startup>1</load-on-startup>
	    </servlet>  
	    <!-- url-pattern配置为/,不带文件后缀,会造成其它静态文件(js,css等)不能访问。如配为*.do,则不影响静态文件的访问 -->  
	     <servlet-mapping>
	        <servlet-name>example</servlet-name>
	        <url-pattern>/</url-pattern>
	    </servlet-mapping>

    2.2 解决中文乱码

<filter>
	  <filter-name>characterEncoding</filter-name>
	  <filter-class>
                  org.springframework.web.filter.CharacterEncodingFilter
          </filter-class>
	  <init-param>
		<param-name>encoding</param-name>
 		<param-value>UTF-8</param-value>
  	</init-param>
  	<init-param>
  		<param-name>forceEncoding</param-name>
  		<param-value>true</param-value>
  	</init-param>
</filter>
<filter-mapping>
  	<filter-name>characterEncoding</filter-name>
  	<url-pattern>/*</url-pattern>
</filter-mapping>

   3. 在WEB-INF下(默认)创建[servlet-name]-servlet.xml

<!-- where to scan -->
<context:component-scan base-package="com.xxx.smvc.controller" />  
<!-- mvc annotation -->
<mvc:annotation-driven/>
<!-- servlet default handler -->
<mvc:default-servlet-handler/>
<!-- 处理静态资源 -->
<mvc:resources location="/resources" mapping="/resources/**" />
<!-- 
<bean name="/example.html" class="com.xxx.smvc.controller.ExampleController" />
-->
<!-- 
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
	    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
	    <property name="prefix" value="/WEB-INF/jsp/"/>
	    <property name="suffix" value=".jsp"/>
</bean>
         -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
	<property name="prefix" value="/WEB-INF/"/>  
        <property name="suffix" value=".jsp" />  
</bean>
<!-- file upload -->
<bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
	<property name="maxUploadSize" value="10240000"></property>
</bean>

 4 编写controller

至此,一个简单的Springmvc环境搭建完成

相关推荐